Search in sources :

Example 1 with VirtualMachineModel

use of org.apache.cloudstack.network.contrail.model.VirtualMachineModel in project cloudstack by apache.

the class ServiceManagerImpl method createServiceInstance.

@Override
public ServiceVirtualMachine createServiceInstance(DataCenter zone, Account owner, VirtualMachineTemplate template, ServiceOffering serviceOffering, String name, Network left, Network right) {
    s_logger.debug("createServiceInstance by " + owner.getAccountName());
    // service instances need to be able to access the public network.
    if (left.getTrafficType() == TrafficType.Guest) {
        _networkModel.checkNetworkPermissions(owner, left);
    }
    if (right.getTrafficType() == TrafficType.Guest) {
        _networkModel.checkNetworkPermissions(owner, right);
    }
    final ApiConnector api = _manager.getApiConnector();
    VirtualNetworkModel leftModel = _manager.getDatabase().lookupVirtualNetwork(left.getUuid(), _manager.getCanonicalName(left), left.getTrafficType());
    if (leftModel == null) {
        throw new CloudRuntimeException("Unable to read virtual-network object");
    }
    VirtualNetworkModel rightModel = _manager.getDatabase().lookupVirtualNetwork(right.getUuid(), _manager.getCanonicalName(right), right.getTrafficType());
    if (rightModel == null) {
        throw new CloudRuntimeException("Unable to read virtual-network object");
    }
    net.juniper.contrail.api.types.Project project;
    try {
        project = _manager.getVncProject(owner.getDomainId(), owner.getAccountId());
    } catch (IOException ex) {
        s_logger.warn("read project", ex);
        throw new CloudRuntimeException(ex);
    }
    try {
        final String srvid = api.findByName(ServiceInstance.class, project, name);
        if (srvid != null) {
            throw new InvalidParameterValueException("service-instance " + name + " already exists uuid=" + srvid);
        }
    } catch (IOException ex) {
        s_logger.warn("service-instance lookup", ex);
        throw new CloudRuntimeException(ex);
    }
    // 1. Create service-instance.
    ServiceInstanceModel serviceModel = new ServiceInstanceModel(project, name, template, serviceOffering, leftModel, rightModel);
    try {
        serviceModel.update(_manager.getModelController());
    } catch (Exception ex) {
        s_logger.warn("service-instance update", ex);
        throw new CloudRuntimeException(ex);
    }
    s_logger.debug("service-instance object created");
    ServiceInstance siObj;
    try {
        _manager.getDatabase().getServiceInstances().add(serviceModel);
        siObj = serviceModel.getServiceInstance();
    } catch (Exception ex) {
        s_logger.warn("DB add", ex);
        throw new CloudRuntimeException(ex);
    }
    // 2. Create one virtual-machine.
    String svmName = name.replace(" ", "_") + "-1";
    ServiceVirtualMachine svm = createServiceVM(zone, owner, template, serviceOffering, svmName, siObj, left, right);
    s_logger.debug("created VMInstance " + svm.getUuid());
    // 3. Create the virtual-machine model and push the update.
    VirtualMachineModel instanceModel = new VirtualMachineModel(svm, svm.getUuid());
    _manager.getDatabase().getVirtualMachines().add(instanceModel);
    try {
        instanceModel.setServiceInstance(_manager.getModelController(), svm, serviceModel);
        instanceModel.update(_manager.getModelController());
    } catch (Exception ex) {
        s_logger.warn("service virtual-machine update", ex);
        throw new CloudRuntimeException(ex);
    }
    return svm;
}
Also used : ServiceInstanceModel(org.apache.cloudstack.network.contrail.model.ServiceInstanceModel) ApiConnector(net.juniper.contrail.api.ApiConnector) VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) ServiceInstance(net.juniper.contrail.api.types.ServiceInstance) IOException(java.io.IOException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 2 with VirtualMachineModel

use of org.apache.cloudstack.network.contrail.model.VirtualMachineModel 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 3 with VirtualMachineModel

use of org.apache.cloudstack.network.contrail.model.VirtualMachineModel in project cloudstack by apache.

the class ModelDatabase method lookupVirtualMachine.

public VirtualMachineModel lookupVirtualMachine(String uuid) {
    VirtualMachineModel vmKey = new VirtualMachineModel(null, uuid);
    VirtualMachineModel current = _vmTable.ceiling(vmKey);
    if (current != null && current.getUuid().equals(uuid)) {
        return current;
    }
    return null;
}
Also used : VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel)

