Search in sources :

Example 1 with NetworkPolicyModel

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

the class ServerDBSyncImpl method equalNetworkPolicy.

public Boolean equalNetworkPolicy(NetworkACLVO db, NetworkPolicy policy, StringBuffer syncLogMesg) {
    syncLogMesg.append("Policy# DB: " + db.getName() + "; VNC: " + policy.getName() + "; action: equal\n");
    NetworkPolicyModel current = _manager.getDatabase().lookupNetworkPolicy(policy.getUuid());
    NetworkPolicyModel policyModel = new NetworkPolicyModel(db.getUuid(), db.getName());
    net.juniper.contrail.api.types.Project project = null;
    try {
        VpcVO vpc = _vpcDao.findById(db.getVpcId());
        if (vpc != null) {
            project = _manager.getVncProject(vpc.getDomainId(), vpc.getAccountId());
        } else {
            project = _manager.getDefaultVncProject();
        }
    } catch (IOException ex) {
        s_logger.warn("read project", ex);
    }
    policyModel.setProject(project);
    List<NetworkACLItemVO> rules = _networkACLItemDao.listByACL(db.getId());
    try {
        policyModel.build(_manager.getModelController(), rules);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (_rwMode) {
        if (current != null) {
            _manager.getDatabase().getNetworkPolicys().remove(current);
        }
        s_logger.debug("add policy model " + policyModel.getName());
        _manager.getDatabase().getNetworkPolicys().add(policyModel);
        try {
            if (!policyModel.verify(_manager.getModelController())) {
                policyModel.update(_manager.getModelController());
            }
        } catch (Exception ex) {
            s_logger.warn("update network-policy", ex);
        }
    } else {
        //compare
        if (current != null && current.compare(_manager.getModelController(), policyModel) == false) {
            syncLogMesg.append("Policy# DB: " + db.getName() + "; VNC: " + policy.getName() + "; attributes differ\n");
            return false;
        }
    }
    return true;
}
Also used : VpcVO(com.cloud.network.vpc.VpcVO) NetworkPolicyModel(org.apache.cloudstack.network.contrail.model.NetworkPolicyModel) IOException(java.io.IOException) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException)

Example 2 with NetworkPolicyModel

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

the class ServerDBSyncImpl method createNetworkPolicy.

public void createNetworkPolicy(NetworkACLVO db, StringBuffer syncLogMesg) throws IOException {
    syncLogMesg.append("Policy# DB: " + db.getName() + "(" + db.getUuid() + "); VNC: none;  action: create\n");
    if (_manager.getDatabase().lookupNetworkPolicy(db.getUuid()) != null) {
        s_logger.warn("Policy model object is already present in DB: " + db.getUuid() + ", name: " + db.getName());
    }
    NetworkPolicyModel policyModel = new NetworkPolicyModel(db.getUuid(), db.getName());
    net.juniper.contrail.api.types.Project project = null;
    try {
        VpcVO vpc = _vpcDao.findById(db.getVpcId());
        if (vpc != null) {
            project = _manager.getVncProject(vpc.getDomainId(), vpc.getAccountId());
        } else {
            project = _manager.getDefaultVncProject();
        }
    } catch (IOException ex) {
        s_logger.warn("read project", ex);
        throw ex;
    }
    policyModel.setProject(project);
    List<NetworkACLItemVO> rules = _networkACLItemDao.listByACL(db.getId());
    try {
        policyModel.build(_manager.getModelController(), rules);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (_rwMode) {
        try {
            if (!policyModel.verify(_manager.getModelController())) {
                policyModel.update(_manager.getModelController());
            }
        } catch (Exception ex) {
            s_logger.warn("create network-policy", ex);
            syncLogMesg.append("Error: Policy# VNC : Unable to create network policy " + db.getName() + "\n");
            return;
        }
        s_logger.debug("add model " + policyModel.getName());
        _manager.getDatabase().getNetworkPolicys().add(policyModel);
        syncLogMesg.append("Policy# VNC: " + db.getUuid() + ", " + policyModel.getName() + " created\n");
    } else {
        syncLogMesg.append("Policy# VNC: " + policyModel.getName() + " created \n");
    }
}
Also used : VpcVO(com.cloud.network.vpc.VpcVO) NetworkPolicyModel(org.apache.cloudstack.network.contrail.model.NetworkPolicyModel) IOException(java.io.IOException) NetworkACLItemVO(com.cloud.network.vpc.NetworkACLItemVO) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException)

Example 3 with NetworkPolicyModel

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

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

Example 5 with NetworkPolicyModel

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

the class ModelDatabase method lookupNetworkPolicy.

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

Aggregations

NetworkPolicyModel (org.apache.cloudstack.network.contrail.model.NetworkPolicyModel)6 InternalErrorException (com.cloud.exception.InternalErrorException)4 IOException (java.io.IOException)4 NetworkACLVO (com.cloud.network.vpc.NetworkACLVO)3 VirtualNetworkModel (org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)3 NetworkACLItemVO (com.cloud.network.vpc.NetworkACLItemVO)2 VpcVO (com.cloud.network.vpc.VpcVO)2 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 FloatingIpPoolModel (org.apache.cloudstack.network.contrail.model.FloatingIpPoolModel)1