Skip to content

Commit f985a67

Browse files
Fixes:apache#7837: Add isolationMethods and vlan to TrafficTypeResponse (apache#8151)
Co-authored-by: dahn <[email protected]> Co-authored-by: dahn <[email protected]>
1 parent 5f9e131 commit f985a67

File tree

6 files changed

+167
-24
lines changed

6 files changed

+167
-24
lines changed

api/src/main/java/com/cloud/network/PhysicalNetworkTrafficType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public interface PhysicalNetworkTrafficType extends InternalIdentity, Identity {
4141
String getHypervNetworkLabel();
4242

4343
String getOvm3NetworkLabel();
44+
45+
String getVlan();
4446
}

api/src/main/java/org/apache/cloudstack/api/command/admin/usage/ListTrafficTypesCmd.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
import org.apache.cloudstack.api.Parameter;
2727
import org.apache.cloudstack.api.response.ListResponse;
2828
import org.apache.cloudstack.api.response.PhysicalNetworkResponse;
29-
import org.apache.cloudstack.api.response.ProviderResponse;
3029
import org.apache.cloudstack.api.response.TrafficTypeResponse;
3130

3231
import com.cloud.network.PhysicalNetworkTrafficType;
3332
import com.cloud.user.Account;
3433
import com.cloud.utils.Pair;
3534

