Search in sources :

Example 1 with MacAddressesType

use of net.juniper.contrail.api.types.MacAddressesType 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);
    }
}
Also used : VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) VMInterfaceModel(org.apache.cloudstack.network.contrail.model.VMInterfaceModel) VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) MacAddressesType(net.juniper.contrail.api.types.MacAddressesType) VMInstanceVO(com.cloud.vm.VMInstanceVO) IOException(java.io.IOException) NicVO(com.cloud.vm.NicVO) InstanceIpModel(org.apache.cloudstack.network.contrail.model.InstanceIpModel) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Example 2 with MacAddressesType

use of net.juniper.contrail.api.types.MacAddressesType 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

MacAddressesType (net.juniper.contrail.api.types.MacAddressesType)2 VirtualMachineInterface (net.juniper.contrail.api.types.VirtualMachineInterface)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 NicVO (com.cloud.vm.NicVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 IOException (java.io.IOException)1 ApiConnector (net.juniper.contrail.api.ApiConnector)1 VirtualMachineInterfacePropertiesType (net.juniper.contrail.api.types.VirtualMachineInterfacePropertiesType)1 ContrailManager (org.apache.cloudstack.network.contrail.management.ContrailManager)1 InstanceIpModel (org.apache.cloudstack.network.contrail.model.InstanceIpModel)1 VMInterfaceModel (org.apache.cloudstack.network.contrail.model.VMInterfaceModel)1 VirtualMachineModel (org.apache.cloudstack.network.contrail.model.VirtualMachineModel)1 VirtualNetworkModel (org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)1