Skip to content

Fixes:#7837: Add isolationMethods and vlan to TrafficTypeResponse #8151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public interface PhysicalNetworkTrafficType extends InternalIdentity, Identity {
String getHypervNetworkLabel();

String getOvm3NetworkLabel();

String getVlan();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.PhysicalNetworkResponse;
import org.apache.cloudstack.api.response.ProviderResponse;
import org.apache.cloudstack.api.response.TrafficTypeResponse;

import com.cloud.network.PhysicalNetworkTrafficType;
import com.cloud.user.Account;
import com.cloud.utils.Pair;

@APICommand(name = "listTrafficTypes", description = "Lists traffic types of a given physical network.", responseObject = ProviderResponse.class, since = "3.0.0",
@APICommand(name = "listTrafficTypes", description = "Lists traffic types of a given physical network.", responseObject = TrafficTypeResponse.class, since = "3.0.0",
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
public class ListTrafficTypesCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListTrafficTypesCmd.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public class TrafficTypeResponse extends BaseResponse {
@Param(description = "The network name label of the physical device dedicated to this traffic on a HyperV host")
private String hypervNetworkLabel;

@SerializedName(ApiConstants.VLAN)
@Param(description = "The VLAN id to be used for Management traffic by VMware host")
private String vlan;

@SerializedName(ApiConstants.ISOLATION_METHODS)
@Param(description = "isolation methods for the physical network traffic")
private String isolationMethods;

@SerializedName(ApiConstants.OVM3_NETWORK_LABEL)
@Param(description = "The network name of the physical device dedicated to this traffic on an OVM3 host")
private String ovm3NetworkLabel;
Expand Down Expand Up @@ -128,4 +136,20 @@ public String getOvm3Label() {
public void setOvm3Label(String ovm3Label) {
this.ovm3NetworkLabel = ovm3Label;
}

public String getIsolationMethods() {
return isolationMethods;
}

public void setIsolationMethods(String isolationMethods) {
this.isolationMethods = isolationMethods;
}

public String getVlan() {
return vlan;
}

public void setVlan(String vlan) {
this.vlan = vlan;
}
}
4 changes: 4 additions & 0 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,9 @@ public TrafficTypeResponse createTrafficTypeResponse(PhysicalNetworkTrafficType
PhysicalNetwork pnet = ApiDBUtils.findPhysicalNetworkById(result.getPhysicalNetworkId());
if (pnet != null) {
response.setPhysicalNetworkId(pnet.getUuid());
if (!pnet.getIsolationMethods().isEmpty()) {
response.setIsolationMethods(String.join(",", pnet.getIsolationMethods()));
}
}
if (result.getTrafficType() != null) {
response.setTrafficType(result.getTrafficType().toString());
Expand All @@ -3094,6 +3097,7 @@ public TrafficTypeResponse createTrafficTypeResponse(PhysicalNetworkTrafficType
response.setVmwareLabel(result.getVmwareNetworkLabel());
response.setHypervLabel(result.getHypervNetworkLabel());
response.setOvm3Label(result.getOvm3NetworkLabel());
response.setVlan(result.getVlan());

response.setObjectName("traffictype");
return response;
Expand Down
41 changes: 41 additions & 0 deletions server/src/test/java/com/cloud/api/ApiResponseHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.cloud.api;

import com.cloud.domain.DomainVO;
import com.cloud.network.Networks;
import com.cloud.network.PhysicalNetworkTrafficType;
import com.cloud.network.as.AutoScaleVmGroup;
import com.cloud.network.as.AutoScaleVmGroupVO;
import com.cloud.network.as.AutoScaleVmProfileVO;
Expand All @@ -25,6 +27,9 @@
import com.cloud.network.dao.LoadBalancerVO;
import com.cloud.network.dao.NetworkServiceMapDao;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.network.dao.PhysicalNetworkTrafficTypeVO;

import com.cloud.storage.VMTemplateVO;
import com.cloud.usage.UsageVO;
import com.cloud.user.Account;
Expand All @@ -43,6 +48,8 @@
import org.apache.cloudstack.api.response.DirectDownloadCertificateResponse;
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
import org.apache.cloudstack.api.response.UsageRecordResponse;
import org.apache.cloudstack.api.response.TrafficTypeResponse;

import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.usage.UsageService;
import org.junit.After;
Expand All @@ -66,6 +73,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -367,4 +376,36 @@ public void testAutoScaleVmProfileResponseWithoutUserData() {
assertNull(response.getUserDataDetails());
}
}

@Test
public void testCreateTrafficTypeResponse() {
PhysicalNetworkVO pnet = new PhysicalNetworkVO();
pnet.addIsolationMethod("VXLAN");
pnet.addIsolationMethod("STT");

try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
when(ApiDBUtils.findPhysicalNetworkById(anyLong())).thenReturn(pnet);
String xenLabel = "xen";
String kvmLabel = "kvm";
String vmwareLabel = "vmware";
String simulatorLabel = "simulator";
String hypervLabel = "hyperv";
String ovmLabel = "ovm";
String vlan = "vlan";
String trafficType = "Public";
PhysicalNetworkTrafficType pnetTrafficType = new PhysicalNetworkTrafficTypeVO(pnet.getId(), Networks.TrafficType.getTrafficType(trafficType), xenLabel, kvmLabel, vmwareLabel, simulatorLabel, vlan, hypervLabel, ovmLabel);

TrafficTypeResponse response = apiResponseHelper.createTrafficTypeResponse(pnetTrafficType);
assertFalse(UUID.fromString(response.getId()).toString().isEmpty());
assertEquals(response.getphysicalNetworkId(), pnet.getUuid());
assertEquals(response.getTrafficType(), trafficType);
assertEquals(response.getXenLabel(), xenLabel);
assertEquals(response.getKvmLabel(), kvmLabel);
assertEquals(response.getVmwareLabel(), vmwareLabel);
assertEquals(response.getHypervLabel(), hypervLabel);
assertEquals(response.getOvm3Label(), ovmLabel);
assertEquals(response.getVlan(), vlan);
assertEquals(response.getIsolationMethods(), "VXLAN,STT");
}
}
}
73 changes: 73 additions & 0 deletions test/integration/smoke/test_network_traffic_type_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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.

# Import Local Modules
from marvin.cloudstackTestCase import cloudstackTestCase
from marvin.lib.base import (
TrafficType,
PhysicalNetwork,
Zone,
)
from nose.plugins.attrib import attr


class TestTrafficTypeApi(cloudstackTestCase):
@classmethod
def setUpClass(cls):
cls.test_client = super(TestTrafficTypeApi, cls).getClsTestClient()
cls.api_client = cls.testClient.getApiClient()
cls.services = cls.testClient.getParsedTestDataConfig()
cls._cleanup = []
cls.services["advanced_sg"]["zone"]["name"] = "TestTrafficTypeApi-zone"
cls.zone = Zone.create(cls.api_client, cls.services["advanced_sg"]["zone"])
cls._cleanup.append(cls.zone)

cls.physical_network = PhysicalNetwork.create(
cls.api_client,
cls.services["l2-network"],
isolationmethods="VLAN",
zoneid=cls.zone.id,
)
cls._cleanup.append(cls.physical_network)

@classmethod
def tearDownClass(cls):
super(TestTrafficTypeApi, cls).tearDownClass()


@attr(tags=["advanced"], required_hardware="false")
def test_list_api_fields(self):
traffic_type = TrafficType.add(
self.api_client,
physicalnetworkid=self.physical_network.id,
kvmnetworklabel="kvm",
traffictype="Public",
vlan="100",
).traffictype

traffic_types = TrafficType.list(
self.api_client,
physicalnetworkid=self.physical_network.id
)

assert len(traffic_types) == 1
response = traffic_types[0]
self.assertEqual(response.id, traffic_type.id)
self.assertEqual(response.kvmnetworklabel, "kvm")
self.assertEqual(response.traffictype, "Public")
self.assertEqual(response.vlan, "100")
self.assertEqual(response.isolationmethods, "VLAN")