Search in sources :

Example 26 with ApiConnector

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 ");
    }
}
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 27 with ApiConnector

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

Example 28 with ApiConnector

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;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ServiceTemplate(net.juniper.contrail.api.types.ServiceTemplate) ApiConnector(net.juniper.contrail.api.ApiConnector) ServiceTemplateType(net.juniper.contrail.api.types.ServiceTemplateType) IOException(java.io.IOException)

Example 29 with ApiConnector

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);
        }
    }
}
Also used : ServiceInstanceType(net.juniper.contrail.api.types.ServiceInstanceType) ObjectReference(net.juniper.contrail.api.ObjectReference) ApiConnector(net.juniper.contrail.api.ApiConnector) ServiceTemplate(net.juniper.contrail.api.types.ServiceTemplate) IOException(java.io.IOException)

Example 30 with ApiConnector

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");
    }
}
Also used : VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) ApiConnector(net.juniper.contrail.api.ApiConnector) MacAddressesType(net.juniper.contrail.api.types.MacAddressesType) ContrailManager(org.apache.cloudstack.network.contrail.management.ContrailManager) InternalErrorException(com.cloud.exception.InternalErrorException) VirtualMachineInterfacePropertiesType(net.juniper.contrail.api.types.VirtualMachineInterfacePropertiesType)

Aggregations

ApiConnector (net.juniper.contrail.api.ApiConnector)44 IOException (java.io.IOException)26 InternalErrorException (com.cloud.exception.InternalErrorException)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 ArrayList (java.util.ArrayList)8 NetworkVO (com.cloud.network.dao.NetworkVO)6 List (java.util.List)6 VMInstanceVO (com.cloud.vm.VMInstanceVO)5 ContrailManagerImpl (org.apache.cloudstack.network.contrail.management.ContrailManagerImpl)5 VlanDao (com.cloud.dc.dao.VlanDao)4 ApiConnectorMock (net.juniper.contrail.api.ApiConnectorMock)4 ObjectReference (net.juniper.contrail.api.ObjectReference)4 ServiceInstance (net.juniper.contrail.api.types.ServiceInstance)4 VirtualMachineInterface (net.juniper.contrail.api.types.VirtualMachineInterface)4 VirtualNetwork (net.juniper.contrail.api.types.VirtualNetwork)4 ContrailManager (org.apache.cloudstack.network.contrail.management.ContrailManager)4 Test (org.junit.Test)4 VlanVO (com.cloud.dc.VlanVO)3 NicVO (com.cloud.vm.NicVO)3 UserVmDao (com.cloud.vm.dao.UserVmDao)3