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);
}
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;
}
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);
}
}
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 ");
}
}
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 ");
}
}
Aggregations