Search in sources :

Example 6 with NetworkDao

use of com.cloud.network.dao.NetworkDao in project cloudstack by apache.

the class AssignLoadBalancerTest method testVmIdAlreadyExist.

@Test(expected = InvalidParameterValueException.class)
public void testVmIdAlreadyExist() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
    AssignToLoadBalancerRuleCmd assignLbRuleCmd = Mockito.mock(AssignToLoadBalancerRuleCmd.class);
    Map<Long, List<String>> vmIdIpMap = new HashMap<Long, List<String>>();
    List<String> secIp = new ArrayList<String>();
    secIp.add("10.1.1.175");
    vmIdIpMap.put(1L, secIp);
    List<Long> vmIds = new ArrayList<Long>();
    vmIds.add(2L);
    LoadBalancerVO lbVO = new LoadBalancerVO("1", "L1", "Lbrule", 1, 22, 22, "rb", 204, 0, 0, "tcp");
    UserVmVO vm = new UserVmVO(2L, "test", "test", 101L, Hypervisor.HypervisorType.Any, 21L, false, false, domainId, 200L, 1, 5L, "", "test");
    LoadBalancerDao lbDao = Mockito.mock(LoadBalancerDao.class);
    LoadBalancerVMMapDao lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
    UserVmDao userVmDao = Mockito.mock(UserVmDao.class);
    AccountDao accountDao = Mockito.mock(AccountDao.class);
    AccountManager accountMgr = Mockito.mock(AccountManager.class);
    NetworkDao networkDao = Mockito.mock(NetworkDao.class);
    NicSecondaryIpDao nicSecIpDao = Mockito.mock(NicSecondaryIpDao.class);
    LoadBalancerVMMapVO lbVmMapVO = new LoadBalancerVMMapVO(1L, 1L, "10.1.1.175", false);
    _lbMgr._lbDao = lbDao;
    _lbMgr._lb2VmMapDao = lb2VmMapDao;
    _lbMgr._vmDao = userVmDao;
    _lbMgr._accountDao = accountDao;
    _lbMgr._accountMgr = accountMgr;
    _lbMgr._networkDao = networkDao;
    _lbMgr._nicSecondaryIpDao = nicSecIpDao;
    _lbvmMapList = new ArrayList<>();
    _lbvmMapList.add(lbVmMapVO);
    _lbMgr._rulesMgr = _rulesMgr;
    _lbMgr._networkModel = _networkModel;
    when(lbDao.findById(anyLong())).thenReturn(lbVO);
    when(userVmDao.findById(anyLong())).thenReturn(vm);
    when(lb2VmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(_lbvmMapList);
    when(nicSecIpDao.findByIp4AddressAndNicId(anyString(), anyLong())).thenReturn(null);
    _lbMgr.assignToLoadBalancer(1L, null, vmIdIpMap);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) LoadBalancerDao(com.cloud.network.dao.LoadBalancerDao) NicSecondaryIpDao(com.cloud.vm.dao.NicSecondaryIpDao) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) AccountDao(com.cloud.user.dao.AccountDao) Matchers.anyString(org.mockito.Matchers.anyString) AssignToLoadBalancerRuleCmd(org.apache.cloudstack.api.command.user.loadbalancer.AssignToLoadBalancerRuleCmd) NetworkDao(com.cloud.network.dao.NetworkDao) Matchers.anyLong(org.mockito.Matchers.anyLong) LoadBalancerVMMapDao(com.cloud.network.dao.LoadBalancerVMMapDao) List(java.util.List) ArrayList(java.util.ArrayList) AccountManager(com.cloud.user.AccountManager) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) UserVmDao(com.cloud.vm.dao.UserVmDao) Test(org.junit.Test)

Example 7 with NetworkDao

use of com.cloud.network.dao.NetworkDao in project cloudstack by apache.

the class PrivateGatewayRules method retrievePrivateNetwork.

public Network retrievePrivateNetwork(final NetworkTopologyVisitor visitor) {
    // This network might be the same we have already as an instance in the
    // RuleApplier super class.
    // Just doing this here, but will double check is remove if it's not
    // needed.
    final NetworkDao networkDao = visitor.getVirtualNetworkApplianceFactory().getNetworkDao();
    final Network network = networkDao.findById(_nicProfile.getNetworkId());
    return network;
}
Also used : NetworkDao(com.cloud.network.dao.NetworkDao) Network(com.cloud.network.Network)

Example 8 with NetworkDao

use of com.cloud.network.dao.NetworkDao in project cloudstack by apache.

the class VirtualMachineModel method setServiceInstanceNics.

