use of net.juniper.contrail.api.types.VirtualMachineInterface in project cloudstack by apache.
the class NetworkProviderTest method dbSyncTest.
@Test
public void dbSyncTest() {
Network network = lookupTestNetwork("test-db-only-net");
if (network == null) {
network = createTestNetwork("test-db-only-net");
}
UserVm vm = _server.createVM("test-db-only-vm", network);
try {
createFloatingIp(network, vm);
} catch (Exception e) {
fail("unable to create floating ip");
}
/* reset ApiServer objects to default config only, so above created objects
* exists only in cludstack db but not in api server
*/
((ApiConnectorMock) _api).initConfig();
/* reset model cached objects */
_contrailMgr.getDatabase().initDb();
/* Create one object of each type directly in api-server - these objects does not exist in cloudstack */
net.juniper.contrail.api.types.Domain domain = new net.juniper.contrail.api.types.Domain();
domain.setName("test-vnc-only-domain--1");
domain.setUuid(UUID.randomUUID().toString());
try {
assertTrue(_api.create(domain));
} catch (IOException ex) {
fail(ex.getMessage());
}
Project project = new Project();
project.setName("test-vnc-only-project-1");
project.setUuid(UUID.randomUUID().toString());
project.setParent(domain);
try {
assertTrue(_api.create(project));
} catch (IOException ex) {
fail(ex.getMessage());
}
VirtualNetwork net = new VirtualNetwork();
net.setName("test-vnc-only-net-1");
net.setUuid(UUID.randomUUID().toString());
net.setParent(project);
NetworkIpam ipam = null;
try {
// Find default-network-ipam
String ipam_id = _api.findByName(NetworkIpam.class, null, "default-network-ipam");
assertNotNull(ipam_id);
ipam = (NetworkIpam) _api.findById(NetworkIpam.class, ipam_id);
assertNotNull(ipam);
} catch (IOException ex) {
fail(ex.getMessage());
}
VnSubnetsType subnet = new VnSubnetsType();
subnet.addIpamSubnets(new SubnetType("10.0.2.0", 24), "10.0.2.254");
net.addNetworkIpam(ipam, subnet);
VirtualMachine vncVm = new VirtualMachine();
vncVm.setName("test-vnc-only-vm-1");
try {
assertTrue(_api.create(vncVm));
} catch (IOException ex) {
fail(ex.getMessage());
}
VirtualMachineInterface vmi = new VirtualMachineInterface();
vmi.setParent(vncVm);
vmi.setName("test-vnc-only-vmi-1");
try {
assertTrue(_api.create(vmi));
assertTrue(_api.create(net));
} catch (IOException ex) {
fail(ex.getMessage());
}
InstanceIp ip_obj = new InstanceIp();
ip_obj.setName(net.getName() + ":0");
ip_obj.setVirtualNetwork(net);
ip_obj.setVirtualMachineInterface(vmi);
try {
assertTrue(_api.create(ip_obj));
// Must perform a GET in order to update the object contents.
assertTrue(_api.read(ip_obj));
assertNotNull(ip_obj.getAddress());
} catch (IOException ex) {
fail(ex.getMessage());
}
// now db sync
if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_UPDATE) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
s_logger.info("# Cloudstack DB & VNC are out of sync - resync done");
}
if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_CHECK) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
s_logger.info("# Cloudstack DB & VNC are still out of sync");
fail("DB Sync failed");
}
}
use of net.juniper.contrail.api.types.VirtualMachineInterface in project cloudstack by apache.
the class ServerDBSyncImpl method deleteVirtualMachineInterfaces.
private void deleteVirtualMachineInterfaces(List<ObjectReference<ApiPropertyBase>> list, StringBuffer syncLogMesg) throws IOException {
if (list == null) {
return;
}
final ApiConnector api = _manager.getApiConnector();
for (ObjectReference<ApiPropertyBase> vmiRef : list) {
VirtualMachineInterface vmi = (VirtualMachineInterface) api.findById(VirtualMachineInterface.class, vmiRef.getUuid());
deleteInstanceIps(vmi.getInstanceIpBackRefs(), syncLogMesg);
deleteFloatingIps(vmi.getFloatingIpBackRefs(), syncLogMesg);
api.delete(VirtualMachineInterface.class, vmiRef.getUuid());
syncLogMesg.append("VNC vmi: " + vmi.getUuid() + " deleted\n");
}
}
use of net.juniper.contrail.api.types.VirtualMachineInterface in project cloudstack by apache.
the class ContrailGuru method reserve.
/**
* Allocate the ip address (and mac) for the specified VM device.
*/
@Override
public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
s_logger.debug("reserve NicProfile on network id: " + network.getId() + " " + network.getName());
s_logger.debug("deviceId: " + nic.getDeviceId());
NicVO nicVO = _nicDao.findById(nic.getId());
assert nicVO != null;
VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
/* Network must have been implemented */
assert vnModel != null;
VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
if (vmModel == null) {
VMInstanceVO vmVo = (VMInstanceVO) vm.getVirtualMachine();
vmModel = new VirtualMachineModel(vmVo, vm.getUuid());
vmModel.setProperties(_manager.getModelController(), vmVo);
}
VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
if (vmiModel == null) {
vmiModel = new VMInterfaceModel(nicVO.getUuid());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
}
try {
vmiModel.build(_manager.getModelController(), (VMInstanceVO) vm.getVirtualMachine(), nicVO);
vmiModel.setActive();
} catch (IOException ex) {
s_logger.error("virtual-machine-interface set", ex);
return;
}
InstanceIpModel ipModel = vmiModel.getInstanceIp();
if (ipModel == null) {
ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
ipModel.addToVMInterface(vmiModel);
} else {
s_logger.debug("Reuse existing instance-ip object on " + ipModel.getName());
}
if (nic.getIPv4Address() != null) {
s_logger.debug("Nic using existing IP address " + nic.getIPv4Address());
ipModel.setAddress(nic.getIPv4Address());
}
try {
vmModel.update(_manager.getModelController());
} catch (Exception ex) {
s_logger.warn("virtual-machine update", ex);
return;
}
_manager.getDatabase().getVirtualMachines().add(vmModel);
VirtualMachineInterface vmi = vmiModel.getVMInterface();
// allocate mac address
if (nic.getMacAddress() == null) {
MacAddressesType macs = vmi.getMacAddresses();
if (macs == null) {
s_logger.debug("no mac address is allocated for Nic " + nicVO.getUuid());
} else {
s_logger.info("VMI " + _manager.getVifNameByVmUuid(vm.getUuid(), nicVO.getDeviceId()) + " got mac address: " + macs.getMacAddress().get(0));
nic.setMacAddress(macs.getMacAddress().get(0));
}
}
if (nic.getIPv4Address() == null) {
s_logger.debug("Allocated IP address " + ipModel.getAddress());
nic.setIPv4Address(ipModel.getAddress());
if (network.getCidr() != null) {
nic.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
}
nic.setIPv4Gateway(network.getGateway());
nic.setFormat(AddressFormat.Ip4);
}
}
use of net.juniper.contrail.api.types.VirtualMachineInterface 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 ");
}
}
use of net.juniper.contrail.api.types.VirtualMachineInterface in project cloudstack by apache.
the class VMInterfaceModel method update.
@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
if (!_netActive || !_nicActive) {
s_logger.debug("vm interface update, _netActive: " + _netActive + ", _nicActive: " + _nicActive);
delete(controller);
return;
}
if (_vmModel == null) {
throw new InternalErrorException("virtual-machine not set on VMI: " + _uuid);
}
if (_vnModel == null) {
throw new InternalErrorException("virtual-network not set on VMI: " + _uuid);
}
ContrailManager manager = controller.getManager();
ApiConnector api = controller.getApiAccessor();
VirtualMachineInterface vmi = (VirtualMachineInterface) api.findById(VirtualMachineInterface.class, _uuid);
boolean create = false;
if (vmi == null) {
create = true;
vmi = new VirtualMachineInterface();
vmi.setParent(_vmModel.getVirtualMachine());
vmi.setName(manager.getVifNameByVmName(_vmModel.getInstanceName(), _deviceId));
vmi.setUuid(_uuid);
vmi.setVirtualNetwork(_vnModel.getVirtualNetwork());
} else {
// Do not try to update VMI to routing-instance references. These are managed by schema-transformer.
vmi.clearRoutingInstance();
}
_vmi = vmi;
if (_macAddress != null) {
MacAddressesType mac = new MacAddressesType();
mac.addMacAddress(_macAddress);
vmi.setMacAddresses(mac);
}
if (_serviceTag != null) {
vmi.setProperties(new VirtualMachineInterfacePropertiesType(_serviceTag, null));
}
if (create) {
if (!api.create(vmi)) {
throw new InternalErrorException("Unable to create virtual-machine-interface " + _uuid);
}
} else {
if (!api.update(vmi)) {
throw new InternalErrorException("Unable to update virtual-machine-interface " + _uuid);
}
}
api.read(vmi);
int ipCount = 0;
for (ModelObject successor : successors()) {
if (successor.getClass() == InstanceIpModel.class) {
ipCount++;
}
successor.update(controller);
}
// delete the object.
if (ipCount == 0) {
s_logger.warn("virtual-machine-interface " + _uuid + " has no instance-ip");
}
}
Aggregations