Search in sources :

Example 1 with VirtualNetworkModel

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

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

the class ServerDBSyncImpl method equalFloatingIp.

public Boolean equalFloatingIp(IPAddressVO db, FloatingIp vnc, StringBuffer syncLogMsg) throws IOException {
    syncLogMsg.append("fip# DB: " + db.getAddress().addr() + "; VNC: " + vnc.getAddress() + "; action: equal" + "\n");
    VirtualNetworkModel vnModel = _manager.lookupPublicNetworkModel();
    assert vnModel != null : "public network vn model is null";
    FloatingIpPoolModel fipPoolModel = vnModel.getFipPoolModel();
    if (fipPoolModel == null) {
        fipPoolModel = new FloatingIpPoolModel();
        fipPoolModel.addToVirtualNetwork(vnModel);
        fipPoolModel.build(_manager.getModelController());
        try {
            fipPoolModel.update(_manager.getModelController());
            vnModel.setFipPoolModel(fipPoolModel);
        } catch (Exception ex) {
            s_logger.warn("floating-ip-pool create: ", ex);
            return false;
        }
    }
    FloatingIpModel current = fipPoolModel.getFloatingIpModel(db.getUuid());
    if (current == null) {
        s_logger.debug("add model " + db.getAddress().addr());
        FloatingIpModel fipModel = new FloatingIpModel(db.getUuid());
        fipModel.addToFloatingIpPool(fipPoolModel);
        fipModel.build(_manager.getModelController(), PublicIp.createFromAddrAndVlan(db, _vlanDao.findById(db.getVlanId())));
        try {
            fipModel.update(_manager.getModelController());
        } catch (Exception ex) {
            s_logger.warn("floating-ip create: ", ex);
            return false;
        }
    }
    return true;
}
Also used : FloatingIpPoolModel(org.apache.cloudstack.network.contrail.model.FloatingIpPoolModel) FloatingIpModel(org.apache.cloudstack.network.contrail.model.FloatingIpModel) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Example 3 with VirtualNetworkModel

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

the class ServerDBSyncImpl method createVirtualNetwork.

