use of net.juniper.contrail.api.ApiConnector 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 net.juniper.contrail.api.ApiConnector 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.ApiConnector in project cloudstack by apache.
the class ServiceInstanceModel method locateServiceTemplate.
private ServiceTemplate locateServiceTemplate(ModelController controller) {
ServiceTemplate tmpl;
try {
ApiConnector api = controller.getApiAccessor();
tmpl = (ServiceTemplate) api.findById(ServiceTemplate.class, _templateId);
} catch (IOException ex) {
s_logger.warn("service-template read", ex);
throw new CloudRuntimeException("Unable to create service-template object", ex);
}
if (tmpl == null) {
tmpl = new ServiceTemplate();
tmpl.setName(_templateName);
tmpl.setUuid(_templateId);
ServiceTemplateType props = new ServiceTemplateType("in-network", null, _templateUrl, false, null);
tmpl.setProperties(props);
try {
ApiConnector api = controller.getApiAccessor();
api.create(tmpl);
} catch (IOException ex) {
throw new CloudRuntimeException("Unable to create service-template object", ex);
}
}
return tmpl;
}
use of net.juniper.contrail.api.ApiConnector in project cloudstack by apache.
the class ServiceInstanceModel method build.
/**
* Recreate the model object from the Contrail API which is the master for this type of object.
* @param siObj
*/
public void build(ModelController controller, ServiceInstance siObj) {
ApiConnector api = controller.getApiAccessor();
_serviceInstance = siObj;
_fqName = StringUtils.join(siObj.getQualifiedName(), ':');
ServiceInstanceType props = siObj.getProperties();
// TODO: read management network names and cache network objects.
ObjectReference ref = siObj.getServiceTemplate().get(0);
if (ref != null) {
try {
ServiceTemplate tmpl = (ServiceTemplate) api.findById(ServiceTemplate.class, ref.getUuid());
_templateId = tmpl.getUuid();
} catch (IOException ex) {
s_logger.warn("service-template read", ex);
}
}
}
use of net.juniper.contrail.api.ApiConnector 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