use of com.cloud.vm.dao.UserVmDao in project cloudstack by apache.
the class VirtualMachineModelTest method testCreateVirtualMachine.
@Test
public void testCreateVirtualMachine() throws IOException {
String uuid = UUID.randomUUID().toString();
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.Allocated);
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);
when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
// Create Virtual-Machine (VM)
VMInstanceVO vm = mock(VMInstanceVO.class);
when(vm.getInstanceName()).thenReturn("testVM1");
when(vm.getState()).thenReturn(VirtualMachine.State.Starting);
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);
assertEquals(vmModel.getInstanceName(), "testVM1");
assertEquals(vmModel.getUuid(), uuid);
vmModel.build(controller, vm);
try {
vmModel.update(controller);
} catch (Exception ex) {
fail("virtual-network update failed ");
}
//verify
assertTrue(vmModel.verify(controller));
}
use of com.cloud.vm.dao.UserVmDao in project cloudstack by apache.
the class DhcpSubNetRules method accept.
@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
_router = router;
UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
final UserVmVO vm = userVmDao.findById(_profile.getId());
userVmDao.loadDetails(vm);
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
// check if this is not the primary subnet.
final NicVO domrGuestNic = nicDao.findByInstanceIdAndIpAddressAndVmtype(_router.getId(), nicDao.getIpAddress(_nic.getNetworkId(), _router.getId()), VirtualMachine.Type.DomainRouter);
// networks.
if (!NetUtils.sameSubnet(domrGuestNic.getIPv4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
final NicIpAliasDao nicIpAliasDao = visitor.getVirtualNetworkApplianceFactory().getNicIpAliasDao();
final List<NicIpAliasVO> aliasIps = nicIpAliasDao.listByNetworkIdAndState(domrGuestNic.getNetworkId(), NicIpAlias.State.active);
boolean ipInVmsubnet = false;
for (final NicIpAliasVO alias : aliasIps) {
// check if any of the alias ips belongs to the Vm's subnet.
if (NetUtils.sameSubnet(alias.getIp4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
ipInVmsubnet = true;
break;
}
}
PublicIp routerPublicIP = null;
DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
final DataCenter dc = dcDao.findById(_router.getDataCenterId());
if (ipInVmsubnet == false) {
try {
if (_network.getTrafficType() == TrafficType.Guest && _network.getGuestType() == GuestType.Shared) {
HostPodDao podDao = visitor.getVirtualNetworkApplianceFactory().getPodDao();
podDao.findById(vm.getPodIdToDeployIn());
final Account caller = CallContext.current().getCallingAccount();
VlanDao vlanDao = visitor.getVirtualNetworkApplianceFactory().getVlanDao();
final List<VlanVO> vlanList = vlanDao.listVlansByNetworkIdAndGateway(_network.getId(), _nic.getIPv4Gateway());
final List<Long> vlanDbIdList = new ArrayList<Long>();
for (final VlanVO vlan : vlanList) {
vlanDbIdList.add(vlan.getId());
}
IpAddressManager ipAddrMgr = visitor.getVirtualNetworkApplianceFactory().getIpAddrMgr();
if (dc.getNetworkType() == NetworkType.Basic) {
routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, false);
} else {
routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, false);
}
_routerAliasIp = routerPublicIP.getAddress().addr();
}
} catch (final InsufficientAddressCapacityException e) {
s_logger.info(e.getMessage());
s_logger.info("unable to configure dhcp for this VM.");
return false;
}
// this means we did not create an IP alias on the router.
_nicAlias = new NicIpAliasVO(domrGuestNic.getId(), _routerAliasIp, _router.getId(), CallContext.current().getCallingAccountId(), _network.getDomainId(), _nic.getNetworkId(), _nic.getIPv4Gateway(), _nic.getIPv4Netmask());
_nicAlias.setAliasCount(routerPublicIP.getIpMacAddress());
nicIpAliasDao.persist(_nicAlias);
final boolean result = visitor.visit(this);
if (result == false) {
final NicIpAliasVO ipAliasVO = nicIpAliasDao.findByInstanceIdAndNetworkId(_network.getId(), _router.getId());
final PublicIp routerPublicIPFinal = routerPublicIP;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(final TransactionStatus status) {
nicIpAliasDao.expunge(ipAliasVO.getId());
IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId());
}
});
throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config");
}
}
return true;
}
return true;
}
use of com.cloud.vm.dao.UserVmDao in project cloudstack by apache.
the class UserdataToRouterRules method accept.
@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
_router = router;
UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
_userVM = userVmDao.findById(_profile.getVirtualMachine().getId());
userVmDao.loadDetails(_userVM);
// for basic zone, send vm data/password information only to the router in the same pod
NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
_nicVo = nicDao.findById(_nic.getId());
return visitor.visit(this);
}
use of com.cloud.vm.dao.UserVmDao in project cloudstack by apache.
the class AssignLoadBalancerTest method testNicIsNotInNw.
@Test(expected = InvalidParameterValueException.class)
public void testNicIsNotInNw() throws ResourceAllocationException, ResourceUnavailableException, InsufficientCapacityException {
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);
LoadBalancerDao lbDao = Mockito.mock(LoadBalancerDao.class);
LoadBalancerVMMapDao lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
UserVmDao userVmDao = Mockito.mock(UserVmDao.class);
_lbMgr._lbDao = lbDao;
_lbMgr._lb2VmMapDao = lb2VmMapDao;
_lbMgr._vmDao = userVmDao;
_lbvmMapList = new ArrayList<>();
_lbMgr._rulesMgr = _rulesMgr;
_lbMgr._networkModel = _networkModel;
when(lbDao.findById(anyLong())).thenReturn(Mockito.mock(LoadBalancerVO.class));
when(userVmDao.findById(anyLong())).thenReturn(Mockito.mock(UserVmVO.class));
when(lb2VmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(_lbvmMapList);
_lbMgr.assignToLoadBalancer(1L, null, vmIdIpMap);
}
use of com.cloud.vm.dao.UserVmDao in project cloudstack by apache.
the class AssignLoadBalancerTest method tesSecIpNotSetToVm.
@Test(expected = InvalidParameterValueException.class)
public void tesSecIpNotSetToVm() 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");
LoadBalancerDao lbDao = Mockito.mock(LoadBalancerDao.class);
LoadBalancerVMMapDao lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
UserVmDao userVmDao = Mockito.mock(UserVmDao.class);
NicSecondaryIpDao nicSecIpDao = Mockito.mock(NicSecondaryIpDao.class);
_lbMgr._lbDao = lbDao;
_lbMgr._lb2VmMapDao = lb2VmMapDao;
_lbMgr._vmDao = userVmDao;
_lbMgr._nicSecondaryIpDao = nicSecIpDao;
_lbvmMapList = new ArrayList<>();
_lbMgr._rulesMgr = _rulesMgr;
_lbMgr._networkModel = _networkModel;
when(lbDao.findById(anyLong())).thenReturn(lbVO);
when(userVmDao.findById(anyLong())).thenReturn(Mockito.mock(UserVmVO.class));
when(lb2VmMapDao.listByLoadBalancerId(anyLong(), anyBoolean())).thenReturn(_lbvmMapList);
when(nicSecIpDao.findByIp4AddressAndNicId(anyString(), anyLong())).thenReturn(null);
_lbMgr.assignToLoadBalancer(1L, null, vmIdIpMap);
}
Aggregations