36-
@APICommand(name = "listTrafficTypes", description = "Lists traffic types of a given physical network.", responseObject = ProviderResponse.class, since = "3.0.0",
35+
@APICommand(name = "listTrafficTypes", description = "Lists traffic types of a given physical network.", responseObject = TrafficTypeResponse.class, since = "3.0.0",
3736
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
3837
public class ListTrafficTypesCmd extends BaseListCmd {
3938

api/src/main/java/org/apache/cloudstack/api/response/TrafficTypeResponse.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public class TrafficTypeResponse extends BaseResponse {
5656
@Param(description = "The network name label of the physical device dedicated to this traffic on a HyperV host")
5757
private String hypervNetworkLabel;
5858

59+
@SerializedName(ApiConstants.VLAN)
60+
@Param(description = "The VLAN id to be used for Management traffic by VMware host")
61+
private String vlan;
62+
63+
@SerializedName(ApiConstants.ISOLATION_METHODS)
64+
@Param(description = "isolation methods for the physical network traffic")
65+
private String isolationMethods;
66+
5967
@SerializedName(ApiConstants.OVM3_NETWORK_LABEL)
6068
@Param(description = "The network name of the physical device dedicated to this traffic on an OVM3 host")
6169
private String ovm3NetworkLabel;
@@ -128,4 +136,20 @@ public String getOvm3Label() {
128136
public void setOvm3Label(String ovm3Label) {
129137
this.ovm3NetworkLabel = ovm3Label;
130138
}
139+
140+
public String getIsolationMethods() {
141+
return isolationMethods;
142+
}
143+
144+
public void setIsolationMethods(String isolationMethods) {
145+
this.isolationMethods = isolationMethods;
146+
}
147+
148+
public String getVlan() {
149+
return vlan;
150+
}
151+
152+
public void setVlan(String vlan) {
153+
this.vlan = vlan;
154+
}
131155
}

server/src/main/java/com/cloud/api/ApiResponseHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,9 @@ public TrafficTypeResponse createTrafficTypeResponse(PhysicalNetworkTrafficType
32563256
PhysicalNetwork pnet = ApiDBUtils.findPhysicalNetworkById(result.getPhysicalNetworkId());
32573257
if (pnet != null) {
32583258
response.setPhysicalNetworkId(pnet.getUuid());
3259+
if (!pnet.getIsolationMethods().isEmpty()) {
3260+
response.setIsolationMethods(String.join(",", pnet.getIsolationMethods()));
3261+
}
32593262
}
32603263
if (result.getTrafficType() != null) {
32613264
response.setTrafficType(result.getTrafficType().toString());
@@ -3266,6 +3269,7 @@ public TrafficTypeResponse createTrafficTypeResponse(PhysicalNetworkTrafficType
32663269
response.setVmwareLabel(result.getVmwareNetworkLabel());
32673270
response.setHypervLabel(result.getHypervNetworkLabel());
32683271
response.setOvm3Label(result.getOvm3NetworkLabel());
3272+
response.setVlan(result.getVlan());
32693273

32703274
response.setObjectName("traffictype");
32713275
return response;

server/src/test/java/com/cloud/api/ApiResponseHelperTest.java

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616
// under the License.
1717
package com.cloud.api;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.assertTrue;
22-
import static org.mockito.ArgumentMatchers.any;
23-
import static org.mockito.ArgumentMatchers.anyLong;
24-
import static org.mockito.Mockito.verify;
25-
import static org.mockito.Mockito.when;
26-
2719
import java.lang.reflect.Field;
2820
import java.text.ParseException;
2921
import java.text.SimpleDateFormat;
@@ -37,9 +29,24 @@
3729
import java.util.TimeZone;
3830
import java.util.UUID;
3931

32+
import org.junit.After;
33+
import org.junit.Assert;
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.mockito.InjectMocks;
38+
import org.mockito.Mock;
39+
import org.mockito.MockedStatic;
40+
import org.mockito.Mockito;
41+
import org.mockito.Spy;
42+
import org.mockito.junit.MockitoJUnitRunner;
43+
import org.springframework.test.util.ReflectionTestUtils;
44+
4045
import org.apache.cloudstack.annotation.dao.AnnotationDao;
46+
import org.apache.cloudstack.api.ResponseObject;
4147
import org.apache.cloudstack.api.response.AutoScaleVmGroupResponse;
4248
import org.apache.cloudstack.api.response.AutoScaleVmProfileResponse;
49+
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
4350
import org.apache.cloudstack.api.response.DirectDownloadCertificateResponse;
4451
import org.apache.cloudstack.api.response.GuestOSCategoryResponse;
4552
import org.apache.cloudstack.api.response.IpQuarantineResponse;
@@ -48,26 +55,17 @@
4855
import org.apache.cloudstack.api.response.TemplateResponse;
4956
import org.apache.cloudstack.api.response.UnmanagedInstanceResponse;
5057
import org.apache.cloudstack.api.response.UsageRecordResponse;
58+
import org.apache.cloudstack.api.response.TrafficTypeResponse;
5159
import org.apache.cloudstack.context.CallContext;
5260
import org.apache.cloudstack.usage.UsageService;
5361
import org.apache.cloudstack.vm.UnmanagedInstanceTO;
54-
import org.junit.After;
55-
import org.junit.Assert;
56-
import org.junit.Before;
57-
import org.junit.Test;
58-
import org.junit.runner.RunWith;
59-
import org.mockito.InjectMocks;
60-
import org.mockito.Mock;
61-
import org.mockito.MockedStatic;
62-
import org.mockito.Mockito;
63-
import org.mockito.Spy;
64-
import org.mockito.junit.MockitoJUnitRunner;
65-
import org.springframework.test.util.ReflectionTestUtils;
6662

6763
import com.cloud.capacity.Capacity;
6864
import com.cloud.configuration.Resource;
6965
import com.cloud.domain.DomainVO;
7066
import com.cloud.host.HostVO;
67+
import com.cloud.network.Networks;
68+
import com.cloud.network.PhysicalNetworkTrafficType;
7169
import com.cloud.network.PublicIpQuarantine;
7270
import com.cloud.network.as.AutoScaleVmGroup;
7371
import com.cloud.network.as.AutoScaleVmGroupVO;
@@ -78,6 +76,8 @@
7876
import com.cloud.network.dao.LoadBalancerVO;
7977
import com.cloud.network.dao.NetworkServiceMapDao;
8078
import com.cloud.network.dao.NetworkVO;
79+
import com.cloud.network.dao.PhysicalNetworkVO;
80+
import com.cloud.network.dao.PhysicalNetworkTrafficTypeVO;
8181
import com.cloud.resource.icon.ResourceIconVO;
8282
import com.cloud.server.ResourceIcon;
8383
import com.cloud.server.ResourceIconManager;
@@ -97,8 +97,16 @@
9797
import com.cloud.vm.ConsoleSessionVO;
9898
import com.cloud.vm.NicSecondaryIp;
9999
import com.cloud.vm.VMInstanceVO;
100-
import org.apache.cloudstack.api.ResponseObject;
101-
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
100+
101+
import static org.junit.Assert.assertEquals;
102+
import static org.junit.Assert.assertFalse;
103+
import static org.junit.Assert.assertNull;
104+
import static org.junit.Assert.assertTrue;
105+
import static org.mockito.ArgumentMatchers.any;
106+
import static org.mockito.ArgumentMatchers.anyLong;
107+
import static org.mockito.Mockito.verify;
108+
import static org.mockito.Mockito.when;
109+
102110

103111
@RunWith(MockitoJUnitRunner.class)
104112
public class ApiResponseHelperTest {
@@ -417,6 +425,39 @@ public void testAutoScaleVmProfileResponseWithoutUserData() {
417425
}
418426
}
419427

428+
@Test
429+
public void testCreateTrafficTypeResponse() {
430+
PhysicalNetworkVO pnet = new PhysicalNetworkVO();
431+
pnet.addIsolationMethod("VXLAN");
432+
pnet.addIsolationMethod("STT");
433+
434+
try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
435+
when(ApiDBUtils.findPhysicalNetworkById(anyLong())).thenReturn(pnet);
436+
String xenLabel = "xen";
437+
String kvmLabel = "kvm";
438+
String vmwareLabel = "vmware";
439+
String simulatorLabel = "simulator";
440+
String hypervLabel = "hyperv";
441+
String ovmLabel = "ovm";
442+
String vlan = "vlan";
443+
String trafficType = "Public";
444+
PhysicalNetworkTrafficType pnetTrafficType = new PhysicalNetworkTrafficTypeVO(pnet.getId(), Networks.TrafficType.getTrafficType(trafficType), xenLabel, kvmLabel, vmwareLabel, simulatorLabel, vlan, hypervLabel, ovmLabel);
445+
446+
TrafficTypeResponse response = apiResponseHelper.createTrafficTypeResponse(pnetTrafficType);
447+
assertFalse(UUID.fromString(response.getId()).toString().isEmpty());
448+
assertEquals(response.getphysicalNetworkId(), pnet.getUuid());
449+
assertEquals(response.getTrafficType(), trafficType);
450+
assertEquals(response.getXenLabel(), xenLabel);
451+
assertEquals(response.getKvmLabel(), kvmLabel);
452+
assertEquals(response.getVmwareLabel(), vmwareLabel);
453+
assertEquals(response.getHypervLabel(), hypervLabel);
454+
assertEquals(response.getOvm3Label(), ovmLabel);
455+
assertEquals(response.getVlan(), vlan);
456+
assertEquals(response.getIsolationMethods(), "VXLAN,STT");
457+
458+
}
459+
}
460+
420461
private UnmanagedInstanceTO getUnmanagedInstaceForTests() {
421462
UnmanagedInstanceTO instance = Mockito.mock(UnmanagedInstanceTO.class);
422463
Mockito.when(instance.getPowerState()).thenReturn(UnmanagedInstanceTO.PowerState.PowerOff);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# Import Local Modules
19+
from marvin.cloudstackTestCase import cloudstackTestCase
20+
from marvin.lib.base import (
21+
TrafficType,
22+
PhysicalNetwork,
23+
Zone,
24+
)
25+
from nose.plugins.attrib import attr
26+
27+
28+
class TestTrafficTypeApi(cloudstackTestCase):
29+
@classmethod
30+
def setUpClass(cls):
31+
cls.test_client = super(TestTrafficTypeApi, cls).getClsTestClient()
32+
cls.api_client = cls.testClient.getApiClient()
33+
cls.services = cls.testClient.getParsedTestDataConfig()
34+
cls._cleanup = []
35+
cls.services["advanced_sg"]["zone"]["name"] = "TestTrafficTypeApi-zone"
36+
cls.zone = Zone.create(cls.api_client, cls.services["advanced_sg"]["zone"])
37+
cls._cleanup.append(cls.zone)
38+
39+
cls.physical_network = PhysicalNetwork.create(
40+
cls.api_client,
41+
cls.services["l2-network"],
42+
isolationmethods="VLAN",
43+
zoneid=cls.zone.id,
44+
)
45+
cls._cleanup.append(cls.physical_network)
46+
47+
@classmethod
48+
def tearDownClass(cls):
49+
super(TestTrafficTypeApi, cls).tearDownClass()
50+
51+
52+
@attr(tags=["advanced"], required_hardware="false")
53+
def test_list_api_fields(self):
54+
traffic_type = TrafficType.add(
55+
self.api_client,
56+
physicalnetworkid=self.physical_network.id,
57+
kvmnetworklabel="kvm",
58+
traffictype="Public",
59+
vlan="100",
60+
).traffictype
61+
62+
traffic_types = TrafficType.list(
63+
self.api_client,
64+
physicalnetworkid=self.physical_network.id
65+
)
66+
67+
assert len(traffic_types) == 1
68+
response = traffic_types[0]
69+
self.assertEqual(response.id, traffic_type.id)
70+
self.assertEqual(response.kvmnetworklabel, "kvm")
71+
self.assertEqual(response.traffictype, "Public")
72+
self.assertEqual(response.vlan, "100")
73+
self.assertEqual(response.isolationmethods, "VLAN")

0 commit comments

Comments
 (0)