private void setServiceInstanceNics(ModelController controller, VMInstanceVO instance) throws IOException {
    NicDao nicDao = controller.getNicDao();
    ContrailManager manager = controller.getManager();
    NetworkDao networkDao = controller.getNetworkDao();
    List<NicVO> nics = nicDao.listByVmId(_instanceId);
    for (NicVO nic : nics) {
        String tag;
        switch(nic.getDeviceId()) {
            case 0:
                tag = "management";
                break;
            case 1:
                tag = "left";
                break;
            case 2:
                tag = "right";
                break;
            default:
                tag = null;
        }
        VMInterfaceModel vmiModel = getVMInterface(nic.getUuid());
        if (vmiModel == null) {
            vmiModel = new VMInterfaceModel(nic.getUuid());
            vmiModel.addToVirtualMachine(this);
            NetworkVO network = networkDao.findById(nic.getNetworkId());
            VirtualNetworkModel vnModel = manager.getDatabase().lookupVirtualNetwork(network.getUuid(), manager.getCanonicalName(network), network.getTrafficType());
            assert vnModel != null;
            vmiModel.addToVirtualNetwork(vnModel);
        }
        vmiModel.setProperties(controller, instance, nic);
        vmiModel.setServiceTag(tag);
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) NetworkDao(com.cloud.network.dao.NetworkDao) NicDao(com.cloud.vm.dao.NicDao) ContrailManager(org.apache.cloudstack.network.contrail.management.ContrailManager) NicVO(com.cloud.vm.NicVO)

Example 9 with NetworkDao

use of com.cloud.network.dao.NetworkDao in project cloudstack by apache.

the class InstanceIpModelTest method testCreateInstanceIp.

@Test
public void testCreateInstanceIp() throws IOException {
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getApiAccessor()).thenReturn(api);
    when(controller.getManager()).thenReturn(contrailMgr);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Implemented);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    NetworkDao networkDao = mock(NetworkDao.class);
    when(networkDao.findById(anyLong())).thenReturn(network);
    when(controller.getNetworkDao()).thenReturn(networkDao);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
    vnModel.build(controller, network);
    try {
        vnModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Running);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-machine update failed ");
    }
    // Create Virtual=Machine-Interface (VMInterface)
    NicVO nic = mock(NicVO.class);
    when(nic.getIPv4Address()).thenReturn("10.1.1.2");
    when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
    when(nic.getDeviceId()).thenReturn(100);
    when(nic.getState()).thenReturn(NicVO.State.Allocated);
    when(nic.getNetworkId()).thenReturn(10L);
    when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
    VMInterfaceModel vmiModel = new VMInterfaceModel(UUID.randomUUID().toString());
    vmiModel.addToVirtualMachine(vmModel);
    vmiModel.addToVirtualNetwork(vnModel);
    try {
        vmiModel.build(controller, vm, nic);
        vmiModel.setActive();
    } catch (Exception ex) {
        fail("vm-interface build failed ");
    }
    try {
        vmiModel.update(controller);
    } catch (Exception ex) {
        fail("vm-interface update failed ");
    }
    InstanceIpModel ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
    ipModel.addToVMInterface(vmiModel);
    ipModel.setAddress(nic.getIPv4Address());
    try {
        ipModel.update(controller);
    } catch (Exception ex) {
        fail("ipInstance update failed ");
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) IOException(java.io.IOException) ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkDao(com.cloud.network.dao.NetworkDao) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Example 10 with NetworkDao

use of com.cloud.network.dao.NetworkDao in project cloudstack by apache.

the class VMInterfaceModelTest method testCreateVMInterface.

@Test
public void testCreateVMInterface() throws IOException {
    String uuid;
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Implemented);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    NetworkDao networkDao = mock(NetworkDao.class);
    when(networkDao.findById(anyLong())).thenReturn(network);
    when(controller.getNetworkDao()).thenReturn(networkDao);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
    vnModel.build(controller, network);
    try {
        vnModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Running);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-machine update failed ");
    }
    // Create Virtual=Machine-Interface (VMInterface)
    NicVO nic = mock(NicVO.class);
    when(nic.getIPv4Address()).thenReturn("10.1.1.2");
    when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
    when(nic.getDeviceId()).thenReturn(100);
    when(nic.getState()).thenReturn(NicVO.State.Allocated);
    when(nic.getNetworkId()).thenReturn(10L);
    when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
    uuid = UUID.randomUUID().toString();
    VMInterfaceModel vmiModel = new VMInterfaceModel(uuid);
    assertEquals(uuid, vmiModel.getUuid());
    vmiModel.addToVirtualMachine(vmModel);
    vmiModel.addToVirtualNetwork(vnModel);
    try {
        vmiModel.build(controller, vm, nic);
        vmiModel.setActive();
    } catch (Exception ex) {
        fail("vm-interface build failed ");
    }
    try {
        vmiModel.update(controller);
    } catch (Exception ex) {
        fail("vm-interface update failed ");
    }
    // Verify virtual-machine-interface creation.
    try {
        VirtualMachineInterface vmi = (VirtualMachineInterface) api.findById(VirtualMachineInterface.class, vmiModel.getUuid());
        assertNotNull(vmi);
    } catch (Exception ex) {
        fail("vm-interface verify failed ");
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkDao(com.cloud.network.dao.NetworkDao) VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) NicVO(com.cloud.vm.NicVO) Test(org.junit.Test)

Aggregations

NetworkDao (com.cloud.network.dao.NetworkDao)10 UserVmDao (com.cloud.vm.dao.UserVmDao)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)4 Network (com.cloud.network.Network)3 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)3 LoadBalancerVMMapDao (com.cloud.network.dao.LoadBalancerVMMapDao)3 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)3 NetworkVO (com.cloud.network.dao.NetworkVO)3 AccountManager (com.cloud.user.AccountManager)3 AccountDao (com.cloud.user.dao.AccountDao)3 NicVO (com.cloud.vm.NicVO)3 UserVmVO (com.cloud.vm.UserVmVO)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Matchers.anyLong (org.mockito.Matchers.anyLong)3 Network (com.cloud.legacymodel.network.Network)2 NetworkModel (com.cloud.network.NetworkModel)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2