Search in sources :

Example 1 with VnSubnetsType

use of net.juniper.contrail.api.types.VnSubnetsType in project cloudstack by apache.

the class VirtualNetworkModel method verify.

@Override
public boolean verify(ModelController controller) {
    assert _initialized : "initialized is false";
    assert _uuid != null : "uuid is not set";
    ApiConnector api = controller.getApiAccessor();
    VlanDao vlanDao = controller.getVlanDao();
    try {
        _vn = (VirtualNetwork) api.findById(VirtualNetwork.class, _uuid);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (_vn == null) {
        return false;
    }
    if (!isDynamicNetwork()) {
        return true;
    }
    List<String> dbSubnets = new ArrayList<String>();
    if (_trafficType == TrafficType.Public) {
        List<VlanVO> vlan_list = vlanDao.listVlansByNetworkId(_id);
        for (VlanVO vlan : vlan_list) {
            String cidr = NetUtils.ipAndNetMaskToCidr(vlan.getVlanGateway(), vlan.getVlanNetmask());
            dbSubnets.add(vlan.getVlanGateway() + cidr);
        }
    } else {
        dbSubnets.add(_gateway + _prefix);
    }
    List<ObjectReference<VnSubnetsType>> ipamRefs = _vn.getNetworkIpam();
    List<String> vncSubnets = new ArrayList<String>();
    if (ipamRefs == null && !dbSubnets.isEmpty()) {
        return false;
    }
    if (ipamRefs != null) {
        for (ObjectReference<VnSubnetsType> ref : ipamRefs) {
            VnSubnetsType vnSubnetType = ref.getAttr();
            if (vnSubnetType != null) {
                List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetType.getIpamSubnets();
                if (subnets != null && !subnets.isEmpty()) {
                    VnSubnetsType.IpamSubnetType ipamSubnet = subnets.get(0);
                    vncSubnets.add(ipamSubnet.getDefaultGateway() + ipamSubnet.getSubnet().getIpPrefix() + "/" + ipamSubnet.getSubnet().getIpPrefixLen());
                }
            }
        }
    }
    // unordered, no duplicates hence perform negation operation as set
    Set<String> diff = new HashSet<String>(dbSubnets);
    diff.removeAll(vncSubnets);
    if (!diff.isEmpty()) {
        s_logger.debug("Subnets changed, network: " + _name + "; db: " + dbSubnets + ", vnc: " + vncSubnets + ", diff: " + diff);
        return false;
    }
    List<ObjectReference<VirtualNetworkPolicyType>> policyRefs = _vn.getNetworkPolicy();
    if ((policyRefs == null || policyRefs.isEmpty()) && _policyModel != null) {
        return false;
    }
    if ((policyRefs != null && !policyRefs.isEmpty()) && _policyModel == null) {
        return false;
    }
    if (policyRefs != null && !policyRefs.isEmpty() && _policyModel != null) {
        ObjectReference<VirtualNetworkPolicyType> ref = policyRefs.get(0);
        if (!ref.getUuid().equals(_policyModel.getUuid())) {
            return false;
        }
    }
    for (ModelObject successor : successors()) {
        if (!successor.verify(controller)) {
            return false;
        }
    }
    return true;
}
Also used : VirtualNetworkPolicyType(net.juniper.contrail.api.types.VirtualNetworkPolicyType) VlanDao(com.cloud.dc.dao.VlanDao) ApiConnector(net.juniper.contrail.api.ApiConnector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectReference(net.juniper.contrail.api.ObjectReference) VlanVO(com.cloud.dc.VlanVO) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType) HashSet(java.util.HashSet)

Example 2 with VnSubnetsType

use of net.juniper.contrail.api.types.VnSubnetsType in project cloudstack by apache.

the class VirtualNetworkModel method update.

@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
    assert _initialized;
    ApiConnector api = controller.getApiAccessor();
    VlanDao vlanDao = controller.getVlanDao();
    VirtualNetwork vn = _vn;
    if (!isDynamicNetwork()) {
        _vn = (VirtualNetwork) controller.getApiAccessor().findById(VirtualNetwork.class, _uuid);
        return;
    }
    assert _uuid != null : "uuid is not set";
    if (_vn == null) {
        vn = _vn = (VirtualNetwork) controller.getApiAccessor().findById(VirtualNetwork.class, _uuid);
        if (vn == null) {
            vn = new VirtualNetwork();
            if (_projectId != null) {
                Project project;
                try {
                    project = (Project) api.findById(Project.class, _projectId);
                } catch (IOException ex) {
                    s_logger.debug("project read", ex);
                    throw new CloudRuntimeException("Failed to read project", ex);
                }
                vn.setParent(project);
            }
            vn.setName(_name);
            vn.setUuid(_uuid);
        }
    }
    if (_policyModel == null) {
        vn.clearNetworkPolicy();
    } else if (!_policyModel.hasPolicyRules()) {
        vn.clearNetworkPolicy();
        _policyModel.removeSuccessor(this);
    } else {
        vn.setNetworkPolicy(_policyModel.getPolicy(), new VirtualNetworkPolicyType(new VirtualNetworkPolicyType.SequenceType(1, 0), null));
    }
    if (_ipam == null) {
        NetworkIpam ipam = null;
        try {
            String ipam_id = api.findByName(NetworkIpam.class, null, "default-network-ipam");
            if (ipam_id == null) {
                s_logger.debug("could not find default-network-ipam");
                return;
            }
            ipam = (NetworkIpam) api.findById(NetworkIpam.class, ipam_id);
            if (ipam == null) {
                s_logger.debug("could not find NetworkIpam with ipam_id: " + ipam_id);
                return;
            }
        } catch (IOException ex) {
            s_logger.error(ex);
            return;
        }
        _ipam = ipam;
    }
    if (_prefix != null) {
        VnSubnetsType subnet = new VnSubnetsType();
        String[] addr_pair = _prefix.split("\\/");
        subnet.addIpamSubnets(new SubnetType(addr_pair[0], Integer.parseInt(addr_pair[1])), _gateway);
        vn.setNetworkIpam(_ipam, subnet);
    } else if (_trafficType == TrafficType.Public) {
        vn.clearNetworkIpam();
        /* Subnet information for Public is stored in the vlan table */
        List<VlanVO> vlan_list = vlanDao.listVlansByNetworkId(_id);
        for (VlanVO vlan : vlan_list) {
            String cidr = NetUtils.ipAndNetMaskToCidr(vlan.getVlanGateway(), vlan.getVlanNetmask());
            int slash = cidr.indexOf('/');
            String ip_addr = cidr.substring(0, slash);
            int plen = Integer.parseInt(cidr.substring(slash + 1));
            VnSubnetsType subnet = new VnSubnetsType();
            subnet.addIpamSubnets(new SubnetType(ip_addr, plen), vlan.getVlanGateway());
            vn.addNetworkIpam(_ipam, subnet);
        }
    }
    if (_vn == null) {
        try {
            api.create(vn);
        } catch (Exception ex) {
            s_logger.debug("virtual-network create", ex);
            throw new CloudRuntimeException("Failed to create virtual-network", ex);
        }
        _vn = vn;
    } else {
        try {
            api.update(vn);
        } catch (IOException ex) {
            s_logger.warn("virtual-network update", ex);
            throw new CloudRuntimeException("Unable to update virtual-network object", ex);
        }
    }
    for (ModelObject successor : successors()) {
        successor.update(controller);
    }
}
Also used : VirtualNetworkPolicyType(net.juniper.contrail.api.types.VirtualNetworkPolicyType) VlanDao(com.cloud.dc.dao.VlanDao) ApiConnector(net.juniper.contrail.api.ApiConnector) IOException(java.io.IOException) SubnetType(net.juniper.contrail.api.types.SubnetType) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Project(net.juniper.contrail.api.types.Project) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkIpam(net.juniper.contrail.api.types.NetworkIpam) ArrayList(java.util.ArrayList) List(java.util.List) VlanVO(com.cloud.dc.VlanVO) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType)

