Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public class ApiConstants {
public static final String IS_DEFAULT_USE = "defaultuse";
public static final String OLD_FORMAT = "oldformat";
public static final String OP = "op";
public static final String OPTION = "option";
public static final String OPTIONS = "options";
public static final String OS_CATEGORY_ID = "oscategoryid";
public static final String OS_CATEGORY_NAME = "oscategoryname";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.network;

import com.cloud.utils.net.NetworkProtocols;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.NetworkProtocolResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

@APICommand(name = "listNetworkProtocols", description = "Lists details of network protocols", responseObject = NetworkProtocolResponse.class,
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false,
authorized = { RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin, RoleType.User}, since = "4.19.0")
public class ListNetworkProtocolsCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(ListNetworkProtocolsCmd.class.getName());


/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@Parameter(name = ApiConstants.OPTION, type = CommandType.STRING, required = true,
description = "The option of network protocols. Supported values are: protocolnumber, icmptype.")
private String option;


/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////


public String getOption() {
return option;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() {
ListResponse<NetworkProtocolResponse> response = new ListResponse<>();
List<NetworkProtocolResponse> networkProtocolResponses = new ArrayList<>();

NetworkProtocols.Option option = NetworkProtocols.Option.getOption(getOption());
switch (option) {
case ProtocolNumber:
updateResponseWithProtocolNumbers(networkProtocolResponses);
break;
case IcmpType:
updateResponseWithIcmpTypes(networkProtocolResponses);
break;
default:
break;
}

response.setResponses(networkProtocolResponses);
response.setResponseName(getCommandName());
setResponseObject(response);
}

@Override
public long getEntityOwnerId() {
return CallContext.current().getCallingAccount().getId();
}

private void updateResponseWithProtocolNumbers(List<NetworkProtocolResponse> responses) {
for (NetworkProtocols.ProtocolNumber protocolNumber : NetworkProtocols.ProtocolNumbers) {
NetworkProtocolResponse networkProtocolResponse = new NetworkProtocolResponse(protocolNumber.getNumber(),
protocolNumber.getKeyword(), protocolNumber.getProtocol());
networkProtocolResponse.setObjectName("networkprotocol");
responses.add(networkProtocolResponse);
}
}

private void updateResponseWithIcmpTypes(List<NetworkProtocolResponse> responses) {
for (NetworkProtocols.IcmpType icmpType : NetworkProtocols.IcmpTypes) {
NetworkProtocolResponse networkProtocolResponse = new NetworkProtocolResponse(icmpType.getType(),
null, icmpType.getDescription());
for (NetworkProtocols.IcmpCode code : icmpType.getIcmpCodes()) {
networkProtocolResponse.addDetail(String.valueOf(code.getCode()), code.getDescription());
}
networkProtocolResponse.setObjectName("networkprotocol");
responses.add(networkProtocolResponse);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.response;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;

import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;

public class NetworkProtocolResponse extends BaseResponse {
@SerializedName(ApiConstants.INDEX)
@Param(description = "the index (ID, Value, Code, Type, Option, etc) of the protocol parameter")
private Integer index;

@SerializedName(ApiConstants.NAME)
@Param(description = "the name of the protocol parameter")
private String name;

@SerializedName(ApiConstants.DESCRIPTION)
@Param(description = "the description of the protocol parameter")
private String description;

@SerializedName(ApiConstants.DETAILS)
@Param(description = "the details of the protocol parameter")
private Map details;

public NetworkProtocolResponse(Integer index, String name, String description) {
this.index = index;
this.name = name;
this.description = description;
}

public Integer getIndex() {
return index;
}

public void setIndex(Integer index) {
this.index = index;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Map getDetails() {
return details;
}

public void setDetails(Map details) {
this.details = details;
}

public void addDetail(String key, String value) {
if (this.details == null) {
this.details = new LinkedHashMap();
}
this.details.put(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.api.command.user.network;

import com.cloud.utils.net.NetworkProtocols;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.NetworkProtocolResponse;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class ListNetworkProtocolsCmdTest {

@Test
public void testListNetworkProtocolNumbers() {
ListNetworkProtocolsCmd cmd = new ListNetworkProtocolsCmd();
String option = NetworkProtocols.Option.ProtocolNumber.toString();
ReflectionTestUtils.setField(cmd, "option", option);
Assert.assertEquals(cmd.getOption(), option);

try {
cmd.execute();
} catch (Exception e) {
e.printStackTrace();
}
Object response = cmd.getResponseObject();
Assert.assertTrue(response instanceof ListResponse);
ListResponse listResponse = (ListResponse) response;
Assert.assertEquals(BaseCmd.getResponseNameByClass(cmd.getClass()), listResponse.getResponseName());
Assert.assertNotNull(listResponse.getResponses());
Assert.assertNotEquals(0, listResponse.getResponses().size());
Object firstResponse = listResponse.getResponses().get(0);
Assert.assertTrue(firstResponse instanceof NetworkProtocolResponse);
Assert.assertEquals("networkprotocol", ((NetworkProtocolResponse) firstResponse).getObjectName());
Assert.assertEquals(Integer.valueOf(0), ((NetworkProtocolResponse) firstResponse).getIndex());
Assert.assertEquals("HOPOPT", ((NetworkProtocolResponse) firstResponse).getName());
}

@Test
public void testListIcmpTypes() {
ListNetworkProtocolsCmd cmd = new ListNetworkProtocolsCmd();
String option = NetworkProtocols.Option.IcmpType.toString();
ReflectionTestUtils.setField(cmd, "option", option);
Assert.assertEquals(cmd.getOption(), option);

try {
cmd.execute();
} catch (Exception e) {
e.printStackTrace();
}
Object response = cmd.getResponseObject();
Assert.assertTrue(response instanceof ListResponse);
ListResponse listResponse = (ListResponse) response;
Assert.assertEquals(BaseCmd.getResponseNameByClass(cmd.getClass()), listResponse.getResponseName());
Assert.assertNotNull(listResponse.getResponses());
Assert.assertNotEquals(0, listResponse.getResponses().size());
Object firstResponse = listResponse.getResponses().get(0);
Assert.assertTrue(firstResponse instanceof NetworkProtocolResponse);
Assert.assertEquals("networkprotocol", ((NetworkProtocolResponse) firstResponse).getObjectName());
Assert.assertEquals(Integer.valueOf(0), ((NetworkProtocolResponse) firstResponse).getIndex());
Assert.assertNotNull(((NetworkProtocolResponse) firstResponse).getDetails());
System.out.println(((NetworkProtocolResponse) firstResponse).getDetails());
Assert.assertEquals("Echo reply", ((NetworkProtocolResponse) firstResponse).getDetails().get("0"));
}

@Test(expected = IllegalArgumentException.class)
public void testListInvalidOption() {
ListNetworkProtocolsCmd cmd = new ListNetworkProtocolsCmd();
String option = "invalid-option";
ReflectionTestUtils.setField(cmd, "option", option);
Assert.assertEquals(cmd.getOption(), option);

cmd.execute();
}
}
66 changes: 66 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,56 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-agent</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-cluster</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-db</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-events</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-jobs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-managed-context</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-security</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-framework-spring-module</artifactId>
Expand All @@ -81,6 +131,11 @@
<artifactId>cloud-framework-spring-lifecycle</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-storage-volume-adaptive</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-storage-volume-solidfire</artifactId>
Expand Down Expand Up @@ -602,6 +657,16 @@
<artifactId>cloud-plugin-storage-object-simulator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-usage</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-utils</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -911,6 +976,7 @@
<exclude>mysql:mysql-connector-java</exclude>
<exclude>org.apache.cloudstack:cloud-plugin-storage-volume-storpool</exclude>
<exclude>org.apache.cloudstack:cloud-plugin-storage-volume-linstor</exclude>
<exclude>org.apache.cloudstack:cloud-usage</exclude>
<exclude>com.linbit.linstor.api:java-linstor</exclude>
</excludes>
</artifactSet>
Expand Down
Loading