Example 4 with VirtualMachineModel

use of org.apache.cloudstack.network.contrail.model.VirtualMachineModel in project cloudstack by apache.

the class ContrailGuru method deallocate.

/**
 * Release permanent resources of a Nic (VMI and addresses).
 */
@Override
public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) {
    s_logger.debug("deallocate NicProfile " + nic.getId() + " on " + network.getName());
    NicVO nicVO = _nicDao.findById(nic.getId());
    assert nicVO != null;
    VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
    if (vmModel == null) {
        return;
    }
    VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
    if (vmiModel == null) {
        return;
    }
    try {
        vmiModel.destroy(_manager.getModelController());
    } catch (IOException ex) {
        return;
    }
    vmModel.removeSuccessor(vmiModel);
    if (!vmModel.hasDescendents()) {
        _manager.getDatabase().getVirtualMachines().remove(vmModel);
        try {
            vmModel.delete(_manager.getModelController());
        } catch (IOException ex) {
            s_logger.warn("virtual-machine delete", ex);
            return;
        }
    }
}
Also used : VMInterfaceModel(org.apache.cloudstack.network.contrail.model.VMInterfaceModel) VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) IOException(java.io.IOException) NicVO(com.cloud.vm.NicVO)

Example 5 with VirtualMachineModel

use of org.apache.cloudstack.network.contrail.model.VirtualMachineModel in project cloudstack by apache.

the class ServerDBSyncImpl method equalVirtualMachine.

public Boolean equalVirtualMachine(VMInstanceVO dbVm, VirtualMachine vncVm, StringBuffer syncLogMsg) {
    syncLogMsg.append("VM# DB: " + dbVm.getInstanceName() + "/" + dbVm.getUuid() + "; VNC: " + vncVm.getUuid() + "; action: equal; DB VM State: " + dbVm.getState() + "\n");
    VirtualMachineModel vmModel = new VirtualMachineModel(dbVm, dbVm.getUuid());
    vmModel.build(_manager.getModelController(), dbVm);
    if (vmModel.isActive()) {
        try {
            buildNicResources(vmModel, dbVm, syncLogMsg);
        } catch (IOException ex) {
            s_logger.warn("build nic information for " + dbVm.getInstanceName(), ex);
        }
    }
    VirtualMachineModel current = _manager.getDatabase().lookupVirtualMachine(vncVm.getUuid());
    if (_rwMode) {
        if (current != null) {
            _manager.getDatabase().getVirtualMachines().remove(current);
        }
        _manager.getDatabase().getVirtualMachines().add(vmModel);
        try {
            vmModel.update(_manager.getModelController());
        } catch (Exception ex) {
            s_logger.warn("update virtual-machine", ex);
        }
    } else {
        // compare
        if (current != null && current.compare(_manager.getModelController(), vmModel) == false) {
            syncLogMsg.append("VM # DB: " + dbVm.getInstanceName() + "; VNC: " + vncVm.getName() + "; attributes differ\n");
            return false;
        }
    }
    return true;
}
Also used : VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException)

Aggregations

VirtualMachineModel (org.apache.cloudstack.network.contrail.model.VirtualMachineModel)8 IOException (java.io.IOException)6 NicVO (com.cloud.vm.NicVO)4 VMInterfaceModel (org.apache.cloudstack.network.contrail.model.VMInterfaceModel)4 VirtualNetworkModel (org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)3 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)2 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2 InstanceIpModel (org.apache.cloudstack.network.contrail.model.InstanceIpModel)2 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 ApiConnector (net.juniper.contrail.api.ApiConnector)1 MacAddressesType (net.juniper.contrail.api.types.MacAddressesType)1 ServiceInstance (net.juniper.contrail.api.types.ServiceInstance)1 VirtualMachineInterface (net.juniper.contrail.api.types.VirtualMachineInterface)1 ServiceInstanceModel (org.apache.cloudstack.network.contrail.model.ServiceInstanceModel)1