Example 3 with VnSubnetsType

use of net.juniper.contrail.api.types.VnSubnetsType in project cloudstack by apache.

the class VirtualNetworkModel method read.

public void read(ModelController controller) {
    ApiConnector api = controller.getApiAccessor();
    VlanDao vlanDao = controller.getVlanDao();
    try {
        _vn = (VirtualNetwork) api.findById(VirtualNetwork.class, _uuid);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (_vn == null) {
        return;
    }
    if (_ipam == null) {
        NetworkIpam ipam = null;
        try {
            String ipam_id = api.findByName(NetworkIpam.class, null, "default-network-ipam");
            if (ipam_id == null) {
                s_logger.debug("could not find default-network-ipam");
                return;
            }
            ipam = (NetworkIpam) api.findById(NetworkIpam.class, ipam_id);
            if (ipam == null) {
                s_logger.debug("could not find NetworkIpam with ipam_id: " + ipam_id);
                return;
            }
        } catch (IOException ex) {
            s_logger.error(ex);
            return;
        }
        _ipam = ipam;
    }
    if (_prefix != null) {
        VnSubnetsType subnet = new VnSubnetsType();
        String[] addr_pair = _prefix.split("\\/");
        subnet.addIpamSubnets(new SubnetType(addr_pair[0], Integer.parseInt(addr_pair[1])), _gateway);
        _vn.setNetworkIpam(_ipam, subnet);
    } else if (_trafficType == TrafficType.Public) {
        _vn.clearNetworkIpam();
        /* Subnet information for Public is stored in the vlan table */
        List<VlanVO> vlan_list = vlanDao.listVlansByNetworkId(_id);
        for (VlanVO vlan : vlan_list) {
            String cidr = NetUtils.ipAndNetMaskToCidr(vlan.getVlanGateway(), vlan.getVlanNetmask());
            int slash = cidr.indexOf('/');
            String ip_addr = cidr.substring(0, slash);
            int plen = Integer.parseInt(cidr.substring(slash + 1));
            VnSubnetsType subnet = new VnSubnetsType();
            subnet.addIpamSubnets(new SubnetType(ip_addr, plen), vlan.getVlanGateway());
            _vn.addNetworkIpam(_ipam, subnet);
        }
    }
    return;
}
Also used : VlanDao(com.cloud.dc.dao.VlanDao) ApiConnector(net.juniper.contrail.api.ApiConnector) NetworkIpam(net.juniper.contrail.api.types.NetworkIpam) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) SubnetType(net.juniper.contrail.api.types.SubnetType) VlanVO(com.cloud.dc.VlanVO) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType)