public void createVirtualNetwork(NetworkVO dbNet, StringBuffer syncLogMesg) throws IOException {
    syncLogMesg.append("VN# DB: " + _manager.getCanonicalName(dbNet) + "(" + dbNet.getUuid() + "); VNC: none;  action: create\n");
    if (_manager.getDatabase().lookupVirtualNetwork(dbNet.getUuid(), _manager.getCanonicalName(dbNet), dbNet.getTrafficType()) != null) {
        s_logger.warn("VN model object is already present in DB: " + dbNet.getUuid() + ", name: " + dbNet.getName());
    }
    VirtualNetworkModel vnModel = new VirtualNetworkModel(dbNet, dbNet.getUuid(), _manager.getCanonicalName(dbNet), dbNet.getTrafficType());
    if (dbNet.getTrafficType() == TrafficType.Guest && dbNet.getNetworkACLId() != null) {
        NetworkACLVO acl = _networkACLDao.findById(dbNet.getNetworkACLId());
        NetworkPolicyModel policyModel = _manager.getDatabase().lookupNetworkPolicy(acl.getUuid());
        if (policyModel == null) {
            s_logger.error("Network(" + dbNet.getName() + ") has ACL but policy model not created: " + acl.getUuid() + ", name: " + acl.getName());
        } else {
            vnModel.addToNetworkPolicy(policyModel);
        }
    }
    vnModel.build(_manager.getModelController(), dbNet);
    if (_rwMode) {
        try {
            if (!vnModel.verify(_manager.getModelController())) {
                vnModel.update(_manager.getModelController());
            }
        } catch (InternalErrorException ex) {
            s_logger.warn("create virtual-network", ex);
            syncLogMesg.append("Error: VN# VNC : Unable to create network " + dbNet.getName() + "\n");
            return;
        }
        s_logger.debug("add model " + vnModel.getName());
        _manager.getDatabase().getVirtualNetworks().add(vnModel);
        syncLogMesg.append("VN# VNC: " + dbNet.getUuid() + ", " + vnModel.getName() + " created\n");
    } else {
        syncLogMesg.append("VN# VNC: " + vnModel.getName() + " created \n");
    }
}
Also used : NetworkACLVO(com.cloud.network.vpc.NetworkACLVO) NetworkPolicyModel(org.apache.cloudstack.network.contrail.model.NetworkPolicyModel) InternalErrorException(com.cloud.exception.InternalErrorException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Example 4 with VirtualNetworkModel

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

the class ContrailManagerImpl method createFloatingIp.

@Override
public boolean createFloatingIp(PublicIpAddress ip) {
    VirtualNetworkModel vnModel = lookupPublicNetworkModel();
    assert vnModel != null : "public network vn model is null";
    FloatingIpPoolModel fipPoolModel = vnModel.getFipPoolModel();
    /* create only, no updates */
    if (fipPoolModel == null) {
        fipPoolModel = new FloatingIpPoolModel();
        fipPoolModel.addToVirtualNetwork(vnModel);
        fipPoolModel.build(getModelController());
        try {
            fipPoolModel.update(getModelController());
            vnModel.setFipPoolModel(fipPoolModel);
        } catch (Exception ex) {
            s_logger.warn("floating-ip-pool create: ", ex);
            return false;
        }
    }
    FloatingIpModel fipModel = fipPoolModel.getFloatingIpModel(ip.getUuid());
    /* create only, no updates*/
    if (fipModel == null) {
        fipModel = new FloatingIpModel(ip.getUuid());
        fipModel.addToFloatingIpPool(fipPoolModel);
        fipModel.build(getModelController(), ip);
        try {
            fipModel.update(getModelController());
        } catch (Exception ex) {
            s_logger.warn("floating-ip create: ", ex);
            return false;
        }
    }
    return true;
}
Also used : FloatingIpPoolModel(org.apache.cloudstack.network.contrail.model.FloatingIpPoolModel) FloatingIpModel(org.apache.cloudstack.network.contrail.model.FloatingIpModel) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Example 5 with VirtualNetworkModel

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

the class ContrailVpcElementImpl method applyNetworkACLs.

@Override
public boolean applyNetworkACLs(Network net, List<? extends NetworkACLItem> rules) throws ResourceUnavailableException {
    s_logger.debug("NetworkElement applyNetworkACLs");
    if (rules == null || rules.isEmpty()) {
        s_logger.debug("no rules to apply");
        return true;
    }
    Long aclId = rules.get(0).getAclId();
    NetworkACLVO acl = _networkACLDao.findById(aclId);
    NetworkPolicyModel policyModel = _manager.getDatabase().lookupNetworkPolicy(acl.getUuid());
    if (policyModel == null) {
        /*
             * For the first time, when a CS ACL applied to a network, create a network-policy in VNC
             * and when there are no networks associated to CS ACL, delete it from VNC.
             */
        policyModel = new NetworkPolicyModel(acl.getUuid(), acl.getName());
        net.juniper.contrail.api.types.Project project;
        try {
            project = _manager.getVncProject(net.getDomainId(), net.getAccountId());
            if (project == null) {
                project = _manager.getDefaultVncProject();
            }
        } catch (IOException ex) {
            s_logger.warn("read project", ex);
            return false;
        }
        policyModel.setProject(project);
    }
    VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(net.getUuid(), _manager.getCanonicalName(net), net.getTrafficType());
    NetworkPolicyModel oldPolicyModel = null;
    /* this method is called when network is destroyed too, hence vn model might have been deleted already */
    if (vnModel != null) {
        oldPolicyModel = vnModel.getNetworkPolicyModel();
        vnModel.addToNetworkPolicy(policyModel);
    }
    try {
        policyModel.build(_manager.getModelController(), rules);
    } catch (Exception e) {
        s_logger.error(e);
        e.printStackTrace();
        return false;
    }
    try {
        if (!policyModel.verify(_manager.getModelController())) {
            policyModel.update(_manager.getModelController());
        }
        _manager.getDatabase().getNetworkPolicys().add(policyModel);
    } catch (Exception ex) {
        s_logger.error("network-policy update: ", ex);
        ex.printStackTrace();
        return false;
    }
    if (!policyModel.hasPolicyRules()) {
        try {
            policyModel.delete(_manager.getModelController());
            _manager.getDatabase().getNetworkPolicys().remove(policyModel);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /*
         * if no other VNs are associated with the old policy,
         * we could delete it from the Contrail VNC
         */
    if (policyModel != oldPolicyModel && oldPolicyModel != null && !oldPolicyModel.hasDescendents()) {
        try {
            oldPolicyModel.delete(_manager.getModelController());
            _manager.getDatabase().getNetworkPolicys().remove(oldPolicyModel);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}
Also used : NetworkACLVO(com.cloud.network.vpc.NetworkACLVO) NetworkPolicyModel(org.apache.cloudstack.network.contrail.model.NetworkPolicyModel) IOException(java.io.IOException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IOException(java.io.IOException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Aggregations

VirtualNetworkModel (org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)16 IOException (java.io.IOException)13 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 FloatingIpPoolModel (org.apache.cloudstack.network.contrail.model.FloatingIpPoolModel)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)3 NetworkVO (com.cloud.network.dao.NetworkVO)3 NetworkACLVO (com.cloud.network.vpc.NetworkACLVO)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 NicVO (com.cloud.vm.NicVO)3 ConfigurationException (javax.naming.ConfigurationException)3 FloatingIpModel (org.apache.cloudstack.network.contrail.model.FloatingIpModel)3 NetworkPolicyModel (org.apache.cloudstack.network.contrail.model.NetworkPolicyModel)3 VMInterfaceModel (org.apache.cloudstack.network.contrail.model.VMInterfaceModel)3 VirtualMachineModel (org.apache.cloudstack.network.contrail.model.VirtualMachineModel)3 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)2 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)2 TrafficType (com.cloud.network.Networks.TrafficType)2 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)2