diff --git a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/MockTungstenAnswerFactory.java b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/MockTungstenAnswerFactory.java index e69de29bb2d1..0eeaae26e8cd 100644 --- a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/MockTungstenAnswerFactory.java +++ b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/MockTungstenAnswerFactory.java @@ -0,0 +1,35 @@ +// 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.network.tungsten.service; + +import org.apache.cloudstack.network.tungsten.agent.api.TungstenAnswer; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class MockTungstenAnswerFactory { + + TungstenAnswer tungstenAnswer; + MockTungstenAnswerFactory(boolean returnResult){ + tungstenAnswer=mock(TungstenAnswer.class); + when(tungstenAnswer.getResult()).thenReturn(returnResult); + } + TungstenAnswer get() { + return tungstenAnswer; + } + +} diff --git a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenElementTest.java b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenElementTest.java index e69de29bb2d1..a9603ac1d3f3 100644 --- a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenElementTest.java +++ b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenElementTest.java @@ -0,0 +1,1014 @@ +// 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.network.tungsten.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.cloud.agent.AgentManager; +import com.cloud.api.ApiDBUtils; +import com.cloud.configuration.Config; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.HostPodVO; +import com.cloud.dc.VlanVO; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.dc.dao.HostPodDao; +import com.cloud.dc.dao.VlanDao; +import com.cloud.deploy.DeployDestination; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.host.HostVO; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor; +import com.cloud.network.IpAddressManager; +import com.cloud.network.Network; +import com.cloud.network.NetworkModel; +import com.cloud.network.Networks; +import com.cloud.network.PhysicalNetworkServiceProvider; +import com.cloud.network.TungstenGuestNetworkIpAddressVO; +import com.cloud.network.dao.IPAddressDao; +import com.cloud.network.dao.IPAddressVO; +import com.cloud.network.dao.LoadBalancerCertMapDao; +import com.cloud.network.dao.LoadBalancerDao; +import com.cloud.network.dao.LoadBalancerVMMapDao; +import com.cloud.network.dao.LoadBalancerVMMapVO; +import com.cloud.network.dao.LoadBalancerVO; +import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkDetailsDao; +import com.cloud.network.dao.NetworkServiceMapDao; +import com.cloud.network.dao.NetworkVO; +import com.cloud.network.dao.PhysicalNetworkDao; +import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao; +import com.cloud.network.dao.PhysicalNetworkTrafficTypeVO; +import com.cloud.network.dao.PhysicalNetworkVO; +import com.cloud.network.dao.TungstenGuestNetworkIpAddressDao; +import com.cloud.network.dao.TungstenProviderDao; +import com.cloud.network.element.TungstenProviderVO; +import com.cloud.network.lb.LoadBalancingRule; +import com.cloud.network.rules.FirewallRule; +import com.cloud.network.rules.FirewallRuleVO; +import com.cloud.network.rules.PortForwardingRule; +import com.cloud.network.rules.StaticNatImpl; +import com.cloud.user.AccountManager; +import com.cloud.user.User; +import com.cloud.uservm.UserVm; +import com.cloud.utils.EncryptionUtil; +import com.cloud.utils.Pair; +import com.cloud.utils.TungstenUtils; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.fsm.StateMachine2; +import com.cloud.utils.net.Ip; +import com.cloud.utils.net.NetUtils; +import com.cloud.vm.Nic; +import com.cloud.vm.NicProfile; +import com.cloud.vm.ReservationContext; +import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachineProfile; +import com.cloud.vm.dao.VMInstanceDao; +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.cloudstack.framework.messagebus.MessageBus; +import org.apache.cloudstack.network.tungsten.agent.api.ApplyTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ApplyTungstenPortForwardingCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AssignTungstenFloatingIpCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenNetworkLoadbalancerCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenVirtualMachineCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenFloatingIpCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenLoadBalancerCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenLoadBalancerListenerCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenVRouterPortCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenVmCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenVmInterfaceCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ReleaseTungstenFloatingIpCommand; +import org.apache.cloudstack.network.tungsten.agent.api.SetupTungstenVRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.TungstenAnswer; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenLoadBalancerHealthMonitorCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenLoadBalancerMemberCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenLoadBalancerPoolCommand; +import org.apache.cloudstack.network.tungsten.dao.TungstenFabricLBHealthMonitorDao; +import org.apache.cloudstack.network.tungsten.dao.TungstenFabricLBHealthMonitorVO; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.Arrays; +import java.util.List; + +@RunWith(MockitoJUnitRunner.class) +public class TungstenElementTest { + @Mock + TungstenFabricUtils tungstenFabricUtils; + @Mock + NetworkModel networkModel; + @Mock + IPAddressDao ipAddressDao; + @Mock + VMInstanceDao vmInstanceDao; + @Mock + LoadBalancerVMMapDao lbVmMapDao; + @Mock + TungstenGuestNetworkIpAddressDao tungstenGuestNetworkIpAddressDao; + @Mock + IpAddressManager ipAddressMgr; + @Mock + ConfigurationDao configDao; + @Mock + LoadBalancerDao lbDao; + @Mock + AccountManager accountMgr; + @Mock + HostDao hostDao; + @Mock + MessageBus messageBus; + @Mock + PhysicalNetworkTrafficTypeDao physicalNetworkTrafficTypeDao; + @Mock + PhysicalNetworkDao physicalNetworkDao; + @Mock + TungstenProviderDao tungstenProviderDao; + @Mock + DataCenterDao dataCenterDao; + @Mock + VlanDao vlanDao; + @Mock + NetworkServiceMapDao networkServiceMapDao; + @Mock + HostPodDao hostPodDao; + @Mock + NetworkDetailsDao networkDetailsDao; + @Mock + AgentManager agentManager; + @Mock + NetworkDao networkDao; + @Mock + TungstenService tungstenService; + @Mock + TungstenFabricLBHealthMonitorDao tungstenFabricLBHealthMonitorDao; + @Mock + LoadBalancerCertMapDao loadBalancerCertMapDao; + + TungstenElement tungstenElement; + + MockedStatic apiDBUtilsMocked; + + MockedStatic encryptionUtilMocked; + + AutoCloseable closeable; + + @Before + public void setup() { + closeable = MockitoAnnotations.openMocks(this); + tungstenElement = new TungstenElement(); + tungstenElement.tungstenFabricUtils = tungstenFabricUtils; + tungstenElement.networkModel = networkModel; + tungstenElement.ipAddressDao = ipAddressDao; + tungstenElement.vmInstanceDao = vmInstanceDao; + tungstenElement.lbVmMapDao = lbVmMapDao; + tungstenElement.tungstenGuestNetworkIpAddressDao = tungstenGuestNetworkIpAddressDao; + tungstenElement.ipAddressMgr = ipAddressMgr; + tungstenElement.configDao = configDao; + tungstenElement.lbDao = lbDao; + tungstenElement.accountMgr = accountMgr; + tungstenElement.hostDao = hostDao; + tungstenElement.messageBus = messageBus; + tungstenElement.physicalNetworkDao = physicalNetworkDao; + tungstenElement.tungstenService = tungstenService; + tungstenElement.tungstenProviderDao = tungstenProviderDao; + tungstenElement.dataCenterDao = dataCenterDao; + tungstenElement.vlanDao = vlanDao; + tungstenElement.networkServiceMapDao = networkServiceMapDao; + tungstenElement.podDao = hostPodDao; + tungstenElement.agentMgr = agentManager; + tungstenElement.networkDetailsDao = networkDetailsDao; + tungstenElement.networkDao = networkDao; + tungstenElement.physicalNetworkTrafficTypeDao = physicalNetworkTrafficTypeDao; + tungstenElement.tungstenFabricLBHealthMonitorDao = tungstenFabricLBHealthMonitorDao; + tungstenElement.loadBalancerCertMapDao = loadBalancerCertMapDao; + + apiDBUtilsMocked = Mockito.mockStatic(ApiDBUtils.class); + encryptionUtilMocked = Mockito.mockStatic(EncryptionUtil.class); + + when(tungstenService.getTungstenProjectFqn(any())).thenReturn("default-domain:default-project"); + } + + @After + public void tearDown() throws Exception { + apiDBUtilsMocked.close(); + encryptionUtilMocked.close(); + closeable.close(); + } + + @Test + public void canHandleSuccessTest() { + Network network = mock(Network.class); + Network.Service service = mock(Network.Service.class); + + when(networkModel.isProviderForNetwork(any(), anyLong())).thenReturn(true); + + assertTrue(tungstenElement.canHandle(network, service)); + } + + @Test + public void canHandleFailTest() { + Network network = mock(Network.class); + Network.Service service = mock(Network.Service.class); + + when(networkModel.isProviderForNetwork(any(), anyLong())).thenReturn(false); + + assertFalse(tungstenElement.canHandle(network, service)); + } + + @Test + public void applyStaticNatsAddRuleSuccessTest() { + Network network = mock(Network.class); + StaticNatImpl staticNat = mock(StaticNatImpl.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + TungstenAnswer assignFloatingIpAnswer = new MockTungstenAnswerFactory(true).get(); + Nic nic = mock(Nic.class); + Network publicNetwork = mock(Network.class); + List staticNatList = List.of(staticNat); + + when(staticNat.isForRevoke()).thenReturn(false); + when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddressVO); + when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vmInstanceVO); + when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(AssignTungstenFloatingIpCommand.class), anyLong())).thenReturn(assignFloatingIpAnswer); + + assertTrue(tungstenElement.applyStaticNats(network, staticNatList)); + } + + @Test + public void applyStaticNatsAddRuleFailTest() { + Network network = mock(Network.class); + StaticNatImpl staticNat = mock(StaticNatImpl.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + TungstenAnswer assignFloatingIpAnswer = new MockTungstenAnswerFactory(false).get(); + Nic nic = mock(Nic.class); + Network publicNetwork = mock(Network.class); + List staticNatList = List.of(staticNat); + + when(staticNat.isForRevoke()).thenReturn(false); + when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddressVO); + when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vmInstanceVO); + when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(AssignTungstenFloatingIpCommand.class), anyLong())).thenReturn(assignFloatingIpAnswer); + + assertFalse(tungstenElement.applyStaticNats(network, staticNatList)); + } + + @Test + public void applyStaticNatsRevokeRuleSuccessTest() { + Network network = mock(Network.class); + StaticNatImpl staticNat = mock(StaticNatImpl.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + TungstenAnswer releaseFloatingIpAnswer = new MockTungstenAnswerFactory(true).get(); + Nic nic = mock(Nic.class); + Network publicNetwork = mock(Network.class); + List staticNatList = List.of(staticNat); + + when(staticNat.isForRevoke()).thenReturn(true); + when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddressVO); + when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vmInstanceVO); + when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(ReleaseTungstenFloatingIpCommand.class), anyLong())).thenReturn(releaseFloatingIpAnswer); + + assertTrue(tungstenElement.applyStaticNats(network, staticNatList)); + } + + @Test + public void applyStaticNatsRevokeRuleFailTest() { + Network network = mock(Network.class); + StaticNatImpl staticNat = mock(StaticNatImpl.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + TungstenAnswer releaseFloatingIpAnswer = new MockTungstenAnswerFactory(false).get(); + Nic nic = mock(Nic.class); + Network publicNetwork = mock(Network.class); + List staticNatList = List.of(staticNat); + + when(staticNat.isForRevoke()).thenReturn(true); + when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddressVO); + when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vmInstanceVO); + when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(ReleaseTungstenFloatingIpCommand.class), anyLong())).thenReturn(releaseFloatingIpAnswer); + + assertFalse(tungstenElement.applyStaticNats(network, staticNatList)); + } + + @Test + public void applyLBRulesAddRuleSuccessTest() { + User caller = mock(User.class); + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + Ip ip = mock(Ip.class); + LoadBalancingRule loadBalancingRule1 = mock(LoadBalancingRule.class); + LoadBalancerVMMapVO loadBalancerVMMapVO = mock(LoadBalancerVMMapVO.class); + LoadBalancerVO loadBalancerVO = mock(LoadBalancerVO.class); + LoadBalancingRule.LbStickinessPolicy lbStickinessPolicy = mock(LoadBalancingRule.LbStickinessPolicy.class); + List loadBalancingRuleList1 = List.of(loadBalancingRule1); + List loadBalancerVMMapVOList = List.of(loadBalancerVMMapVO); + List lbStickinessPolicyList = List.of(lbStickinessPolicy); + List loadBalancerVOList = List.of(loadBalancerVO); + TungstenFabricLBHealthMonitorVO tungstenFabricLBHealthMonitorVO = mock(TungstenFabricLBHealthMonitorVO.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + TungstenAnswer createTungstenNetworkLoadbalancerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenLoadBalancerPoolAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenLoadBalancerMemberAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenHealthMonitorAnswer = new MockTungstenAnswerFactory(true).get(); + LoadBalancingRule.LbSslCert lbSslCert = mock(LoadBalancingRule.LbSslCert.class); + when(lbStickinessPolicy.getMethodName()).thenReturn("AppCookie"); + List> pairList = List.of(new Pair<>("cookieName", "cookieValue")); + + when(lbStickinessPolicy.getParams()).thenReturn(pairList); + when(loadBalancingRule1.getId()).thenReturn(1L); + when(loadBalancingRule1.getState()).thenReturn(FirewallRule.State.Add); + when(loadBalancingRule1.getAlgorithm()).thenReturn("roundrobin"); + when(loadBalancingRule1.getSourcePortStart()).thenReturn(443); + when(loadBalancingRule1.getDefaultPortStart()).thenReturn(443); + when(loadBalancingRule1.getStickinessPolicies()).thenReturn(lbStickinessPolicyList); + when(loadBalancingRule1.getSourceIp()).thenReturn(ip); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(ipAddressVO.getAddress()).thenReturn(ip); + when(lbVmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(loadBalancerVMMapVOList); + when(tungstenGuestNetworkIpAddressVO.getGuestIpAddress()).thenReturn(ip); + when(ip.addr()).thenReturn("10.10.10.10"); + when(tungstenGuestNetworkIpAddressDao.findByNetworkIdAndPublicIp(anyLong(), anyString())).thenReturn(tungstenGuestNetworkIpAddressVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkLoadbalancerCommand.class), anyLong())).thenReturn(createTungstenNetworkLoadbalancerAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerPoolCommand.class), anyLong())).thenReturn(updateTungstenLoadBalancerPoolAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerMemberCommand.class), anyLong())).thenReturn(updateTungstenLoadBalancerMemberAnswer); + when(configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key())).thenReturn("enabled"); + when(tungstenService.updateLoadBalancer(any(), any())).thenReturn(true); + when(EncryptionUtil.generateSignature(anyString(), anyString())).thenReturn("generatedString"); + when(tungstenFabricLBHealthMonitorDao.findByLbId(anyLong())).thenReturn(tungstenFabricLBHealthMonitorVO); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerHealthMonitorCommand.class), anyLong())).thenReturn(updateTungstenHealthMonitorAnswer); + + assertTrue(tungstenElement.applyLBRules(network, loadBalancingRuleList1)); + } + + @Test + public void applyLBRulesAddRuleFailTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + Ip ip = mock(Ip.class); + LoadBalancingRule loadBalancingRule1 = mock(LoadBalancingRule.class); + LoadBalancerVMMapVO loadBalancerVMMapVO = mock(LoadBalancerVMMapVO.class); + LoadBalancerVO loadBalancerVO = mock(LoadBalancerVO.class); + LoadBalancingRule.LbStickinessPolicy lbStickinessPolicy = mock(LoadBalancingRule.LbStickinessPolicy.class); + List loadBalancingRuleList1 = List.of(loadBalancingRule1); + List loadBalancerVMMapVOList = List.of(loadBalancerVMMapVO); + List lbStickinessPolicyList = List.of(lbStickinessPolicy); + List loadBalancerVOList = List.of(loadBalancerVO); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + TungstenAnswer createTungstenNetworkLoadbalancerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenLoadBalancerPoolAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenLoadBalancerMemberAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenHealthMonitorAnswer = new MockTungstenAnswerFactory(true).get(); + List> pairList = List.of(new Pair<>("cookieName", "cookieValue")); + TungstenFabricLBHealthMonitorVO tungstenFabricLBHealthMonitorVO = mock(TungstenFabricLBHealthMonitorVO.class); + + when(lbStickinessPolicy.getMethodName()).thenReturn("AppCookie"); + when(lbStickinessPolicy.getParams()).thenReturn(pairList); + when(loadBalancingRule1.getId()).thenReturn(1L); + when(loadBalancingRule1.getState()).thenReturn(FirewallRule.State.Add); + when(loadBalancingRule1.getAlgorithm()).thenReturn("roundrobin"); + when(loadBalancingRule1.getSourcePortStart()).thenReturn(80); + when(loadBalancingRule1.getDefaultPortStart()).thenReturn(443); + when(loadBalancingRule1.getStickinessPolicies()).thenReturn(lbStickinessPolicyList); + when(loadBalancingRule1.getSourceIp()).thenReturn(ip); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(ipAddressVO.getAddress()).thenReturn(ip); + when(lbVmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(loadBalancerVMMapVOList); + when(ip.addr()).thenReturn("10.10.10.10"); + when(ipAddressMgr.acquireGuestIpAddress(any(), any())).thenReturn("192.168.100.100"); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkLoadbalancerCommand.class), anyLong())).thenReturn(createTungstenNetworkLoadbalancerAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerPoolCommand.class), anyLong())).thenReturn(updateTungstenLoadBalancerPoolAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerMemberCommand.class), anyLong())).thenReturn(updateTungstenLoadBalancerMemberAnswer); + when(configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key())).thenReturn("disabled"); + when(tungstenFabricLBHealthMonitorDao.findByLbId(anyLong())).thenReturn(tungstenFabricLBHealthMonitorVO); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenLoadBalancerHealthMonitorCommand.class), anyLong())).thenReturn(updateTungstenHealthMonitorAnswer); + + assertFalse(tungstenElement.applyLBRules(network, loadBalancingRuleList1)); + } + + @Test + public void applyLBRulesRevokeRuleFailTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + Ip ip1 = mock(Ip.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + LoadBalancingRule loadBalancingRule1 = mock(LoadBalancingRule.class); + LoadBalancerVO loadBalancerVO1 = mock(LoadBalancerVO.class); + List loadBalancingRuleList1 = List.of(loadBalancingRule1); + List loadBalancerVOList1 = List.of(loadBalancerVO1); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenLoadBalancerCommand = new MockTungstenAnswerFactory(true).get(); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(loadBalancingRule1.getSourceIp()).thenReturn(ip1); + when(loadBalancingRule1.getState()).thenReturn(FirewallRule.State.Revoke); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(ipAddressVO.getAddress()).thenReturn(ip1); + when(ip1.addr()).thenReturn("10.10.10.10"); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenLoadBalancerCommand.class), anyLong())).thenReturn(deleteTungstenLoadBalancerCommand); + when(lbDao.listByIpAddress(anyLong())).thenReturn(loadBalancerVOList1); + when(tungstenGuestNetworkIpAddressDao.findByNetworkIdAndPublicIp(anyLong(),anyString())).thenReturn(tungstenGuestNetworkIpAddressVO); + when(tungstenGuestNetworkIpAddressDao.remove(anyLong())).thenReturn(false); + + assertFalse(tungstenElement.applyLBRules(network, loadBalancingRuleList1)); + } + + @Test + public void applyLBRulesRevokeRuleSuccessTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + Ip ip = mock(Ip.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + LoadBalancingRule loadBalancingRule = mock(LoadBalancingRule.class); + LoadBalancerVO loadBalancerVO1 = mock(LoadBalancerVO.class); + LoadBalancerVO loadBalancerVO2 = mock(LoadBalancerVO.class); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + TungstenAnswer deleteTungstenLoadBalancerListenerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenLoadBalancerCommand = new MockTungstenAnswerFactory(true).get(); + List loadBalancingRuleList = List.of(loadBalancingRule); + List loadBalancerVOList = Arrays.asList(loadBalancerVO1, loadBalancerVO2); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(loadBalancingRule.getSourceIp()).thenReturn(ip); + when(loadBalancingRule.getState()).thenReturn(FirewallRule.State.Revoke); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(ip.addr()).thenReturn("10.10.10.10"); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenLoadBalancerListenerCommand.class), anyLong())).thenReturn(deleteTungstenLoadBalancerListenerAnswer); + when(tungstenService.updateLoadBalancer(any(), any())).thenReturn(true); + when(lbDao.listByIpAddress(anyLong())).thenReturn(loadBalancerVOList); + + assertTrue(tungstenElement.applyLBRules(network, loadBalancingRuleList)); + } + + @Test + public void applyPFRulesAddRuleSuccessTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + PortForwardingRule portForwardingRule = mock(PortForwardingRule.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + UserVm userVm = mock(UserVm.class); + Nic nic = mock(Nic.class); + TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(true).get(); + List portForwardingRuleList = List.of(portForwardingRule); + + when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Add); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ApiDBUtils.findIpAddressById(anyLong())).thenReturn(ipAddressVO); + when(ApiDBUtils.findUserVmById(anyLong())).thenReturn(userVm); + when(networkModel.getNicInNetwork(anyLong(), anyLong())).thenReturn(nic); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenPortForwardingCommand.class), anyLong())).thenReturn(applyTungstenPortForwardingAnswer); + + assertTrue(tungstenElement.applyPFRules(network, portForwardingRuleList)); + } + + @Test + public void applyPFRulesAddRuleFailTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + PortForwardingRule portForwardingRule = mock(PortForwardingRule.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + UserVm userVm = mock(UserVm.class); + Nic nic = mock(Nic.class); + TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(false).get(); + List portForwardingRuleList = List.of(portForwardingRule); + + when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Add); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ApiDBUtils.findIpAddressById(anyLong())).thenReturn(ipAddressVO); + when(ApiDBUtils.findUserVmById(anyLong())).thenReturn(userVm); + when(networkModel.getNicInNetwork(anyLong(), anyLong())).thenReturn(nic); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenPortForwardingCommand.class), anyLong())).thenReturn(applyTungstenPortForwardingAnswer); + + assertFalse(tungstenElement.applyPFRules(network, portForwardingRuleList)); + } + + @Test + public void applyPFRulesRevokeRuleSuccessTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + PortForwardingRule portForwardingRule = mock(PortForwardingRule.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + UserVm userVm = mock(UserVm.class); + Nic nic = mock(Nic.class); + TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(true).get(); + List portForwardingRuleList = List.of(portForwardingRule); + + when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Revoke); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ApiDBUtils.findIpAddressById(anyLong())).thenReturn(ipAddressVO); + when(ApiDBUtils.findUserVmById(anyLong())).thenReturn(userVm); + when(networkModel.getNicInNetwork(anyLong(), anyLong())).thenReturn(nic); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenPortForwardingCommand.class), anyLong())).thenReturn(applyTungstenPortForwardingAnswer); + + assertTrue(tungstenElement.applyPFRules(network, portForwardingRuleList)); + } + + @Test + public void applyPFRulesRevokeRuleFailTest() { + Network network = mock(Network.class); + Network publicNetwork = mock(Network.class); + PortForwardingRule portForwardingRule = mock(PortForwardingRule.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + UserVm userVm = mock(UserVm.class); + Nic nic = mock(Nic.class); + TungstenAnswer applyTungstenPortForwardingAnswer = new MockTungstenAnswerFactory(false).get(); + List portForwardingRuleList = List.of(portForwardingRule); + + when(portForwardingRule.getState()).thenReturn(FirewallRule.State.Revoke); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), any())).thenReturn(publicNetwork); + when(ApiDBUtils.findIpAddressById(anyLong())).thenReturn(ipAddressVO); + when(ApiDBUtils.findUserVmById(anyLong())).thenReturn(userVm); + when(networkModel.getNicInNetwork(anyLong(), anyLong())).thenReturn(nic); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenPortForwardingCommand.class), anyLong())).thenReturn(applyTungstenPortForwardingAnswer); + + assertFalse(tungstenElement.applyPFRules(network, portForwardingRuleList)); + } + + @Test + public void preparePublicNetworkTest() throws ConcurrentOperationException { + Network network = mock(Network.class); + NicProfile nicProfile = new NicProfile(); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + DeployDestination deployDestination = mock(DeployDestination.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + TungstenAnswer createTungstenVMAnswer = new MockTungstenAnswerFactory(true).get(); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + TungstenAnswer createTungstenNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + + nicProfile.setIPv4Address("192.168.100.100"); + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenVirtualMachineCommand.class), anyLong())).thenReturn(createTungstenVMAnswer); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createTungstenNetworkPolicyAnswer); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.ConsoleProxy); + + assertTrue(tungstenElement.prepare(network, nicProfile, virtualMachineProfile, deployDestination, reservationContext)); + assertEquals(Nic.ReservationStrategy.Create, nicProfile.getReservationStrategy()); + assertEquals(Networks.BroadcastDomainType.TUNGSTEN, nicProfile.getBroadcastType()); + assertEquals(Networks.BroadcastDomainType.TUNGSTEN.toUri("tf"), nicProfile.getBroadCastUri()); + assertEquals(TungstenUtils.DEFAULT_VHOST_INTERFACE, nicProfile.getName()); + } + + @Test + public void prepareManagementNetworkTest() throws ConcurrentOperationException { + Network network = mock(Network.class); + NicProfile nicProfile = new NicProfile(); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + DeployDestination deployDestination = mock(DeployDestination.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + TungstenAnswer createTungstenVMAnswer = new MockTungstenAnswerFactory(true).get(); + + nicProfile.setIPv4Address("192.168.100.100"); + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenVirtualMachineCommand.class), anyLong())).thenReturn(createTungstenVMAnswer); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.SecondaryStorageVm); + + assertTrue(tungstenElement.prepare(network, nicProfile, virtualMachineProfile, deployDestination, reservationContext)); + assertEquals(Nic.ReservationStrategy.Create, nicProfile.getReservationStrategy()); + assertEquals(Networks.BroadcastDomainType.TUNGSTEN, nicProfile.getBroadcastType()); + assertEquals(Networks.BroadcastDomainType.TUNGSTEN.toUri("tf"), nicProfile.getBroadCastUri()); + assertEquals(TungstenUtils.DEFAULT_VHOST_INTERFACE, nicProfile.getName()); + } + + @Test(expected = CloudRuntimeException.class) + public void prepareWithExceptionTest() throws ConcurrentOperationException { + Network network = mock(Network.class); + NicProfile nicProfile = new NicProfile(); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + DeployDestination deployDestination = mock(DeployDestination.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + TungstenAnswer createTungstenVMAnswer = mock(TungstenAnswer.class); + + nicProfile.setIPv4Address("192.168.100.100"); + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenVirtualMachineCommand.class), anyLong())).thenReturn(createTungstenVMAnswer); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.SecondaryStorageVm); + + tungstenElement.prepare(network, nicProfile, virtualMachineProfile, deployDestination, reservationContext); + } + + @Test + public void releasePublicNetworkTest() throws ConcurrentOperationException, ResourceUnavailableException { + Network network = mock(Network.class); + NicProfile nicProfile = mock(NicProfile.class); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + TungstenAnswer deleteTungstenVRouterPortAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteVmiAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteVmAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteTungstenNetworkPolicyAnswer = mock(TungstenAnswer.class); + + when(nicProfile.getIPv4Address()).thenReturn("192.168.100.100"); + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.SecondaryStorageVm); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong())).thenReturn(deleteTungstenVRouterPortAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmInterfaceCommand.class), anyLong())).thenReturn(deleteVmiAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmCommand.class), anyLong())).thenReturn(deleteVmAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(deleteTungstenNetworkPolicyAnswer); + + assertTrue(tungstenElement.release(network, nicProfile, virtualMachineProfile, reservationContext)); + } + + @Test + public void releaseManagementNetworkTest() throws ConcurrentOperationException, ResourceUnavailableException { + Network network = mock(Network.class); + NicProfile nicProfile = mock(NicProfile.class); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + TungstenAnswer deleteTungstenVRouterPortAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteVmiAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteVmAnswer = mock(TungstenAnswer.class); + + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.SecondaryStorageVm); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong())).thenReturn(deleteTungstenVRouterPortAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmInterfaceCommand.class), anyLong())).thenReturn(deleteVmiAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmCommand.class), anyLong())).thenReturn(deleteVmAnswer); + + assertTrue(tungstenElement.release(network, nicProfile, virtualMachineProfile, reservationContext)); + } + + @Test(expected = CloudRuntimeException.class) + public void releaseWithExceptionTest() throws ConcurrentOperationException, ResourceUnavailableException { + Network network = mock(Network.class); + NicProfile nicProfile = mock(NicProfile.class); + VirtualMachineProfile virtualMachineProfile = mock(VirtualMachineProfile.class); + ReservationContext reservationContext = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO host = mock(HostVO.class); + TungstenAnswer deleteTungstenVRouterPortAnswer = mock(TungstenAnswer.class); + TungstenAnswer deleteVmiAnswer = mock(TungstenAnswer.class); + + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(host); + when(virtualMachineProfile.getType()).thenReturn(VirtualMachine.Type.SecondaryStorageVm); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong())).thenReturn(deleteTungstenVRouterPortAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmInterfaceCommand.class), anyLong())).thenReturn(deleteVmiAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenVmCommand.class), anyLong())).thenThrow(IllegalArgumentException.class); + + tungstenElement.release(network, nicProfile, virtualMachineProfile, reservationContext); + } + + @Test + public void destroyTest() throws ConcurrentOperationException, ResourceUnavailableException { + IPAddressVO ipAddressVO1 = mock(IPAddressVO.class); + IPAddressVO ipAddressVO2 = mock(IPAddressVO.class); + Network network = mock(Network.class); + ReservationContext reservationContext = mock(ReservationContext.class); + List ipAddressVOList = Arrays.asList(ipAddressVO1, ipAddressVO2); + TungstenAnswer tungstenDeleteFIPAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer tungstenDeleteNPAnswer = new MockTungstenAnswerFactory(true).get(); + + when(ipAddressDao.listByAssociatedNetwork(anyLong(), anyBoolean())).thenReturn(ipAddressVOList); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFloatingIpCommand.class), anyLong())).thenReturn(tungstenDeleteFIPAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(tungstenDeleteNPAnswer); + + assertTrue(tungstenElement.destroy(network, reservationContext)); + } + + @Test + public void isReadyTest() { + PhysicalNetworkServiceProvider physicalNetworkServiceProvider = mock(PhysicalNetworkServiceProvider.class); + PhysicalNetworkTrafficTypeVO physicalNetworkTrafficTypeVO = mock(PhysicalNetworkTrafficTypeVO.class); + + when(physicalNetworkTrafficTypeDao.findBy(anyLong(), eq(Networks.TrafficType.Management))).thenReturn(physicalNetworkTrafficTypeVO); + + assertTrue(tungstenElement.isReady(physicalNetworkServiceProvider)); + } + + @Test + public void shutdownProviderInstancesTest() throws ConcurrentOperationException { + PhysicalNetworkServiceProvider physicalNetworkServiceProvider = mock(PhysicalNetworkServiceProvider.class); + ReservationContext reservationContext = mock(ReservationContext.class); + PhysicalNetworkVO physicalNetworkVO = mock(PhysicalNetworkVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + Network publicNetwork = mock(Network.class); + VlanVO vlanVO1 = mock(VlanVO.class); + VlanVO vlanVO2 = mock(VlanVO.class); + List vlanVOList = Arrays.asList(vlanVO1, vlanVO2); + HostPodVO hostPodVO1 = mock(HostPodVO.class); + HostPodVO hostPodVO2 = mock(HostPodVO.class); + List hostPodVOList = Arrays.asList(hostPodVO1, hostPodVO2); + + when(physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetworkVO); + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(vlanDao.listVlansByNetworkIdIncludingRemoved(anyLong())).thenReturn(vlanVOList); + when(hostPodDao.listByDataCenterId(anyLong())).thenReturn(hostPodVOList); + + assertTrue(tungstenElement.shutdownProviderInstances(physicalNetworkServiceProvider, reservationContext)); + verify(networkServiceMapDao, times(1)).deleteByNetworkId(anyLong()); + verify(tungstenService, times(2)).removePublicNetworkSubnet(any(VlanVO.class)); + verify(tungstenService, times(1)).deletePublicNetwork(anyLong()); + verify(tungstenService, times(2)).removeManagementNetworkSubnet(any(HostPodVO.class)); + verify(tungstenService, times(1)).deleteManagementNetwork(anyLong()); + } + + //@Test + //public void processConnectWithoutSecurityGroupTest() throws ConnectionException { + // Host host = mock(Host.class); + // StartupCommand startupCommand = mock(StartupCommand.class); + // TungstenProviderVO tungstenProvider = mock(TungstenProviderVO.class); + // DataCenterVO dataCenterVO = mock(DataCenterVO.class); + // VlanVO vlanVO1 = mock(VlanVO.class); + // VlanVO vlanVO2 = mock(VlanVO.class); + // List vlanList = Arrays.asList(vlanVO1, vlanVO2); + // Network publicNetwork = mock(Network.class); + // NetworkDetailVO networkDetail = mock(NetworkDetailVO.class); +// + // when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); + // when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProvider); + // when(host.getPublicIpAddress()).thenReturn("192.168.100.100"); + // when(tungstenProvider.getGateway()).thenReturn("192.168.100.100"); + // when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + // when(vlanDao.listByZone(anyLong())).thenReturn(vlanList); + // when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + // when(networkDetailsDao.findDetail(anyLong(), anyString())).thenReturn(networkDetail); + // when(vlanVO1.getVlanGateway()).thenReturn("192.168.100.1"); + // when(vlanVO1.getVlanNetmask()).thenReturn("255.255.255.0"); + // when(vlanVO2.getVlanGateway()).thenReturn("192.168.101.1"); + // when(vlanVO2.getVlanNetmask()).thenReturn("255.255.255.0"); + // when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(false); +// + // tungstenElement.processConnect(host, startupCommand, true); + // verify(agentManager, times(1)).easySend(anyLong(), any(SetupTungstenVRouterCommand.class)); + //} + + //@Test + //public void processConnectWithSecurityGroupTest() throws ConnectionException { + // Host host = mock(Host.class); + // StartupCommand startupCommand = mock(StartupCommand.class); + // TungstenProviderVO tungstenProvider = mock(TungstenProviderVO.class); + // DataCenterVO dataCenterVO = mock(DataCenterVO.class); + // NetworkVO network = mock(NetworkVO.class); + // NetworkDetailVO networkDetail = mock(NetworkDetailVO.class); + // Network publicNetwork = mock(Network.class); +// + // when(host.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); + // when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProvider); + // when(host.getPublicIpAddress()).thenReturn("192.168.100.100"); + // when(tungstenProvider.getGateway()).thenReturn("192.168.100.100"); + // when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + // when(networkDao.listByZoneSecurityGroup(anyLong())).thenReturn(Arrays.asList(network)); + // when(networkDetailsDao.findDetail(anyLong(), anyString())).thenReturn(networkDetail); + // when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + // when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(true); +// + // tungstenElement.processConnect(host, startupCommand, true); + // verify(agentManager, times(1)).easySend(anyLong(), any(SetupTungstenVRouterCommand.class)); + //} + + @Test + public void processHostAboutToBeRemovedWithSecurityGroupTest() { + HostVO hostVO = mock(HostVO.class); + TungstenProviderVO tungstenProvider = mock(TungstenProviderVO.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + NetworkVO network = mock(NetworkVO.class); + + when(hostVO.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProvider); + when(hostVO.getPublicIpAddress()).thenReturn("192.168.100.100"); + when(tungstenProvider.getGateway()).thenReturn("192.168.100.100"); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(hostDao.findById(anyLong())).thenReturn(hostVO); + when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(true); + when(networkDao.listByZoneSecurityGroup(anyLong())).thenReturn(List.of(network)); + + tungstenElement.processHostAboutToBeRemoved(1L); + verify(agentManager, times(1)).easySend(anyLong(), any(SetupTungstenVRouterCommand.class)); + } + + @Test + public void processHostAboutToBeRemovedWithoutSecurityGroupTest() { + HostVO hostVO = mock(HostVO.class); + TungstenProviderVO tungstenProvider = mock(TungstenProviderVO.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(hostVO.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProvider); + when(hostVO.getPublicIpAddress()).thenReturn("192.168.100.100"); + when(tungstenProvider.getGateway()).thenReturn("192.168.100.100"); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(hostDao.findById(anyLong())).thenReturn(hostVO); + when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(false); + + tungstenElement.processHostAboutToBeRemoved(1L); + verify(agentManager, times(1)).easySend(anyLong(), any(SetupTungstenVRouterCommand.class)); + } + + @Test + public void applyFWRulesWithAddEgressRuleTest() throws ResourceUnavailableException { + Network network = mock(Network.class); + FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class); + Network publicNetwork = mock(Network.class); + TungstenAnswer createNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + + when(firewallRuleVO.getState()).thenReturn(FirewallRule.State.Add); + when(firewallRuleVO.getSourceCidrList()).thenReturn(List.of("192.168.100.0/24")); + when(firewallRuleVO.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(firewallRuleVO.getPurpose()).thenReturn(FirewallRule.Purpose.Firewall); + when(firewallRuleVO.getTrafficType()).thenReturn(FirewallRule.TrafficType.Egress); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createNetworkPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyNetworkPolicyAnswer); + + assertTrue(tungstenElement.applyFWRules(network, List.of(firewallRuleVO))); + } + + @Test + public void applyFWRulesWithAddIngressRuleTest() throws ResourceUnavailableException { + Network network = mock(Network.class); + FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class); + Network publicNetwork = mock(Network.class); + TungstenAnswer createNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + Ip ip = mock(Ip.class); + + when(firewallRuleVO.getState()).thenReturn(FirewallRule.State.Add); + when(firewallRuleVO.getSourceCidrList()).thenReturn(List.of("192.168.100.0/24")); + when(firewallRuleVO.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(ipAddressDao.findById(anyLong())).thenReturn(ipAddressVO); + when(ipAddressVO.getAddress()).thenReturn(ip); + when(firewallRuleVO.getPurpose()).thenReturn(FirewallRule.Purpose.Firewall); + when(firewallRuleVO.getTrafficType()).thenReturn(FirewallRule.TrafficType.Ingress); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createNetworkPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyNetworkPolicyAnswer); + + assertTrue(tungstenElement.applyFWRules(network, List.of(firewallRuleVO))); + } + + @Test + public void applyFWRulesWithRevokeRuleTest() throws ResourceUnavailableException { + Network network = mock(Network.class); + FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class); + Network publicNetwork = mock(Network.class); + TungstenAnswer deleteNetworkPolicyAnswer = mock(TungstenAnswer.class); + + when(firewallRuleVO.getState()).thenReturn(FirewallRule.State.Revoke); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(firewallRuleVO.getPurpose()).thenReturn(FirewallRule.Purpose.Firewall); + when(firewallRuleVO.getTrafficType()).thenReturn(FirewallRule.TrafficType.Ingress); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(deleteNetworkPolicyAnswer); + + assertTrue(tungstenElement.applyFWRules(network, List.of(firewallRuleVO))); + } + + @Test + public void postStateTransitionEventWithVmStartedTest() { + StateMachine2.Transition transition = mock(StateMachine2.Transition.class); + VMInstanceVO vo = mock(VMInstanceVO.class); + Object opaque = mock(Object.class); + + when(transition.getCurrentState()).thenReturn(VirtualMachine.State.Starting); + when(transition.getToState()).thenReturn(VirtualMachine.State.Running); + when(tungstenService.addTungstenVmSecurityGroup(any(VMInstanceVO.class))).thenReturn(true); + + assertTrue(tungstenElement.postStateTransitionEvent(transition, vo, true, opaque)); + } + + @Test + public void postStateTransitionEventWithVmStopedTest() { + StateMachine2.Transition transition = mock(StateMachine2.Transition.class); + VMInstanceVO vo = mock(VMInstanceVO.class); + Object opaque = mock(Object.class); + + when(transition.getCurrentState()).thenReturn(VirtualMachine.State.Stopping); + when(transition.getToState()).thenReturn(VirtualMachine.State.Stopped); + when(tungstenService.removeTungstenVmSecurityGroup(any(VMInstanceVO.class))).thenReturn(true); + + assertTrue(tungstenElement.postStateTransitionEvent(transition, vo, true, opaque)); + } + + @Test + public void prepareMigrationTest() { + NicProfile nic = mock(NicProfile.class); + Network network = mock(Network.class); + VirtualMachineProfile vm = mock(VirtualMachineProfile.class); + DeployDestination dest = mock(DeployDestination.class); + ReservationContext context = mock(ReservationContext.class); + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + HostVO hostVO = mock(HostVO.class); + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(vm.getType()).thenReturn(VirtualMachine.Type.ConsoleProxy); + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public); + when(vmInstanceDao.findById(anyLong())).thenReturn(vmInstanceVO); + when(hostDao.findById(anyLong())).thenReturn(hostVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenVirtualMachineCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenElement.prepareMigration(nic, network, vm, dest, context)); + } + + @Test + public void rollbackMigration() { + NicProfile nic = mock(NicProfile.class); + Network network = mock(Network.class); + VirtualMachineProfile vm = mock(VirtualMachineProfile.class); + ReservationContext src = mock(ReservationContext.class); + ReservationContext dest = mock(ReservationContext.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + HostVO hostVO = mock(HostVO.class); + + when(vm.getType()).thenReturn(VirtualMachine.Type.ConsoleProxy); + when(vm.getVirtualMachine()).thenReturn(virtualMachine); + when(hostDao.findById(anyLong())).thenReturn(hostVO); + + tungstenElement.rollbackMigration(nic, network, vm, src, dest); + verify(tungstenFabricUtils, times(1)).sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong()); + } + + @Test + public void commitMigration() { + NicProfile nic = mock(NicProfile.class); + Network network = mock(Network.class); + VirtualMachineProfile vm = mock(VirtualMachineProfile.class); + ReservationContext src = mock(ReservationContext.class); + ReservationContext dest = mock(ReservationContext.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + HostVO hostVO = mock(HostVO.class); + + when(vm.getType()).thenReturn(VirtualMachine.Type.ConsoleProxy); + when(vm.getVirtualMachine()).thenReturn(virtualMachine); + when(hostDao.findById(anyLong())).thenReturn(hostVO); + + tungstenElement.commitMigration(nic, network, vm, src, dest); + verify(tungstenFabricUtils, times(1)).sendTungstenCommand(any(DeleteTungstenVRouterPortCommand.class), anyLong()); + } +} \ No newline at end of file diff --git a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenFabricUtilsTest.java b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenFabricUtilsTest.java index e69de29bb2d1..d8e461fb6ae0 100644 --- a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenFabricUtilsTest.java +++ b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenFabricUtilsTest.java @@ -0,0 +1,103 @@ +// 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.network.tungsten.service; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.cloud.agent.AgentManager; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.network.dao.TungstenProviderDao; +import com.cloud.network.element.TungstenProviderVO; +import org.apache.cloudstack.network.tungsten.agent.api.TungstenAnswer; +import org.apache.cloudstack.network.tungsten.agent.api.TungstenCommand; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class TungstenFabricUtilsTest { + @Mock + AgentManager agentMgr; + @Mock + TungstenProviderDao tungstenProviderDao; + + TungstenFabricUtils tungstenFabricUtils; + + AutoCloseable closeable; + + @Before + public void setup() { + closeable = MockitoAnnotations.openMocks(this); + tungstenFabricUtils = new TungstenFabricUtils(); + tungstenFabricUtils.agentMgr = agentMgr; + tungstenFabricUtils.tungstenProviderDao = tungstenProviderDao; + } + + @After + public void tearDown() throws Exception { + closeable.close(); + } + + @Test + public void sendTungstenCommandSuccessTest() { + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenCommand tungstenCommand = mock(TungstenCommand.class); + + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO); + when(agentMgr.easySend(anyLong(), any(TungstenCommand.class))).thenReturn(tungstenAnswer); + + assertEquals(tungstenAnswer, tungstenFabricUtils.sendTungstenCommand(tungstenCommand, anyLong())); + } + + @Test(expected = InvalidParameterValueException.class) + public void sendTungstenCommandWithNullTungstenProvider() { + TungstenCommand tungstenCommand = mock(TungstenCommand.class); + + tungstenFabricUtils.sendTungstenCommand(tungstenCommand, anyLong()); + } + + @Test(expected = InvalidParameterValueException.class) + public void sendTungstenCommandWithNullAnswer() { + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenCommand tungstenCommand = mock(TungstenCommand.class); + + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO); + + tungstenFabricUtils.sendTungstenCommand(tungstenCommand, anyLong()); + } + + @Test(expected = InvalidParameterValueException.class) + public void sendTungstenCommandWithFalseAnswer() { + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(false).get(); + TungstenCommand tungstenCommand = mock(TungstenCommand.class); + + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO); + when(agentMgr.easySend(anyLong(), any(TungstenCommand.class))).thenReturn(tungstenAnswer); + + tungstenFabricUtils.sendTungstenCommand(tungstenCommand, anyLong()); + } +} \ No newline at end of file diff --git a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenServiceImplTest.java b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenServiceImplTest.java index e69de29bb2d1..3eb61510b09f 100644 --- a/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenServiceImplTest.java +++ b/plugins/network-elements/tungsten/src/test/java/org/apache/cloudstack/network/tungsten/service/TungstenServiceImplTest.java @@ -0,0 +1,1671 @@ +// 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.network.tungsten.service; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.cloud.agent.AgentManager; +import com.cloud.agent.api.Answer; +import com.cloud.configuration.Config; +import com.cloud.configuration.ConfigurationManager; +import com.cloud.dc.DataCenter; +import com.cloud.dc.DataCenterIpAddressVO; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.HostPodVO; +import com.cloud.dc.Vlan; +import com.cloud.dc.VlanVO; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.dc.dao.DataCenterIpAddressDao; +import com.cloud.domain.DomainVO; +import com.cloud.domain.dao.DomainDao; +import com.cloud.host.HostVO; +import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor; +import com.cloud.network.IpAddressManager; +import com.cloud.network.Network; +import com.cloud.network.NetworkModel; +import com.cloud.network.NetworkService; +import com.cloud.network.Networks; +import com.cloud.network.TungstenGuestNetworkIpAddressVO; +import com.cloud.network.dao.FirewallRulesDao; +import com.cloud.network.dao.IPAddressDao; +import com.cloud.network.dao.IPAddressVO; +import com.cloud.network.dao.LoadBalancerCertMapDao; +import com.cloud.network.dao.LoadBalancerCertMapVO; +import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkDetailVO; +import com.cloud.network.dao.NetworkDetailsDao; +import com.cloud.network.dao.NetworkVO; +import com.cloud.network.dao.SslCertVO; +import com.cloud.network.dao.TungstenGuestNetworkIpAddressDao; +import com.cloud.network.dao.TungstenProviderDao; +import com.cloud.network.element.TungstenProviderVO; +import com.cloud.network.lb.LoadBalancingRule; +import com.cloud.network.rules.FirewallRule; +import com.cloud.network.rules.FirewallRuleVO; +import com.cloud.network.security.SecurityGroup; +import com.cloud.network.security.SecurityGroupManager; +import com.cloud.network.security.SecurityGroupRuleVO; +import com.cloud.network.security.SecurityGroupService; +import com.cloud.network.security.SecurityGroupVO; +import com.cloud.network.security.SecurityRule; +import com.cloud.network.security.TungstenSecurityGroupRuleVO; +import com.cloud.network.security.dao.SecurityGroupDao; +import com.cloud.network.security.dao.SecurityGroupRuleDao; +import com.cloud.network.security.dao.SecurityGroupVMMapDao; +import com.cloud.network.security.dao.TungstenSecurityGroupRuleDao; +import com.cloud.projects.ProjectManager; +import com.cloud.projects.ProjectVO; +import com.cloud.projects.dao.ProjectDao; +import com.cloud.user.AccountVO; +import com.cloud.user.DomainManager; +import com.cloud.user.dao.AccountDao; +import com.cloud.utils.db.EntityManager; +import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.net.Ip; +import com.cloud.utils.net.NetUtils; +import com.cloud.vm.Nic; +import com.cloud.vm.NicSecondaryIp; +import com.cloud.vm.NicVO; +import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.dao.NicDao; +import com.cloud.vm.dao.NicSecondaryIpDao; +import com.cloud.vm.dao.NicSecondaryIpVO; +import net.juniper.tungsten.api.ApiObjectBase; +import net.juniper.tungsten.api.ApiPropertyBase; +import net.juniper.tungsten.api.ObjectReference; +import net.juniper.tungsten.api.types.ActionListType; +import net.juniper.tungsten.api.types.AddressGroup; +import net.juniper.tungsten.api.types.AddressType; +import net.juniper.tungsten.api.types.ApplicationPolicySet; +import net.juniper.tungsten.api.types.FirewallPolicy; +import net.juniper.tungsten.api.types.FirewallRuleEndpointType; +import net.juniper.tungsten.api.types.FirewallRuleMatchTagsType; +import net.juniper.tungsten.api.types.FirewallSequence; +import net.juniper.tungsten.api.types.FirewallServiceGroupType; +import net.juniper.tungsten.api.types.FirewallServiceType; +import net.juniper.tungsten.api.types.Loadbalancer; +import net.juniper.tungsten.api.types.LogicalRouter; +import net.juniper.tungsten.api.types.NetworkPolicy; +import net.juniper.tungsten.api.types.PolicyEntriesType; +import net.juniper.tungsten.api.types.PolicyRuleType; +import net.juniper.tungsten.api.types.PortType; +import net.juniper.tungsten.api.types.ServiceGroup; +import net.juniper.tungsten.api.types.SubnetListType; +import net.juniper.tungsten.api.types.SubnetType; +import net.juniper.tungsten.api.types.Tag; +import net.juniper.tungsten.api.types.TagType; +import net.juniper.tungsten.api.types.VirtualMachine; +import net.juniper.tungsten.api.types.VirtualMachineInterface; +import net.juniper.tungsten.api.types.VirtualNetwork; +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.cloudstack.framework.messagebus.MessageBus; +import org.apache.cloudstack.framework.messagebus.MessageSubscriber; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenNetworkGatewayToLogicalRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenNetworkSubnetCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenPolicyRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenSecondaryIpAddressCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenSecurityGroupRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.AddTungstenVmToSecurityGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ApplyTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ApplyTungstenTagCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenAddressGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenApplicationPolicySetCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenDefaultProjectCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenDomainCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenFirewallPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenFirewallRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenFloatingIpCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenFloatingIpPoolCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenNetworkCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenProjectCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenRoutingLogicalRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenSecurityGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenServiceGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenTagCommand; +import org.apache.cloudstack.network.tungsten.agent.api.CreateTungstenTagTypeCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenAddressGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenApplicationPolicySetCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenDomainCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenFirewallPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenFirewallRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenFloatingIpCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenFloatingIpPoolCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenNetworkCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenNetworkPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenProjectCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenRoutingLogicalRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenSecurityGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenServiceGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenTagCommand; +import org.apache.cloudstack.network.tungsten.agent.api.DeleteTungstenTagTypeCommand; +import org.apache.cloudstack.network.tungsten.agent.api.GetTungstenFabricNetworkCommand; +import org.apache.cloudstack.network.tungsten.agent.api.GetTungstenLoadBalancerCommand; +import org.apache.cloudstack.network.tungsten.agent.api.GetTungstenNetworkDnsCommand; +import org.apache.cloudstack.network.tungsten.agent.api.GetTungstenSecurityGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenAddressGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenApplicationPolicySetCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenFirewallPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenFirewallRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenNetworkCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenNicCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenPolicyRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenRoutingLogicalRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenServiceGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenTagCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenTagTypeCommand; +import org.apache.cloudstack.network.tungsten.agent.api.ListTungstenVmCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenNetworkGatewayFromLogicalRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenNetworkSubnetCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenPolicyCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenPolicyRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenSecondaryIpAddressCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenSecurityGroupRuleCommand; +import org.apache.cloudstack.network.tungsten.agent.api.RemoveTungstenTagCommand; +import org.apache.cloudstack.network.tungsten.agent.api.SetupTungstenVRouterCommand; +import org.apache.cloudstack.network.tungsten.agent.api.TungstenAnswer; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateLoadBalancerServiceInstanceCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenDefaultSecurityGroupCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenLoadbalancerSslCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenLoadbalancerStatsCommand; +import org.apache.cloudstack.network.tungsten.agent.api.UpdateTungstenVrouterConfigCommand; +import org.apache.cloudstack.network.tungsten.model.TungstenLogicalRouter; +import org.apache.cloudstack.network.tungsten.model.TungstenNetworkPolicy; +import org.apache.cloudstack.network.tungsten.model.TungstenRule; +import org.apache.cloudstack.network.tungsten.model.TungstenTag; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; + +import java.util.Arrays; +import java.util.List; + +@RunWith(MockitoJUnitRunner.class) +public class TungstenServiceImplTest { + @Mock + MessageBus messageBus; + @Mock + ProjectDao projectDao; + @Mock + AccountDao accountDao; + @Mock + NetworkDao networkDao; + @Mock + ConfigurationDao configDao; + @Mock + IPAddressDao ipAddressDao; + @Mock + EntityManager entityMgr; + @Mock + NetworkModel networkModel; + @Mock + DomainDao domainDao; + @Mock + LoadBalancerCertMapDao lbCertMapDao; + @Mock + FirewallRulesDao fwRulesDao; + @Mock + TungstenGuestNetworkIpAddressDao tungstenGuestNetworkIpAddressDao; + @Mock + TungstenProviderDao tungstenProviderDao; + @Mock + TungstenFabricUtils tungstenFabricUtils; + @Mock + AgentManager agentMgr; + @Mock + HostDao hostDao; + @Mock + NetworkDetailsDao networkDetailsDao; + @Mock + SecurityGroupDao securityGroupDao; + @Mock + NicDao nicDao; + @Mock + TungstenSecurityGroupRuleDao tungstenSecurityGroupRuleDao; + @Mock + SecurityGroupVMMapDao securityGroupVMMapDao; + @Mock + SecurityGroupRuleDao securityGroupRuleDao; + @Mock + SecurityGroupManager securityGroupManager; + @Mock + NicSecondaryIpDao nicSecIpDao; + @Mock + DataCenterIpAddressDao dataCenterIpAddressDao; + @Mock + DataCenterDao dataCenterDao; + @Mock + IpAddressManager ipAddressManager; + + TungstenServiceImpl tungstenService; + + AutoCloseable closeable; + + @Before + public void setup() { + closeable = MockitoAnnotations.openMocks(this); + tungstenService = new TungstenServiceImpl(); + tungstenService.projectDao = projectDao; + tungstenService.tungstenProviderDao = tungstenProviderDao; + tungstenService.networkModel = networkModel; + tungstenService.ipAddressDao = ipAddressDao; + tungstenService.tungstenFabricUtils = tungstenFabricUtils; + tungstenService.domainDao = domainDao; + tungstenService.accountDao = accountDao; + tungstenService.dataCenterIpAddressDao = dataCenterIpAddressDao; + tungstenService.networkDetailsDao = networkDetailsDao; + tungstenService.agentMgr = agentMgr; + tungstenService.hostDao = hostDao; + tungstenService.configDao = configDao; + tungstenService.fwRulesDao = fwRulesDao; + tungstenService.lbCertMapDao = lbCertMapDao; + tungstenService.entityMgr = entityMgr; + tungstenService.tungstenGuestNetworkIpAddressDao = tungstenGuestNetworkIpAddressDao; + tungstenService.securityGroupDao = securityGroupDao; + tungstenService.tungstenSecurityGroupRuleDao = tungstenSecurityGroupRuleDao; + tungstenService.securityGroupVMMapDao = securityGroupVMMapDao; + tungstenService.nicDao = nicDao; + tungstenService.nicSecIpDao = nicSecIpDao; + tungstenService.securityGroupRuleDao = securityGroupRuleDao; + tungstenService.securityGroupManager = securityGroupManager; + tungstenService.networkDao = networkDao; + tungstenService.dataCenterDao = dataCenterDao; + tungstenService.ipAddressManager = ipAddressManager; + tungstenService.messageBus = messageBus; + } + + @After + public void tearDown() throws Exception { + closeable.close(); + } + + @Test + public void createTungstenFloatingIpTest() throws Exception { + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer createTungstenFloatingIpAnswer = new MockTungstenAnswerFactory(true).get(); + Ip ip = mock(Ip.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(networkVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenFloatingIpCommand.class), anyLong())).thenReturn(createTungstenFloatingIpAnswer); + when(ipAddressVO.getAddress()).thenReturn(ip); + + assertTrue(ReflectionTestUtils.invokeMethod(tungstenService, "createTungstenFloatingIp", 1L, ipAddressVO)); + } + + @Test + public void deleteTungstenFloatingIpWithIpAddressTest() throws Exception { + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer deleteTungstenFloatingIpAnswer = new MockTungstenAnswerFactory(true).get(); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(networkVO); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFloatingIpCommand.class), anyLong())).thenReturn(deleteTungstenFloatingIpAnswer); + + assertTrue(ReflectionTestUtils.invokeMethod(tungstenService, "deleteTungstenFloatingIp", 1L, ipAddressVO)); + } + + @Test + public void deleteTungstenDomainTest() throws Exception { + DomainVO domainVO = mock(DomainVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer deleteTungstenDomainAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenDomainCommand.class), anyLong())).thenReturn(deleteTungstenDomainAnswer); + + assertTrue(ReflectionTestUtils.invokeMethod(tungstenService, "deleteTungstenDomain", domainVO)); + } + + @Test + public void deleteTungstenProjectTest() throws Exception { + ProjectVO projectVO = mock(ProjectVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer deleteTungstenProjectAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenProjectCommand.class), anyLong())).thenReturn(deleteTungstenProjectAnswer); + + assertTrue(ReflectionTestUtils.invokeMethod(tungstenService, "deleteTungstenProject", projectVO)); + } + + @Test + public void addTungstenDefaultNetworkPolicyTest() { + TungstenRule tungstenRule = mock(TungstenRule.class); + TungstenAnswer createTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createTungstenPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyTungstenPolicyAnswer); + when(createTungstenPolicyAnswer.getApiObjectBase()).thenReturn(networkPolicy); + + assertTrue(tungstenService.addTungstenDefaultNetworkPolicy(1L, "default-domain:default-project", "policyName", "7279ed91-314e-45be-81b4-b10395fd2ae3" + , List.of(tungstenRule), 1, 1)); + } + + @Test + public void createManagementNetworkTest() { + Network managementNetwork = mock(Network.class); + VirtualNetwork managementVirtualNetwork = mock(VirtualNetwork.class); + VirtualNetwork fabricVirtualNetwork = mock(VirtualNetwork.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + TungstenAnswer createTungstenNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenDefaultSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenGlobalVrouterConfigAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenFabricNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), + eq(Networks.TrafficType.Management))).thenReturn(managementNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkCommand.class), anyLong())).thenReturn(createTungstenNetworkAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenDefaultSecurityGroupCommand.class), anyLong())).thenReturn(updateTungstenDefaultSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenVrouterConfigCommand.class), anyLong())).thenReturn(updateTungstenGlobalVrouterConfigAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenFabricNetworkCommand.class), anyLong())).thenReturn(getTungstenFabricNetworkAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createTungstenPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyTungstenPolicyAnswer); + + when(createTungstenNetworkAnswer.getApiObjectBase()).thenReturn(managementVirtualNetwork); + when(createTungstenPolicyAnswer.getApiObjectBase()).thenReturn(networkPolicy); + when(getTungstenFabricNetworkAnswer.getApiObjectBase()).thenReturn(fabricVirtualNetwork); + + assertTrue(tungstenService.createManagementNetwork(1L)); + } + + @Test + public void addManagementNetworkSubnetTest() { + HostPodVO hostPodVO = mock(HostPodVO.class); + Network managementNetwork = mock(Network.class); + TungstenAnswer addTungstenNetworkSubnetAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + DataCenterIpAddressVO dataCenterIpAddressVO = mock(DataCenterIpAddressVO.class); + + when(hostPodVO.getDescription()).thenReturn("192.168.100.100-192.168.100.200"); + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), + eq(Networks.TrafficType.Management))).thenReturn(managementNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenNetworkSubnetCommand.class), anyLong())).thenReturn(addTungstenNetworkSubnetAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.150"); + when(managementNetwork.getCidr()).thenReturn("192.168.100.0/24"); + when(managementNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(dataCenterIpAddressDao.listByPodIdDcIdIpAddress(anyLong(), anyLong(), anyString())).thenReturn(List.of(dataCenterIpAddressVO)); + when(dataCenterIpAddressDao.mark(anyLong(), anyLong(), anyString())).thenReturn(true); + + assertTrue(tungstenService.addManagementNetworkSubnet(hostPodVO)); + } + + @Test + public void deleteManagementNetworkTest() { + Network managementNetwork = mock(Network.class); + TungstenAnswer deleteTungstenManagementPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenFabricNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenFabricPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + VirtualNetwork fabricVirtualNetwork = mock(VirtualNetwork.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), + eq(Networks.TrafficType.Management))).thenReturn(managementNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenFabricNetworkCommand.class), anyLong())).thenReturn(getTungstenFabricNetworkAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(deleteTungstenFabricPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkCommand.class), anyLong())).thenReturn(deleteTungstenNetworkAnswer); + when(getTungstenFabricNetworkAnswer.getApiObjectBase()).thenReturn(fabricVirtualNetwork); + + assertTrue(tungstenService.deleteManagementNetwork(1L)); + } + + @Test + public void removeManagementNetworkSubnetTest() { + HostPodVO hostPodVO = mock(HostPodVO.class); + Network managementNetwork = mock(Network.class); + TungstenAnswer removeTungstenNetworkSubnetAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + DataCenterIpAddressVO dataCenterIpAddressVO = mock(DataCenterIpAddressVO.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), + eq(Networks.TrafficType.Management))).thenReturn(managementNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenNetworkSubnetCommand.class), anyLong())).thenReturn(removeTungstenNetworkSubnetAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.150"); + when(managementNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Management); + when(dataCenterIpAddressDao.listByPodIdDcIdIpAddress(anyLong(), anyLong(), anyString())).thenReturn(List.of(dataCenterIpAddressVO)); + + assertTrue(tungstenService.removeManagementNetworkSubnet(hostPodVO)); + verify(dataCenterIpAddressDao, times(1)).releasePodIpAddress(anyLong()); + } + + @Test + public void createPublicNetworkTest() { + Network publicNetwork = mock(Network.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + NetworkDetailVO networkDetailVO = mock(NetworkDetailVO.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + TungstenAnswer createPublicNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createFloatingIpPoolAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkCommand.class), anyLong())).thenReturn(createPublicNetworkAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenFloatingIpPoolCommand.class), anyLong())).thenReturn(createFloatingIpPoolAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createTungstenPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyTungstenPolicyAnswer); + when(createPublicNetworkAnswer.getApiObjectBase()).thenReturn(virtualNetwork); + when(createTungstenPolicyAnswer.getApiObjectBase()).thenReturn(networkPolicy); + when(networkDetailsDao.persist(any(NetworkDetailVO.class))).thenReturn(networkDetailVO); + when(virtualNetwork.getQualifiedName()).thenReturn(Arrays.asList("default-domain", "default-project", "publicNetwork")); + + assertTrue(tungstenService.createPublicNetwork(1L)); + } + + @Test + public void addPublicNetworkSubnetTest() { + VlanVO vlanVO = mock(VlanVO.class); + Network publicNetwork = mock(Network.class); + TungstenAnswer addTungstenNetworkSubnetAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer applyTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenNetworkSubnetCommand.class), anyLong())).thenReturn(addTungstenNetworkSubnetAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(createTungstenPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyTungstenPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.150"); + when(vlanVO.getIpRange()).thenReturn("192.168.100.100-192.168.100.200"); + when(vlanVO.getVlanGateway()).thenReturn("192.168.100.1"); + when(vlanVO.getVlanNetmask()).thenReturn("255.255.255.0"); + when(publicNetwork.getCidr()).thenReturn("192.168.100.0/24"); + when(publicNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Public); + when(createTungstenPolicyAnswer.getApiObjectBase()).thenReturn(networkPolicy); + when(ipAddressDao.mark(anyLong(), any(Ip.class))).thenReturn(true); + + assertTrue(tungstenService.addPublicNetworkSubnet(vlanVO)); + } + + @Test + public void deletePublicNetworkTest() { + Network publicNetwork = mock(Network.class); + TungstenAnswer deleteTungstenNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenFloatingIpPoolAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer deleteTungstenNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(deleteTungstenNetworkPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFloatingIpPoolCommand.class), anyLong())).thenReturn(deleteTungstenFloatingIpPoolAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkCommand.class), anyLong())).thenReturn(deleteTungstenNetworkAnswer); + + assertTrue(tungstenService.deletePublicNetwork(1L)); + } + + @Test + public void removePublicNetworkSubnetTest() { + VlanVO vlanVO = mock(VlanVO.class); + Network publicNetwork = mock(Network.class); + TungstenAnswer deleteTungstenNetworkPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer removeTungstenNetworkSubnetAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(deleteTungstenNetworkPolicyAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenNetworkSubnetCommand.class), anyLong())).thenReturn(removeTungstenNetworkSubnetAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.150"); + when(publicNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Public); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + + assertTrue(tungstenService.removePublicNetworkSubnet(vlanVO)); + verify(ipAddressDao, times(1)).unassignIpAddress(anyLong()); + } + + @Test + public void allocateDnsIpAddressTest() { + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(networkVO.getTrafficType()).thenReturn(Networks.TrafficType.Guest); + when(networkVO.getGuestType()).thenReturn(Network.GuestType.Isolated); + when(tungstenGuestNetworkIpAddressDao.persist(any(TungstenGuestNetworkIpAddressVO.class))).thenReturn(tungstenGuestNetworkIpAddressVO); + when(networkVO.getCidr()).thenReturn("192.168.100.0/24"); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.100"); + + assertTrue(tungstenService.allocateDnsIpAddress(networkVO, null, "test")); + } + + @Test + public void deallocateDnsIpAddressTest() { + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(networkVO.getTrafficType()).thenReturn(Networks.TrafficType.Guest); + when(networkVO.getGuestType()).thenReturn(Network.GuestType.Isolated); + when(tungstenGuestNetworkIpAddressDao.findByNetworkAndGuestIpAddress(anyLong(), anyString())).thenReturn(tungstenGuestNetworkIpAddressVO); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.100.100"); + + tungstenService.deallocateDnsIpAddress(networkVO, null, "test"); + verify(tungstenGuestNetworkIpAddressDao, times(1)).expunge(anyLong()); + } + + @Test + public void subscribeTungstenEventTest() { + tungstenService.subscribeTungstenEvent(); + + verify(messageBus, times(1)).subscribe(eq(IpAddressManager.MESSAGE_ASSIGN_IPADDR_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(IpAddressManager.MESSAGE_RELEASE_IPADDR_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(TungstenService.MESSAGE_APPLY_NETWORK_POLICY_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ConfigurationManager.MESSAGE_CREATE_VLAN_IP_RANGE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ConfigurationManager.MESSAGE_DELETE_VLAN_IP_RANGE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ConfigurationManager.MESSAGE_CREATE_POD_IP_RANGE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ConfigurationManager.MESSAGE_DELETE_POD_IP_RANGE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(DomainManager.MESSAGE_CREATE_TUNGSTEN_DOMAIN_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(DomainManager.MESSAGE_DELETE_TUNGSTEN_DOMAIN_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ProjectManager.MESSAGE_CREATE_TUNGSTEN_PROJECT_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(ProjectManager.MESSAGE_DELETE_TUNGSTEN_PROJECT_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(TungstenService.MESSAGE_SYNC_TUNGSTEN_DB_WITH_DOMAINS_AND_PROJECTS_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(SecurityGroupService.MESSAGE_CREATE_TUNGSTEN_SECURITY_GROUP_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(SecurityGroupService.MESSAGE_DELETE_TUNGSTEN_SECURITY_GROUP_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(SecurityGroupService.MESSAGE_ADD_SECURITY_GROUP_RULE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(SecurityGroupService.MESSAGE_REMOVE_SECURITY_GROUP_RULE_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(NetworkService.MESSAGE_ASSIGN_NIC_SECONDARY_IP_EVENT), any(MessageSubscriber.class)); + verify(messageBus, times(1)).subscribe(eq(NetworkService.MESSAGE_RELEASE_NIC_SECONDARY_IP_EVENT), any(MessageSubscriber.class)); + } + + @Test + public void syncTungstenDbWithCloudstackProjectsAndDomainsTest() { + DomainVO domainVO = mock(DomainVO.class); + ProjectVO projectVO = mock(ProjectVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer createTungstenDomainAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createTungstenProjectAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer createTungstenDefaultProjectAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateTungstenDefaultSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + + when(domainDao.listAll()).thenReturn(List.of(domainVO)); + when(projectDao.listAll()).thenReturn(List.of(projectVO)); + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenDomainCommand.class), anyLong())).thenReturn(createTungstenDomainAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenProjectCommand.class), anyLong())).thenReturn(createTungstenProjectAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateTungstenDefaultSecurityGroupCommand.class), anyLong())).thenReturn(updateTungstenDefaultSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenDefaultProjectCommand.class), anyLong())).thenReturn(createTungstenDefaultProjectAnswer); + when(domainDao.findById(anyLong())).thenReturn(domainVO); + + assertTrue(tungstenService.syncTungstenDbWithCloudstackProjectsAndDomains()); + } + + @Test + public void updateLoadBalancerTest() { + Network network = mock(Network.class); + LoadBalancingRule loadBalancingRule = mock(LoadBalancingRule.class); + Network publicNetwork = mock(Network.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + HostVO hostVO = mock(HostVO.class); + TungstenAnswer getTungstenLoadBalancerAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer updateLoadBalancerServiceInstanceAnswer = new MockTungstenAnswerFactory(true).get(); + Answer updateTungstenLoadbalancerStatsAnswer = mock(Answer.class); + Answer updateTungstenLoadbalancerSslAnswer = mock(Answer.class); + FirewallRuleVO firewallRuleVO = mock(FirewallRuleVO.class); + LoadBalancerCertMapVO loadBalancerCertMapVO = mock(LoadBalancerCertMapVO.class); + SslCertVO sslCertVO = mock(SslCertVO.class); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + Ip ip = mock(Ip.class); + AccountVO accountVO = mock(AccountVO.class); + Loadbalancer loadbalancer = mock(Loadbalancer.class); + + when(networkModel.getSystemNetworkByZoneAndTrafficType(anyLong(), eq(Networks.TrafficType.Public))).thenReturn(publicNetwork); + when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddressVO); + when(hostDao.listAllHostsByZoneAndHypervisorType(anyLong(), eq(Hypervisor.HypervisorType.KVM))).thenReturn(List.of(hostVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenLoadBalancerCommand.class), anyLong())).thenReturn(getTungstenLoadBalancerAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(UpdateLoadBalancerServiceInstanceCommand.class), anyLong())).thenReturn(updateLoadBalancerServiceInstanceAnswer); + when(agentMgr.easySend(anyLong(), any(UpdateTungstenLoadbalancerStatsCommand.class))).thenReturn(updateTungstenLoadbalancerStatsAnswer); + when(agentMgr.easySend(anyLong(), any(UpdateTungstenLoadbalancerSslCommand.class))).thenReturn(updateTungstenLoadbalancerSslAnswer); + when(updateTungstenLoadbalancerStatsAnswer.getResult()).thenReturn(true); + when(updateTungstenLoadbalancerSslAnswer.getResult()).thenReturn(true); + when(configDao.getValue(Config.NetworkLBHaproxyStatsVisbility.key())).thenReturn("enabled"); + when(fwRulesDao.listByIpAndPurposeAndNotRevoked(anyLong(), eq(FirewallRule.Purpose.LoadBalancing))).thenReturn(List.of(firewallRuleVO)); + when(lbCertMapDao.findByLbRuleId(anyLong())).thenReturn(loadBalancerCertMapVO); + when(entityMgr.findById(eq(SslCertVO.class), anyLong())).thenReturn(sslCertVO); + when(tungstenGuestNetworkIpAddressDao.findByNetworkIdAndPublicIp(anyLong(), anyString())).thenReturn(tungstenGuestNetworkIpAddressVO); + when(loadBalancingRule.getSourceIp()).thenReturn(ip); + when(accountDao.findById(anyLong())).thenReturn(accountVO); + when(ip.addr()).thenReturn("192.168.100.100"); + when(getTungstenLoadBalancerAnswer.getApiObjectBase()).thenReturn(loadbalancer); + when(ipAddressVO.getAddress()).thenReturn(ip); + when(tungstenGuestNetworkIpAddressVO.getGuestIpAddress()).thenReturn(ip); + + assertTrue(tungstenService.updateLoadBalancer(network, loadBalancingRule)); + } + + @Test + public void createTungstenSecurityGroupTest() { + SecurityGroup securityGroup = mock(SecurityGroup.class); + ProjectVO projectVO = mock(ProjectVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + DomainVO domainVO = mock(DomainVO.class); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + TungstenAnswer createTungstenSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + + when(projectDao.findByProjectAccountId(anyLong())).thenReturn(projectVO); + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(domainDao.findById(anyLong())).thenReturn(domainVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenSecurityGroupCommand.class), anyLong())).thenReturn(createTungstenSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + + try (MockedStatic transactionMocked = Mockito.mockStatic(Transaction.class)) { + transactionMocked.when(() -> Transaction.execute(any(TransactionCallback.class))).thenReturn(List.of(tungstenSecurityGroupRuleVO)); + assertTrue(tungstenService.createTungstenSecurityGroup(securityGroup)); + } + } + + @Test + public void deleteTungstenSecurityGroupTest() { + SecurityGroup securityGroup = mock(SecurityGroup.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer deleteTungstenSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenSecurityGroupCommand.class), anyLong())).thenReturn(deleteTungstenSecurityGroupAnswer); + + assertTrue(tungstenService.deleteTungstenSecurityGroup(securityGroup)); + } + + @Test + public void addTungstenSecurityGroupEgressRuleTest() { + SecurityRule securityRule = mock(SecurityRule.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenAnswer getTungstenSecurityGroupAnswer = mock(TungstenAnswer.class); + TungstenAnswer removeTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + net.juniper.tungsten.api.types.SecurityGroup securityGroup = mock(net.juniper.tungsten.api.types.SecurityGroup.class); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + NicVO nicVO = mock(NicVO.class); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(securityGroupDao.findById(anyLong())).thenReturn(securityGroupVO); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenSecurityGroupCommand.class), anyLong())).thenReturn(getTungstenSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(removeTungstenSecurityGroupRuleAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + when(getTungstenSecurityGroupAnswer.getApiObjectBase()).thenReturn(securityGroup); + when(securityRule.getRuleType()).thenReturn(SecurityRule.SecurityRuleType.EgressRule); + when(tungstenSecurityGroupRuleDao.findDefaultSecurityRule(anyLong(), anyString(), anyString())).thenReturn(tungstenSecurityGroupRuleVO); + when(securityRule.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(securityRule.getEndPort()).thenReturn(NetUtils.PORT_RANGE_MIN); + when(securityGroupVMMapDao.listVmIdsBySecurityGroup(anyLong())).thenReturn(List.of(1L)); + when(nicDao.findDefaultNicForVM(anyLong())).thenReturn(nicVO); + when(nicVO.getIPv4Address()).thenReturn("192.168.100.100"); + when(nicVO.getIPv6Address()).thenReturn("fd00::1"); + when(nicVO.getSecondaryIp()).thenReturn(true); + when(nicSecIpDao.getSecondaryIpAddressesForNic(anyLong())).thenReturn(List.of("192.168.100.200")); + when(tungstenSecurityGroupRuleDao.persist(any(TungstenSecurityGroupRuleVO.class))).thenReturn(tungstenSecurityGroupRuleVO); + when(securityRule.getAllowedNetworkId()).thenReturn(1L); + + assertTrue(tungstenService.addTungstenSecurityGroupRule(List.of(securityRule))); + } + + @Test + public void addTungstenSecurityGroupIngressRuleTest() { + SecurityRule securityRule = mock(SecurityRule.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenAnswer getTungstenSecurityGroupAnswer = mock(TungstenAnswer.class); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + net.juniper.tungsten.api.types.SecurityGroup securityGroup = mock(net.juniper.tungsten.api.types.SecurityGroup.class); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(securityGroupDao.findById(anyLong())).thenReturn(securityGroupVO); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenSecurityGroupCommand.class), anyLong())).thenReturn(getTungstenSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + when(getTungstenSecurityGroupAnswer.getApiObjectBase()).thenReturn(securityGroup); + when(securityRule.getRuleType()).thenReturn(SecurityRule.SecurityRuleType.IngressRule); + when(securityRule.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(securityRule.getEndPort()).thenReturn(NetUtils.PORT_RANGE_MIN); + when(securityRule.getAllowedNetworkId()).thenReturn(null); + + assertTrue(tungstenService.addTungstenSecurityGroupRule(List.of(securityRule))); + } + + @Test + public void removeTungstenSecurityGroupEgressRuleTest() { + SecurityRule securityRule = mock(SecurityRule.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer removeTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + NicVO nicVO = mock(NicVO.class); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(securityGroupDao.findById(anyLong())).thenReturn(securityGroupVO); + when(securityRule.getRuleType()).thenReturn(SecurityRule.SecurityRuleType.EgressRule); + when(securityRule.getType()).thenReturn("egress"); + when(tungstenSecurityGroupRuleDao.persist(any(TungstenSecurityGroupRuleVO.class))).thenReturn(tungstenSecurityGroupRuleVO); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(removeTungstenSecurityGroupRuleAnswer); + when(securityRule.getAllowedNetworkId()).thenReturn(1L); + when(securityGroupVMMapDao.listVmIdsBySecurityGroup(anyLong())).thenReturn(List.of(1L)); + when(nicDao.findDefaultNicForVM(anyLong())).thenReturn(nicVO); + when(tungstenSecurityGroupRuleDao.expunge(anyLong())).thenReturn(true); + when(nicVO.getIPv4Address()).thenReturn("192.168.100.100"); + when(nicVO.getIPv6Address()).thenReturn("fd00::1"); + when(nicVO.getSecondaryIp()).thenReturn(true); + when(nicSecIpDao.getSecondaryIpAddressesForNic(anyLong())).thenReturn(List.of("192.168.100.200")); + when(tungstenSecurityGroupRuleDao.findBySecurityGroupAndRuleTypeAndRuleTarget(anyLong(), anyString(), anyString())).thenReturn(tungstenSecurityGroupRuleVO); + + assertTrue(tungstenService.removeTungstenSecurityGroupRule(securityRule)); + } + + @Test + public void removeTungstenSecurityGroupIngressRuleTest() { + SecurityRule securityRule = mock(SecurityRule.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer removeTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(securityGroupDao.findById(anyLong())).thenReturn(securityGroupVO); + when(securityRule.getRuleType()).thenReturn(SecurityRule.SecurityRuleType.IngressRule); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(removeTungstenSecurityGroupRuleAnswer); + when(securityRule.getAllowedNetworkId()).thenReturn(null); + + assertTrue(tungstenService.removeTungstenSecurityGroupRule(securityRule)); + } + + @Test + public void addTungstenNicSecondaryIpAddressTest() { + NicSecondaryIp nicSecondaryIp = mock(NicSecondaryIp.class); + Network network = mock(Network.class); + DataCenter dataCenter = mock(DataCenter.class); + Nic nic = mock(Nic.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + SecurityGroupRuleVO securityGroupRuleVO = mock(SecurityGroupRuleVO.class); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer addTungstenSecondaryIpAddressAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + + when(entityMgr.findById(eq(NicSecondaryIp.class), anyLong())).thenReturn(nicSecondaryIp); + when(entityMgr.findById(eq(Network.class), anyLong())).thenReturn(network); + when(entityMgr.findById(eq(DataCenter.class), anyLong())).thenReturn(dataCenter); + when(entityMgr.findById(eq(Nic.class), anyLong())).thenReturn(nic); + when(nicSecondaryIp.getIp4Address()).thenReturn("192.168.100.100"); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecondaryIpAddressCommand.class), anyLong())).thenReturn(addTungstenSecondaryIpAddressAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + when(dataCenter.isSecurityGroupEnabled()).thenReturn(true); + when(network.getGuestType()).thenReturn(Network.GuestType.Shared); + when(securityGroupManager.getSecurityGroupsForVm(anyLong())).thenReturn(List.of(securityGroupVO)); + when(securityGroupRuleDao.listByAllowedSecurityGroupId(anyLong())).thenReturn(List.of(securityGroupRuleVO)); + when(tungstenSecurityGroupRuleDao.persist(any(TungstenSecurityGroupRuleVO.class))).thenReturn(tungstenSecurityGroupRuleVO); + when(securityGroupRuleVO.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + + assertTrue(tungstenService.addTungstenNicSecondaryIpAddress(1L)); + } + + @Test + public void removeTungstenNicSecondaryIpAddressTest() { + NicSecondaryIpVO nicSecondaryIpVO = mock(NicSecondaryIpVO.class); + Network network = mock(Network.class); + DataCenter dataCenter = mock(DataCenter.class); + TungstenAnswer removeTungstenSecondaryIpAddressAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenAnswer removeTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + + when(entityMgr.findById(eq(Network.class), anyLong())).thenReturn(network); + when(entityMgr.findById(eq(DataCenter.class), anyLong())).thenReturn(dataCenter); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenSecondaryIpAddressCommand.class), anyLong())).thenReturn(removeTungstenSecondaryIpAddressAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(removeTungstenSecurityGroupRuleAnswer); + + when(dataCenter.isSecurityGroupEnabled()).thenReturn(true); + when(network.getGuestType()).thenReturn(Network.GuestType.Shared); + when(tungstenSecurityGroupRuleDao.listByRuleTarget(anyString())).thenReturn(List.of(tungstenSecurityGroupRuleVO)); + when(securityGroupDao.findById(anyLong())).thenReturn(securityGroupVO); + when(tungstenSecurityGroupRuleDao.expunge(anyLong())).thenReturn(true); + when(nicSecondaryIpVO.getIp4Address()).thenReturn("192.168.100.100"); + + assertTrue(tungstenService.removeTungstenNicSecondaryIpAddress(nicSecondaryIpVO)); + } + + @Test + public void createTungstenPolicyTest() { + TungstenAnswer createTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenNetworkPolicy tungstenNetworkPolicy = mock(TungstenNetworkPolicy.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenPolicyCommand.class), anyLong())).thenReturn(createTungstenPolicyAnswer); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(createTungstenPolicyAnswer.getTungstenModel()).thenReturn(tungstenNetworkPolicy); + when(tungstenNetworkPolicy.getNetworkPolicy()).thenReturn(networkPolicy); + + assertNotNull(tungstenService.createTungstenPolicy(1L, "test")); + } + + @Test + public void addTungstenPolicyRuleTest() throws Exception { + TungstenAnswer addTungstenPolicyRuleAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + PolicyEntriesType policyEntriesType = mock(PolicyEntriesType.class); + PolicyRuleType policyRuleType = mock(PolicyRuleType.class); + ActionListType actionListType = mock(ActionListType.class); + AddressType addressType = mock(AddressType.class); + SubnetType subnetType = mock(SubnetType.class); + PortType portType = mock(PortType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenPolicyRuleCommand.class), anyLong())).thenReturn(addTungstenPolicyRuleAnswer); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(addTungstenPolicyRuleAnswer.getApiObjectBase()).thenReturn(networkPolicy); + when(networkPolicy.getEntries()).thenReturn(policyEntriesType); + when(policyEntriesType.getPolicyRule()).thenReturn(List.of(policyRuleType)); + + when(policyRuleType.getRuleUuid()).thenReturn("8b4637b6-5629-46de-8fb2-d0b0502bfa85"); + when(policyRuleType.getActionList()).thenReturn(actionListType); + when(actionListType.getSimpleAction()).thenReturn("pass"); + when(policyRuleType.getSrcAddresses()).thenReturn(List.of(addressType)); + when(addressType.getSubnet()).thenReturn(subnetType); + when(policyRuleType.getSrcPorts()).thenReturn(List.of(portType)); + when(policyRuleType.getDstAddresses()).thenReturn(List.of(addressType)); + when(policyRuleType.getDstPorts()).thenReturn(List.of(portType)); + + try (MockedConstruction ignored = + Mockito.mockConstruction(AddTungstenPolicyRuleCommand.class, (mock, context) -> { + when(mock.getUuid()).thenReturn("8b4637b6-5629-46de-8fb2-d0b0502bfa85"); + })) { + + assertNotNull(tungstenService.addTungstenPolicyRule(1L, "948f421c-edde-4518-a391-09299cc25dc2", "pass", + "<>", "tcp", "network1", "192.168.100.100", 32, 80, 80, + "network2", "192.168.200.200", 32, 80, 80)); + } + } + + @Test + public void listTungstenPolicyTest() { + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer listTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenNetworkPolicy tungstenNetworkPolicy = mock(TungstenNetworkPolicy.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(networkDao.findById(anyLong())).thenReturn(networkVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenPolicyCommand.class), anyLong())).thenReturn(listTungstenPolicyAnswer); + when(listTungstenPolicyAnswer.getTungstenModelList()).thenReturn(List.of(tungstenNetworkPolicy)); + when(tungstenNetworkPolicy.getNetworkPolicy()).thenReturn(networkPolicy); + when(tungstenNetworkPolicy.getVirtualNetworkList()).thenReturn( + List.of(virtualNetwork)); + + assertNotNull(tungstenService.listTungstenPolicy(1L, 2L, 3L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void listTungstenNetworkTest() { + TungstenAnswer listTungstenNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenNetworkCommand.class), anyLong())).thenReturn(listTungstenNetworkAnswer); + doReturn(List.of(virtualNetwork)).when(listTungstenNetworkAnswer).getApiObjectBaseList(); + when((virtualNetwork.getName())).thenReturn("guestNetwork1"); + + assertNotNull(tungstenService.listTungstenNetwork(1L, "948f421c-edde-4518-a391-09299cc25dc2", false)); + } + + @Test + public void listTungstenNicTest() { + TungstenAnswer listTungstenNicAnswer = new MockTungstenAnswerFactory(true).get(); + VirtualMachineInterface virtualMachineInterface = mock(VirtualMachineInterface.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenNicCommand.class), anyLong())).thenReturn(listTungstenNicAnswer); + doReturn(List.of(virtualMachineInterface)).when(listTungstenNicAnswer).getApiObjectBaseList(); + + assertNotNull(tungstenService.listTungstenNic(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void listTungstenVmTest() { + TungstenAnswer listTungstenVmAnswer = new MockTungstenAnswerFactory(true).get(); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenVmCommand.class), anyLong())).thenReturn(listTungstenVmAnswer); + doReturn(List.of(virtualMachine)).when(listTungstenVmAnswer).getApiObjectBaseList(); + + assertNotNull(tungstenService.listTungstenVm(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenPolicyTest() { + TungstenAnswer deleteTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenPolicyCommand.class), anyLong())).thenReturn(deleteTungstenPolicyAnswer); + + assertTrue(tungstenService.deleteTungstenPolicy(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void listTungstenPolicyRuleWithRuleUuidTest() { + TungstenAnswer listTungstenPolicyRuleAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + PolicyEntriesType policyEntriesType = mock(PolicyEntriesType.class); + PolicyRuleType policyRuleType = mock(PolicyRuleType.class); + ActionListType actionListType = mock(ActionListType.class); + AddressType addressType = mock(AddressType.class); + SubnetType subnetType = mock(SubnetType.class); + PortType portType = mock(PortType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenPolicyRuleCommand.class), anyLong())).thenReturn(listTungstenPolicyRuleAnswer); + doReturn(networkPolicy).when(listTungstenPolicyRuleAnswer).getApiObjectBase(); + when(networkPolicy.getEntries()).thenReturn(policyEntriesType); + when(policyEntriesType.getPolicyRule()).thenReturn(List.of(policyRuleType)); + when(policyRuleType.getRuleUuid()).thenReturn("8b4637b6-5629-46de-8fb2-d0b0502bfa85"); + when(policyRuleType.getActionList()).thenReturn(actionListType); + when(actionListType.getSimpleAction()).thenReturn("pass"); + when(policyRuleType.getSrcAddresses()).thenReturn(List.of(addressType)); + when(addressType.getSubnet()).thenReturn(subnetType); + when(policyRuleType.getSrcPorts()).thenReturn(List.of(portType)); + when(policyRuleType.getDstAddresses()).thenReturn(List.of(addressType)); + when(policyRuleType.getDstPorts()).thenReturn(List.of(portType)); + + assertNotNull(tungstenService.listTungstenPolicyRule(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void listTungstenPolicyRuleWithAllRuleTest() { + TungstenAnswer listTungstenPolicyRuleAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + PolicyEntriesType policyEntriesType = mock(PolicyEntriesType.class); + PolicyRuleType policyRuleType = mock(PolicyRuleType.class); + ActionListType actionListType = mock(ActionListType.class); + AddressType addressType = mock(AddressType.class); + SubnetType subnetType = mock(SubnetType.class); + PortType portType = mock(PortType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenPolicyRuleCommand.class), anyLong())).thenReturn(listTungstenPolicyRuleAnswer); + doReturn(networkPolicy).when(listTungstenPolicyRuleAnswer).getApiObjectBase(); + when(networkPolicy.getEntries()).thenReturn(policyEntriesType); + when(policyEntriesType.getPolicyRule()).thenReturn(List.of(policyRuleType)); + when(policyRuleType.getRuleUuid()).thenReturn("8b4637b6-5629-46de-8fb2-d0b0502bfa85"); + when(policyRuleType.getActionList()).thenReturn(actionListType); + when(actionListType.getSimpleAction()).thenReturn("pass"); + when(policyRuleType.getSrcAddresses()).thenReturn(List.of(addressType)); + when(addressType.getSubnet()).thenReturn(subnetType); + when(policyRuleType.getSrcPorts()).thenReturn(List.of(portType)); + when(policyRuleType.getDstAddresses()).thenReturn(List.of(addressType)); + when(policyRuleType.getDstPorts()).thenReturn(List.of(portType)); + + assertNotNull(tungstenService.listTungstenPolicyRule(1L, "948f421c-edde-4518-a391-09299cc25dc2", null)); + } + + @Test + public void removeTungstenPolicyRuleTest() { + TungstenAnswer removeTungstenPolicyRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenNetworkPolicy tungstenNetworkPolicy = mock(TungstenNetworkPolicy.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenPolicyRuleCommand.class), anyLong())).thenReturn(removeTungstenPolicyRuleAnswer); + when(removeTungstenPolicyRuleAnswer.getTungstenModel()).thenReturn(tungstenNetworkPolicy); + when(tungstenNetworkPolicy.getNetworkPolicy()).thenReturn(networkPolicy); + when(tungstenNetworkPolicy.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.removeTungstenPolicyRule(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void createTungstenTagTest() { + TungstenAnswer createTungstenTagAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenTag tungstenTag = mock(TungstenTag.class); + Tag tag = mock(Tag.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + VirtualMachineInterface virtualMachineInterface = mock(VirtualMachineInterface.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenTagCommand.class), anyLong())).thenReturn(createTungstenTagAnswer); + when(createTungstenTagAnswer.getTungstenModel()).thenReturn(tungstenTag); + when(tungstenTag.getTag()).thenReturn(tag); + doReturn(List.of(virtualNetwork)).when(tungstenTag).getVirtualNetworkList(); + doReturn(List.of(virtualMachine)).when(tungstenTag).getVirtualMachineList(); + doReturn(List.of(virtualMachineInterface)).when(tungstenTag).getVirtualMachineInterfaceList(); + doReturn(List.of(networkPolicy)).when(tungstenTag).getNetworkPolicyList(); + + assertNotNull(tungstenService.createTungstenTag(1L, "testTag", "testTagType")); + } + + @Test + public void createTungstenTagTypeTest() { + TungstenAnswer createTungstenTagTypeAnswer = new MockTungstenAnswerFactory(true).get(); + TagType tagtype = mock(TagType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenTagTypeCommand.class), anyLong())).thenReturn(createTungstenTagTypeAnswer); + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(createTungstenTagTypeAnswer.getApiObjectBase()).thenReturn(tagtype); + + assertNotNull(tungstenService.createTungstenTagType(1L, "testTagType")); + } + + @Test + public void listTungstenTagsTest() { + TungstenAnswer listTungstenTagAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenTag tungstenTag = mock(TungstenTag.class); + Tag tag = mock(Tag.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + VirtualMachineInterface virtualMachineInterface = mock(VirtualMachineInterface.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + ApplicationPolicySet applicationPolicySet = mock(ApplicationPolicySet.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenTagCommand.class), anyLong())).thenReturn(listTungstenTagAnswer); + when(listTungstenTagAnswer.getTungstenModelList()).thenReturn(List.of(tungstenTag)); + when(tungstenTag.getTag()).thenReturn(tag); + doReturn(List.of(virtualNetwork)).when(tungstenTag).getVirtualNetworkList(); + doReturn(List.of(virtualMachine)).when(tungstenTag).getVirtualMachineList(); + doReturn(List.of(virtualMachineInterface)).when(tungstenTag).getVirtualMachineInterfaceList(); + doReturn(List.of(networkPolicy)).when(tungstenTag).getNetworkPolicyList(); + + assertNotNull(tungstenService.listTungstenTags(1L, "948f421c-edde-4518-a391-09299cc25dc2" + , "8b4637b6-5629-46de-8fb2-d0b0502bfa85", "8d097a79-a38d-4db4-8a41-16f15d9c5afa", "a329662e-1805-4a89-9b05-2b818ea35978", + "d5e3f5c5-97ed-41b6-9b6f-7f696b9eddeb", "f5ba12c8-d4c5-4c20-a57d-67a9b6fca652")); + } + + @Test + public void listTungstenTagTypesTest() { + TungstenAnswer listTungstenTagTypeAnswer = new MockTungstenAnswerFactory(true).get(); + TagType tagtype = mock(TagType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenTagTypeCommand.class), anyLong())).thenReturn(listTungstenTagTypeAnswer); + doReturn(List.of(tagtype)).when(listTungstenTagTypeAnswer).getApiObjectBaseList(); + + assertNotNull(tungstenService.listTungstenTagTypes(1L, "testTagType")); + } + + @Test + public void deleteTungstenTagTest() { + TungstenAnswer deleteTungstenTagAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenTagCommand.class), anyLong())).thenReturn(deleteTungstenTagAnswer); + + assertTrue(tungstenService.deleteTungstenTag(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenTagTypeTest() { + TungstenAnswer deleteTungstenTagTypeAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenTagTypeCommand.class), anyLong())).thenReturn(deleteTungstenTagTypeAnswer); + + assertTrue(tungstenService.deleteTungstenTagType(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void applyTungstenPolicyTest() { + TungstenAnswer applyTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenNetworkPolicy tungstenNetworkPolicy = mock(TungstenNetworkPolicy.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenNetworkPolicyCommand.class), anyLong())).thenReturn(applyTungstenPolicyAnswer); + when(applyTungstenPolicyAnswer.getTungstenModel()).thenReturn(tungstenNetworkPolicy); + when(tungstenNetworkPolicy.getNetworkPolicy()).thenReturn(networkPolicy); + when(tungstenNetworkPolicy.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.applyTungstenPolicy(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85", 1, 1)); + } + + @Test + public void applyTungstenTagTest() { + TungstenAnswer applyTungstenTagAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenTag tungstenTag = mock(TungstenTag.class); + Tag tag = mock(Tag.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + VirtualMachineInterface virtualMachineInterface = mock(VirtualMachineInterface.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ApplyTungstenTagCommand.class), anyLong())).thenReturn(applyTungstenTagAnswer); + when(applyTungstenTagAnswer.getTungstenModel()).thenReturn(tungstenTag); + when(tungstenTag.getTag()).thenReturn(tag); + when(tungstenTag.getNetworkPolicyList()).thenReturn(List.of(networkPolicy)); + when(tungstenTag.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + when(tungstenTag.getVirtualMachineList()).thenReturn(List.of(virtualMachine)); + when(tungstenTag.getVirtualMachineInterfaceList()).thenReturn(List.of(virtualMachineInterface)); + + assertNotNull(tungstenService.applyTungstenTag(1L, List.of("948f421c-edde-4518-a391-09299cc25dc2"), List.of("8b4637b6-5629-46de-8fb2-d0b0502bfa85") + , List.of("8d097a79-a38d-4db4-8a41-16f15d9c5afa"), "a329662e-1805-4a89-9b05-2b818ea35978", "d5e3f5c5-97ed-41b6-9b6f-7f696b9eddeb" + , "f5ba12c8-d4c5-4c20-a57d-67a9b6fca652")); + } + + @Test + public void removeTungstenPolicyTest() { + TungstenAnswer removeTungstenPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenNetworkPolicy tungstenNetworkPolicy = mock(TungstenNetworkPolicy.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenPolicyCommand.class), anyLong())).thenReturn(removeTungstenPolicyAnswer); + when(removeTungstenPolicyAnswer.getTungstenModel()).thenReturn(tungstenNetworkPolicy); + when(tungstenNetworkPolicy.getNetworkPolicy()).thenReturn(networkPolicy); + when(tungstenNetworkPolicy.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.removeTungstenPolicy(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void removeTungstenTagTest() { + TungstenAnswer removeTungstenTagAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenTag tungstenTag = mock(TungstenTag.class); + Tag tag = mock(Tag.class); + NetworkPolicy networkPolicy = mock(NetworkPolicy.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + VirtualMachine virtualMachine = mock(VirtualMachine.class); + VirtualMachineInterface virtualMachineInterface = mock(VirtualMachineInterface.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenTagCommand.class), anyLong())).thenReturn(removeTungstenTagAnswer); + when(removeTungstenTagAnswer.getTungstenModel()).thenReturn(tungstenTag); + when(tungstenTag.getTag()).thenReturn(tag); + when(tungstenTag.getNetworkPolicyList()).thenReturn(List.of(networkPolicy)); + when(tungstenTag.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + when(tungstenTag.getVirtualMachineList()).thenReturn(List.of(virtualMachine)); + when(tungstenTag.getVirtualMachineInterfaceList()).thenReturn(List.of(virtualMachineInterface)); + + assertNotNull(tungstenService.removeTungstenTag(1L, List.of("948f421c-edde-4518-a391-09299cc25dc2"), + List.of("8b4637b6-5629-46de-8fb2-d0b0502bfa85"), + List.of("8d097a79-a38d-4db4-8a41-16f15d9c5afa"), "a329662e-1805-4a89-9b05-2b818ea35978", null, + "d5e3f5c5-97ed-41b6-9b6f-7f696b9eddeb")); + } + + @Test + public void createTungstenAddressGroupTest() { + TungstenAnswer createTungstenAddressGroupAnswer = new MockTungstenAnswerFactory(true).get(); + AddressGroup addressGroup = mock(AddressGroup.class); + SubnetListType subnetListType = mock(SubnetListType.class); + SubnetType subnetType = mock(SubnetType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenAddressGroupCommand.class), anyLong())).thenReturn(createTungstenAddressGroupAnswer); + when(createTungstenAddressGroupAnswer.getApiObjectBase()).thenReturn(addressGroup); + when(addressGroup.getPrefix()).thenReturn(subnetListType); + when(subnetListType.getSubnet()).thenReturn(List.of(subnetType)); + + assertNotNull(tungstenService.createTungstenAddressGroup(1L, "test", "192.168.100.0", 24)); + } + + @Test + public void createTungstenServiceGroupTest() { + TungstenAnswer createTungstenServiceGroupAnswer = new MockTungstenAnswerFactory(true).get(); + ServiceGroup serviceGroup = mock(ServiceGroup.class); + FirewallServiceGroupType firewallServiceGroupType = mock(FirewallServiceGroupType.class); + FirewallServiceType firewallServiceType = mock(FirewallServiceType.class); + PortType portType = mock(PortType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenServiceGroupCommand.class), anyLong())).thenReturn(createTungstenServiceGroupAnswer); + when(createTungstenServiceGroupAnswer.getApiObjectBase()).thenReturn(serviceGroup); + when(serviceGroup.getFirewallServiceList()).thenReturn(firewallServiceGroupType); + when(firewallServiceGroupType.getFirewallService()).thenReturn(List.of(firewallServiceType)); + when(firewallServiceType.getDstPorts()).thenReturn(portType); + + assertNotNull(tungstenService.createTungstenServiceGroup(1L, "test", "tcp", 80, 80)); + } + + @Test + public void createTungstenFirewallRuleTest() { + TungstenAnswer createTungstenFirewallRuleAnswer = new MockTungstenAnswerFactory(true).get(); + net.juniper.tungsten.api.types.FirewallRule firewallRule = mock(net.juniper.tungsten.api.types.FirewallRule.class); + ActionListType actionListType = mock(ActionListType.class); + ObjectReference serviceGroup = mock(ObjectReference.class); + FirewallRuleEndpointType firewallRuleEndpointType1 = mock(FirewallRuleEndpointType.class); + FirewallRuleEndpointType firewallRuleEndpointType2 = mock(FirewallRuleEndpointType.class); + FirewallRuleMatchTagsType firewallRuleMatchTagsType = mock(FirewallRuleMatchTagsType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenFirewallRuleCommand.class), anyLong())).thenReturn(createTungstenFirewallRuleAnswer); + when(createTungstenFirewallRuleAnswer.getApiObjectBase()).thenReturn(firewallRule); + when(firewallRule.getActionList()).thenReturn(actionListType); + when(actionListType.getSimpleAction()).thenReturn("pass"); + when(firewallRule.getServiceGroup()).thenReturn(List.of(serviceGroup)); + when(serviceGroup.getReferredName()).thenReturn(List.of("service")); + when(firewallRule.getEndpoint1()).thenReturn(firewallRuleEndpointType1); + when(firewallRule.getEndpoint2()).thenReturn(firewallRuleEndpointType2); + when(firewallRuleEndpointType1.getTags()).thenReturn(List.of("tag")); + when(firewallRuleEndpointType2.getTags()).thenReturn(null); + when(firewallRuleEndpointType2.getAddressGroup()).thenReturn("address:group"); + when(firewallRule.getMatchTags()).thenReturn(firewallRuleMatchTagsType); + + assertNotNull(tungstenService.createTungstenFirewallRule(1L, "f5ba12c8-d4c5-4c20-a57d-67a9b6fca652", "test", "pass", "948f421c-edde-4518-a391-09299cc25dc2", + "8b4637b6-5629-46de-8fb2-d0b0502bfa85", "8d097a79-a38d-4db4-8a41-16f15d9c5afa", null, "<>", "a329662e-1805-4a89-9b05-2b818ea35978" + , "d5e3f5c5-97ed-41b6-9b6f-7f696b9eddeb", null, "df8e4490-2a40-4d63-a6f3-1f829ffe4fc6", 1)); + } + + @Test + public void createTungstenFirewallPolicyTest() { + TungstenAnswer createTungstenFirewallPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + FirewallPolicy firewallPolicy = mock(FirewallPolicy.class); + ObjectReference firewallSequenceObjectReference = mock(ObjectReference.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenFirewallPolicyCommand.class), anyLong())).thenReturn(createTungstenFirewallPolicyAnswer); + when(createTungstenFirewallPolicyAnswer.getApiObjectBase()).thenReturn(firewallPolicy); + when(firewallPolicy.getFirewallRule()).thenReturn(List.of(firewallSequenceObjectReference)); + when(firewallSequenceObjectReference.getReferredName()).thenReturn(List.of("firewallrule")); + + assertNotNull(tungstenService.createTungstenFirewallPolicy(1L, "f5ba12c8-d4c5-4c20-a57d-67a9b6fca652","test", 1)); + } + + @Test + public void createTungstenApplicationPolicySetTest() { + TungstenAnswer createTungstenApplicationPolicySetAnswer = new MockTungstenAnswerFactory(true).get(); + ApplicationPolicySet applicationPolicySet = mock(ApplicationPolicySet.class); + ObjectReference objectReference = mock(ObjectReference.class); + ObjectReference firewallSequenceObjectReference = mock(ObjectReference.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenApplicationPolicySetCommand.class), anyLong())).thenReturn(createTungstenApplicationPolicySetAnswer); + when(createTungstenApplicationPolicySetAnswer.getApiObjectBase()).thenReturn(applicationPolicySet); + when(applicationPolicySet.getTag()).thenReturn(List.of(objectReference)); + when(objectReference.getReferredName()).thenReturn(List.of("tag")); + when(applicationPolicySet.getFirewallPolicy()).thenReturn(List.of(firewallSequenceObjectReference)); + when(firewallSequenceObjectReference.getReferredName()).thenReturn(List.of("firewallrule")); + + assertNotNull(tungstenService.createTungstenApplicationPolicySet(1L, "test")); + } + + @Test + public void listTungstenApplicationPolicySetTest() { + TungstenAnswer listTungstenApplicationPolicySetAnswer = new MockTungstenAnswerFactory(true).get(); + ApplicationPolicySet applicationPolicySet = mock(ApplicationPolicySet.class); + ObjectReference objectReference = mock(ObjectReference.class); + ObjectReference firewallSequenceObjectReference = mock(ObjectReference.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenApplicationPolicySetCommand.class), anyLong())).thenReturn(listTungstenApplicationPolicySetAnswer); + doReturn(List.of(applicationPolicySet)).when(listTungstenApplicationPolicySetAnswer).getApiObjectBaseList(); + when(applicationPolicySet.getTag()).thenReturn(List.of(objectReference)); + when(objectReference.getReferredName()).thenReturn(List.of("tag")); + when(applicationPolicySet.getFirewallPolicy()).thenReturn(List.of(firewallSequenceObjectReference)); + when(firewallSequenceObjectReference.getReferredName()).thenReturn(List.of("firewallrule")); + + assertNotNull(tungstenService.listTungstenApplicationPolicySet(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void listTungstenFirewallPolicyTest() { + TungstenAnswer listTungstenFirewallPolicyAnswer = new MockTungstenAnswerFactory(true).get(); + FirewallPolicy firewallPolicy = mock(FirewallPolicy.class); + ObjectReference firewallSequenceObjectReference = mock(ObjectReference.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenFirewallPolicyCommand.class), anyLong())).thenReturn(listTungstenFirewallPolicyAnswer); + doReturn(List.of(firewallPolicy)).when(listTungstenFirewallPolicyAnswer).getApiObjectBaseList(); + when(firewallPolicy.getFirewallRule()).thenReturn(List.of(firewallSequenceObjectReference)); + when(firewallSequenceObjectReference.getReferredName()).thenReturn(List.of("firewallrule")); + + assertNotNull(tungstenService.listTungstenFirewallPolicy(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void listTungstenFirewallRuleTest() { + TungstenAnswer listTungstenFirewallRuleAnswer = new MockTungstenAnswerFactory(true).get(); + net.juniper.tungsten.api.types.FirewallRule firewallRule = mock(net.juniper.tungsten.api.types.FirewallRule.class); + ActionListType actionListType = mock(ActionListType.class); + ObjectReference serviceGroup = mock(ObjectReference.class); + FirewallRuleEndpointType firewallRuleEndpointType1 = mock(FirewallRuleEndpointType.class); + FirewallRuleEndpointType firewallRuleEndpointType2 = mock(FirewallRuleEndpointType.class); + FirewallRuleMatchTagsType firewallRuleMatchTagsType = mock(FirewallRuleMatchTagsType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenFirewallRuleCommand.class), anyLong())).thenReturn(listTungstenFirewallRuleAnswer); + doReturn(List.of(firewallRule)).when(listTungstenFirewallRuleAnswer).getApiObjectBaseList(); + when(firewallRule.getActionList()).thenReturn(actionListType); + when(actionListType.getSimpleAction()).thenReturn("pass"); + when(firewallRule.getServiceGroup()).thenReturn(List.of(serviceGroup)); + when(serviceGroup.getReferredName()).thenReturn(List.of("service")); + when(firewallRule.getEndpoint1()).thenReturn(firewallRuleEndpointType1); + when(firewallRule.getEndpoint2()).thenReturn(firewallRuleEndpointType2); + when(firewallRuleEndpointType1.getTags()).thenReturn(List.of("tag")); + when(firewallRuleEndpointType2.getTags()).thenReturn(null); + when(firewallRuleEndpointType2.getAddressGroup()).thenReturn("address:group"); + when(firewallRule.getMatchTags()).thenReturn(firewallRuleMatchTagsType); + + assertNotNull(tungstenService.listTungstenFirewallRule(1L, "948f421c-edde-4518-a391-09299cc25dc2", + "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void listTungstenAddressGroupTest() { + TungstenAnswer listTungstenAddressGroupAnswer = new MockTungstenAnswerFactory(true).get(); + AddressGroup addressGroup = mock(AddressGroup.class); + SubnetListType subnetListType = mock(SubnetListType.class); + SubnetType subnetType = mock(SubnetType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenAddressGroupCommand.class), anyLong())).thenReturn(listTungstenAddressGroupAnswer); + doReturn(List.of(addressGroup)).when(listTungstenAddressGroupAnswer).getApiObjectBaseList(); + when(addressGroup.getPrefix()).thenReturn(subnetListType); + when(subnetListType.getSubnet()).thenReturn( + List.of(subnetType)); + + assertNotNull(tungstenService.listTungstenAddressGroup(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void listTungstenServiceGroupTest() { + TungstenAnswer listTungstenServiceGroupAnswer = new MockTungstenAnswerFactory(true).get(); + ServiceGroup serviceGroup = mock(ServiceGroup.class); + FirewallServiceGroupType firewallServiceGroupType = mock(FirewallServiceGroupType.class); + FirewallServiceType firewallServiceType = mock(FirewallServiceType.class); + PortType portType = mock(PortType.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenServiceGroupCommand.class), anyLong())).thenReturn(listTungstenServiceGroupAnswer); + doReturn(List.of(serviceGroup)).when(listTungstenServiceGroupAnswer).getApiObjectBaseList(); + when(serviceGroup.getFirewallServiceList()).thenReturn(firewallServiceGroupType); + when(firewallServiceGroupType.getFirewallService()).thenReturn(List.of(firewallServiceType)); + when(firewallServiceType.getDstPorts()).thenReturn(portType); + + assertNotNull(tungstenService.listTungstenServiceGroup(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenApplicationPolicySetTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenApplicationPolicySetCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteTungstenApplicationPolicySet(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenFirewallPolicyTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFirewallPolicyCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteTungstenFirewallPolicy(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenFirewallRuleTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenFirewallRuleCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteTungstenFirewallRule(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenServiceGroupTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenServiceGroupCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteTungstenServiceGroup(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void deleteTungstenAddressGroupTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenAddressGroupCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteTungstenAddressGroup(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void createSharedNetworkTest() { + Network network = mock(Network.class); + Vlan vlan = mock(Vlan.class); + AccountVO accountVO = mock(AccountVO.class); + TungstenAnswer createTungstenSharedNetworkAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer addNetworkSubnetAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenAnswer getTungstenNetworkDnsAnswer = new MockTungstenAnswerFactory(true).get(); + NetworkDetailVO networkDetailVO = mock(NetworkDetailVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + HostVO hostVO = mock(HostVO.class); + Answer answer = mock(Answer.class); + IPAddressVO ipAddressVO = mock(IPAddressVO.class); + ApiObjectBase apiObjectBase = mock(ApiObjectBase.class); + + when(accountDao.findById(anyLong())).thenReturn(accountVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenNetworkCommand.class), anyLong())).thenReturn(createTungstenSharedNetworkAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenNetworkSubnetCommand.class), anyLong())).thenReturn(addNetworkSubnetAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenNetworkDnsCommand.class), anyLong())).thenReturn(getTungstenNetworkDnsAnswer); + when(network.getMode()).thenReturn(Networks.Mode.Dhcp); + when(network.getCidr()).thenReturn("192.168.100.0/24"); + when(vlan.getIpRange()).thenReturn("192.168.100.100-192.168.100.200"); + when(vlan.getVlanGateway()).thenReturn("192.168.100.1"); + when(vlan.getIp6Gateway()).thenReturn("fd00::1"); + when(networkDetailsDao.persist(any(NetworkDetailVO.class))).thenReturn(networkDetailVO); + when(tungstenProviderDao.findByZoneId(anyLong())).thenReturn(tungstenProviderVO); + when(hostDao.findByPublicIp(anyString())).thenReturn(hostVO); + when(agentMgr.easySend(anyLong(), any(SetupTungstenVRouterCommand.class))).thenReturn(answer); + when(answer.getResult()).thenReturn(true); + when(vlan.getIp6Cidr()).thenReturn("fd00::1/64"); + when(vlan.getIp6Range()).thenReturn("fd00::100-fd00::200"); + when(getTungstenNetworkDnsAnswer.getDetails()).thenReturn("192.168.1.150"); + when(createTungstenSharedNetworkAnswer.getApiObjectBase()).thenReturn(apiObjectBase); + when(apiObjectBase.getQualifiedName()).thenReturn(List.of("network")); + when(tungstenProviderVO.getGateway()).thenReturn("192.168.100.1"); + + assertTrue(tungstenService.createSharedNetwork(network, vlan)); + } + + @Test + public void addTungstenVmSecurityGroupTest() { + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenProviderVO tungstenProviderVO = mock(TungstenProviderVO.class); + TungstenAnswer getTungstenSecurityGroupAnswer = mock(TungstenAnswer.class); + TungstenAnswer addTungstenVmToSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + net.juniper.tungsten.api.types.SecurityGroup securityGroup = mock(net.juniper.tungsten.api.types.SecurityGroup.class); + NicVO nicVO = mock(NicVO.class); + SecurityGroupRuleVO securityGroupRuleVO = mock(SecurityGroupRuleVO.class); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + TungstenAnswer addTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(true); + when(securityGroupManager.getSecurityGroupsForVm(anyLong())).thenReturn(List.of(securityGroupVO)); + when(tungstenProviderDao.findAll()).thenReturn(List.of(tungstenProviderVO)); + when(tungstenFabricUtils.sendTungstenCommand(any(GetTungstenSecurityGroupCommand.class), anyLong())).thenReturn(getTungstenSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenVmToSecurityGroupCommand.class), anyLong())).thenReturn(addTungstenVmToSecurityGroupAnswer); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenSecurityGroupRuleCommand.class), anyLong())).thenReturn(addTungstenSecurityGroupRuleAnswer); + when(getTungstenSecurityGroupAnswer.getApiObjectBase()).thenReturn(securityGroup); + when(nicDao.findDefaultNicForVM(anyLong())).thenReturn(nicVO); + when(nicVO.getBroadcastUri()).thenReturn(Networks.BroadcastDomainType.TUNGSTEN.toUri("tf")); + when(securityGroupRuleDao.listByAllowedSecurityGroupId(anyLong())).thenReturn(List.of(securityGroupRuleVO)); + when(nicVO.getIPv4Address()).thenReturn("192.168.100.100"); + when(nicVO.getIPv6Address()).thenReturn("fd00::1"); + when(nicVO.getSecondaryIp()).thenReturn(true); + when(nicSecIpDao.getSecondaryIpAddressesForNic(anyLong())).thenReturn(List.of("192.168.100.200")); + when(securityGroupRuleVO.getProtocol()).thenReturn(NetUtils.ALL_PROTO); + when(tungstenSecurityGroupRuleDao.persist(any(TungstenSecurityGroupRuleVO.class))).thenReturn(tungstenSecurityGroupRuleVO); + + assertTrue(tungstenService.addTungstenVmSecurityGroup(vmInstanceVO)); + } + + @Test + public void removeTungstenVmSecurityGroupTest() { + VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + SecurityGroupVO securityGroupVO = mock(SecurityGroupVO.class); + TungstenAnswer removeTungstenVmFromSecurityGroupAnswer = new MockTungstenAnswerFactory(true).get(); + NicVO nicVO = mock(NicVO.class); + SecurityGroupRuleVO securityGroupRuleVO = mock(SecurityGroupRuleVO.class); + TungstenAnswer removeTungstenSecurityGroupRuleAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenSecurityGroupRuleVO tungstenSecurityGroupRuleVO = mock(TungstenSecurityGroupRuleVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(dataCenterVO.isSecurityGroupEnabled()).thenReturn(true); + + assertTrue(tungstenService.removeTungstenVmSecurityGroup(vmInstanceVO)); + } + + @Test + public void createRoutingLogicalRouterTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenLogicalRouter tungstenLogicalRouter = mock(TungstenLogicalRouter.class); + LogicalRouter logicalRouter = mock(LogicalRouter.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(CreateTungstenRoutingLogicalRouterCommand.class), anyLong())).thenReturn(tungstenAnswer); + when(tungstenAnswer.getTungstenModel()).thenReturn(tungstenLogicalRouter); + when(tungstenLogicalRouter.getLogicalRouter()).thenReturn(logicalRouter); + when(tungstenLogicalRouter.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.createRoutingLogicalRouter(1L, "default-domain:default-project", "testLR")); + } + + @Test + public void addNetworkGatewayToLogicalRouterTest() { + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenLogicalRouter tungstenLogicalRouter = mock(TungstenLogicalRouter.class); + LogicalRouter logicalRouter = mock(LogicalRouter.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(networkDao.findByUuid(anyString())).thenReturn(networkVO); + when(ipAddressManager.acquireLastGuestIpAddress(any(Network.class))).thenReturn("192.168.100.100"); + when(tungstenFabricUtils.sendTungstenCommand(any(AddTungstenNetworkGatewayToLogicalRouterCommand.class), anyLong())).thenReturn(tungstenAnswer); + when(tungstenAnswer.getTungstenModel()).thenReturn(tungstenLogicalRouter); + when(tungstenLogicalRouter.getLogicalRouter()).thenReturn(logicalRouter); + when(tungstenLogicalRouter.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.addNetworkGatewayToLogicalRouter(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void listRoutingLogicalRouterTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenLogicalRouter tungstenLogicalRouter = mock(TungstenLogicalRouter.class); + LogicalRouter logicalRouter = mock(LogicalRouter.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(tungstenFabricUtils.sendTungstenCommand(any(ListTungstenRoutingLogicalRouterCommand.class), anyLong())).thenReturn(tungstenAnswer); + when(tungstenAnswer.getTungstenModelList()).thenReturn(List.of(tungstenLogicalRouter)); + when(tungstenLogicalRouter.getLogicalRouter()).thenReturn(logicalRouter); + when(tungstenLogicalRouter.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.listRoutingLogicalRouter(1L, null, "948f421c-edde-4518-a391-09299cc25dc2")); + } + + @Test + public void removeNetworkGatewayFromLogicalRouterTest() { + NetworkVO networkVO = mock(NetworkVO.class); + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + TungstenGuestNetworkIpAddressVO tungstenGuestNetworkIpAddressVO = mock(TungstenGuestNetworkIpAddressVO.class); + TungstenLogicalRouter tungstenLogicalRouter = mock(TungstenLogicalRouter.class); + LogicalRouter logicalRouter = mock(LogicalRouter.class); + VirtualNetwork virtualNetwork = mock(VirtualNetwork.class); + DataCenterVO dataCenterVO = mock(DataCenterVO.class); + + when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO); + when(networkDao.findByUuid(anyString())).thenReturn(networkVO); + when(tungstenGuestNetworkIpAddressDao.findByNetworkAndLogicalRouter(anyLong(), anyString())).thenReturn(tungstenGuestNetworkIpAddressVO); + when(tungstenFabricUtils.sendTungstenCommand(any(RemoveTungstenNetworkGatewayFromLogicalRouterCommand.class), anyLong())).thenReturn(tungstenAnswer); + when(tungstenAnswer.getTungstenModel()).thenReturn(tungstenLogicalRouter); + when(tungstenLogicalRouter.getLogicalRouter()).thenReturn(logicalRouter); + when(tungstenLogicalRouter.getVirtualNetworkList()).thenReturn(List.of(virtualNetwork)); + + assertNotNull(tungstenService.removeNetworkGatewayFromLogicalRouter(1L, "948f421c-edde-4518-a391-09299cc25dc2", "8b4637b6-5629-46de-8fb2-d0b0502bfa85")); + } + + @Test + public void deleteLogicalRouterTest() { + TungstenAnswer tungstenAnswer = new MockTungstenAnswerFactory(true).get(); + + when(tungstenFabricUtils.sendTungstenCommand(any(DeleteTungstenRoutingLogicalRouterCommand.class), anyLong())).thenReturn(tungstenAnswer); + + assertTrue(tungstenService.deleteLogicalRouter(1L, "948f421c-edde-4518-a391-09299cc25dc2")); + } +} \ No newline at end of file