Example 4 with VnSubnetsType

use of net.juniper.contrail.api.types.VnSubnetsType in project cloudstack by apache.

the class VirtualNetworkModel method compare.

@Override
public boolean compare(ModelController controller, ModelObject o) {
    VirtualNetworkModel latest;
    assert _vn != null : "vnc virtual network current is not initialized";
    try {
        latest = (VirtualNetworkModel) o;
    } catch (ClassCastException ex) {
        s_logger.warn("Invalid model object is passed to cast to VirtualNetworkModel");
        return false;
    }
    try {
        latest.read(controller);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    assert latest._vn != null : "vnc virtual network new is not initialized";
    List<ObjectReference<VnSubnetsType>> currentIpamRefs = _vn.getNetworkIpam();
    List<ObjectReference<VnSubnetsType>> newIpamRefs = latest._vn.getNetworkIpam();
    List<String> currentSubnets = new ArrayList<String>();
    List<String> newSubnets = new ArrayList<String>();
    if ((currentIpamRefs == null && newIpamRefs != null) || (currentIpamRefs != null && newIpamRefs == null)) {
        // Check for existence only
        s_logger.debug("ipams differ: current=" + currentIpamRefs + ", new=" + newIpamRefs);
        return false;
    }
    if (currentIpamRefs == null) {
        return true;
    }
    for (ObjectReference<VnSubnetsType> ref : currentIpamRefs) {
        VnSubnetsType vnSubnetType = ref.getAttr();
        if (vnSubnetType != null) {
            List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetType.getIpamSubnets();
            if (subnets != null && !subnets.isEmpty()) {
                VnSubnetsType.IpamSubnetType ipamSubnet = subnets.get(0);
                currentSubnets.add(ipamSubnet.getDefaultGateway() + ipamSubnet.getSubnet().getIpPrefix() + "/" + ipamSubnet.getSubnet().getIpPrefixLen());
            }
        }
    }
    for (ObjectReference<VnSubnetsType> ref : newIpamRefs) {
        VnSubnetsType vnSubnetType = ref.getAttr();
        if (vnSubnetType != null) {
            List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetType.getIpamSubnets();
            if (subnets != null && !subnets.isEmpty()) {
                VnSubnetsType.IpamSubnetType ipamSubnet = subnets.get(0);
                newSubnets.add(ipamSubnet.getDefaultGateway() + ipamSubnet.getSubnet().getIpPrefix() + "/" + ipamSubnet.getSubnet().getIpPrefixLen());
            }
        }
    }
    Set<String> diff = new HashSet<String>(currentSubnets);
    diff.removeAll(newSubnets);
    if (!diff.isEmpty()) {
        s_logger.debug("Subnets differ, network: " + _name + "; db: " + currentSubnets + ", vnc: " + newSubnets + ", diff: " + diff);
        return false;
    }
    List<ObjectReference<VirtualNetworkPolicyType>> currentPolicyRefs = _vn.getNetworkPolicy();
    List<ObjectReference<VirtualNetworkPolicyType>> latestPolicyRefs = latest._vn.getNetworkPolicy();
    if (currentPolicyRefs == null && latestPolicyRefs == null) {
        return true;
    }
    if ((currentPolicyRefs == null && latestPolicyRefs != null) || (currentPolicyRefs != null && latestPolicyRefs == null)) {
        return false;
    }
    if ((currentPolicyRefs != null && latestPolicyRefs != null) && (currentPolicyRefs.size() != latestPolicyRefs.size())) {
        return false;
    }
    if ((currentPolicyRefs != null && latestPolicyRefs != null) && currentPolicyRefs.isEmpty() && latestPolicyRefs.isEmpty()) {
        return true;
    }
    // both must be non empty lists
    ObjectReference<VirtualNetworkPolicyType> ref1 = null;
    if (currentPolicyRefs != null) {
        ref1 = currentPolicyRefs.get(0);
    }
    ObjectReference<VirtualNetworkPolicyType> ref2 = null;
    if (latestPolicyRefs != null) {
        ref2 = latestPolicyRefs.get(0);
    }
    if (ref1 == null && ref2 == null) {
        return true;
    }
    if ((ref1 != null && ref2 == null) || (ref1 == null && ref2 != null)) {
        return false;
    }
    if ((ref1 != null && ref2 != null) && ((ref1.getUuid() != null && ref2.getUuid() == null) || (ref1.getUuid() == null && ref2.getUuid() != null))) {
        return false;
    }
    if ((ref1 != null && ref2 != null) && (ref1.getUuid() == null && ref2.getUuid() == null)) {
        return true;
    }
    if ((ref1 != null && ref2 != null) && !ref1.getUuid().equals(ref2.getUuid())) {
        return false;
    }
    return true;
}
Also used : VirtualNetworkPolicyType(net.juniper.contrail.api.types.VirtualNetworkPolicyType) ArrayList(java.util.ArrayList) InternalErrorException(com.cloud.exception.InternalErrorException) IOException(java.io.IOException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ObjectReference(net.juniper.contrail.api.ObjectReference) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType) HashSet(java.util.HashSet)

Example 5 with VnSubnetsType

use of net.juniper.contrail.api.types.VnSubnetsType in project cloudstack by apache.

the class NetworkProviderTest method dbSyncTest.

@Test
public void dbSyncTest() {
    Network network = lookupTestNetwork("test-db-only-net");
    if (network == null) {
        network = createTestNetwork("test-db-only-net");
    }
    UserVm vm = _server.createVM("test-db-only-vm", network);
    try {
        createFloatingIp(network, vm);
    } catch (Exception e) {
        fail("unable to create floating ip");
    }
    /* reset ApiServer objects to default config only, so above created objects
         * exists only in cludstack db but not in api server
         */
    ((ApiConnectorMock) _api).initConfig();
    /* reset model cached objects */
    _contrailMgr.getDatabase().initDb();
    /* Create one object of each type directly in api-server - these objects does not exist in cloudstack */
    net.juniper.contrail.api.types.Domain domain = new net.juniper.contrail.api.types.Domain();
    domain.setName("test-vnc-only-domain--1");
    domain.setUuid(UUID.randomUUID().toString());
    try {
        assertTrue(_api.create(domain));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    Project project = new Project();
    project.setName("test-vnc-only-project-1");
    project.setUuid(UUID.randomUUID().toString());
    project.setParent(domain);
    try {
        assertTrue(_api.create(project));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VirtualNetwork net = new VirtualNetwork();
    net.setName("test-vnc-only-net-1");
    net.setUuid(UUID.randomUUID().toString());
    net.setParent(project);
    NetworkIpam ipam = null;
    try {
        // Find default-network-ipam
        String ipam_id = _api.findByName(NetworkIpam.class, null, "default-network-ipam");
        assertNotNull(ipam_id);
        ipam = (NetworkIpam) _api.findById(NetworkIpam.class, ipam_id);
        assertNotNull(ipam);
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VnSubnetsType subnet = new VnSubnetsType();
    subnet.addIpamSubnets(new SubnetType("10.0.2.0", 24), "10.0.2.254");
    net.addNetworkIpam(ipam, subnet);
    VirtualMachine vncVm = new VirtualMachine();
    vncVm.setName("test-vnc-only-vm-1");
    try {
        assertTrue(_api.create(vncVm));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    VirtualMachineInterface vmi = new VirtualMachineInterface();
    vmi.setParent(vncVm);
    vmi.setName("test-vnc-only-vmi-1");
    try {
        assertTrue(_api.create(vmi));
        assertTrue(_api.create(net));
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    InstanceIp ip_obj = new InstanceIp();
    ip_obj.setName(net.getName() + ":0");
    ip_obj.setVirtualNetwork(net);
    ip_obj.setVirtualMachineInterface(vmi);
    try {
        assertTrue(_api.create(ip_obj));
        // Must perform a GET in order to update the object contents.
        assertTrue(_api.read(ip_obj));
        assertNotNull(ip_obj.getAddress());
    } catch (IOException ex) {
        fail(ex.getMessage());
    }
    // now db sync
    if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_UPDATE) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
        s_logger.info("# Cloudstack DB & VNC are out of sync - resync done");
    }
    if (_dbSync.syncAll(DBSyncGeneric.SYNC_MODE_CHECK) == ServerDBSync.SYNC_STATE_OUT_OF_SYNC) {
        s_logger.info("# Cloudstack DB & VNC are still out of sync");
        fail("DB Sync failed");
    }
}
Also used : InstanceIp(net.juniper.contrail.api.types.InstanceIp) IOException(java.io.IOException) SubnetType(net.juniper.contrail.api.types.SubnetType) CloudException(com.cloud.exception.CloudException) IOException(java.io.IOException) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Project(net.juniper.contrail.api.types.Project) UserVm(com.cloud.uservm.UserVm) VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) Network(com.cloud.network.Network) NetworkIpam(net.juniper.contrail.api.types.NetworkIpam) Domain(com.cloud.domain.Domain) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) VnSubnetsType(net.juniper.contrail.api.types.VnSubnetsType) VirtualMachine(net.juniper.contrail.api.types.VirtualMachine) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)5 VnSubnetsType (net.juniper.contrail.api.types.VnSubnetsType)5 ArrayList (java.util.ArrayList)4 VlanVO (com.cloud.dc.VlanVO)3 VlanDao (com.cloud.dc.dao.VlanDao)3 ApiConnector (net.juniper.contrail.api.ApiConnector)3 NetworkIpam (net.juniper.contrail.api.types.NetworkIpam)3 SubnetType (net.juniper.contrail.api.types.SubnetType)3 VirtualNetworkPolicyType (net.juniper.contrail.api.types.VirtualNetworkPolicyType)3 InternalErrorException (com.cloud.exception.InternalErrorException)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ObjectReference (net.juniper.contrail.api.ObjectReference)2 Project (net.juniper.contrail.api.types.Project)2 VirtualNetwork (net.juniper.contrail.api.types.VirtualNetwork)2 Domain (com.cloud.domain.Domain)1 CloudException (com.cloud.exception.CloudException)1 Network (com.cloud.network.Network)1 UserVm (com.cloud.uservm.UserVm)1