Search in sources :

Example 1 with ObjectReference

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

the class VirtualNetworkModelTest method setUp.

@Override
@Before
public void setUp() throws IOException {
    //Network UUIDs
    String uuid = UUID.randomUUID().toString();
    String uuid1 = UUID.randomUUID().toString();
    String uuid2 = UUID.randomUUID().toString();
    String uuid3 = UUID.randomUUID().toString();
    //ContrailManager
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    controller = mock(ModelController.class);
    VlanDao vlanDao = mock(VlanDao.class);
    ApiConnector api = mock(ApiConnectorMock.class);
    //Mock classes/methods
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    when(controller.getVlanDao()).thenReturn(vlanDao);
    //Policy References used by vnModel1
    List<ObjectReference<VirtualNetworkPolicyType>> policyRefs1 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
    ObjectReference<VirtualNetworkPolicyType> objectReference1 = new ObjectReference<VirtualNetworkPolicyType>();
    policyRefs1.add(objectReference1);
    //Policy References used by vnModel2
    List<ObjectReference<VirtualNetworkPolicyType>> policyRefs2 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
    ObjectReference<VirtualNetworkPolicyType> objectReference2 = new ObjectReference<VirtualNetworkPolicyType>();
    policyRefs2.add(objectReference2);
    //Policy References used by vnModel3
    List<ObjectReference<VirtualNetworkPolicyType>> policyRefs3 = new ArrayList<ObjectReference<VirtualNetworkPolicyType>>();
    ObjectReference<VirtualNetworkPolicyType> objectReference3 = new ObjectReference<VirtualNetworkPolicyType>();
    objectReference3.setReference(Arrays.asList(""), null, null, UUID.randomUUID().toString());
    policyRefs3.add(objectReference3);
    //Network to be compared with
    VirtualNetwork vn = mock(VirtualNetwork.class);
    when(api.findById(VirtualNetwork.class, uuid)).thenReturn(vn);
    //Network to be compared with
    VirtualNetwork vn1 = mock(VirtualNetwork.class);
    when(api.findById(VirtualNetwork.class, uuid1)).thenReturn(vn1);
    when(vn1.getNetworkPolicy()).thenReturn(policyRefs1);
    //Network to be compared to
    VirtualNetwork vn2 = mock(VirtualNetwork.class);
    when(api.findById(VirtualNetwork.class, uuid2)).thenReturn(vn2);
    when(vn2.getNetworkPolicy()).thenReturn(policyRefs2);
    //Network to be compared to
    VirtualNetwork vn3 = mock(VirtualNetwork.class);
    when(api.findById(VirtualNetwork.class, uuid3)).thenReturn(vn3);
    when(vn3.getNetworkPolicy()).thenReturn(policyRefs3);
    //Virtual-Network 1
    NetworkVO network1 = mock(NetworkVO.class);
    when(network1.getName()).thenReturn("testnetwork");
    when(network1.getState()).thenReturn(State.Allocated);
    when(network1.getGateway()).thenReturn("10.1.1.1");
    when(network1.getCidr()).thenReturn("10.1.1.0/24");
    when(network1.getPhysicalNetworkId()).thenReturn(42L);
    when(network1.getDomainId()).thenReturn(10L);
    when(network1.getAccountId()).thenReturn(42L);
    //Virtual-Network 2
    NetworkVO network2 = mock(NetworkVO.class);
    when(network2.getName()).thenReturn("Testnetwork");
    when(network2.getState()).thenReturn(State.Allocated);
    when(network2.getGateway()).thenReturn("10.1.1.1");
    when(network2.getCidr()).thenReturn("10.1.1.0/24");
    when(network2.getPhysicalNetworkId()).thenReturn(42L);
    when(network2.getDomainId()).thenReturn(10L);
    when(network2.getAccountId()).thenReturn(42L);
    //Virtual-Network 3
    NetworkVO network3 = mock(NetworkVO.class);
    when(network3.getName()).thenReturn("Testnetwork");
    when(network3.getState()).thenReturn(State.Allocated);
    when(network3.getGateway()).thenReturn("10.1.1.1");
    when(network3.getCidr()).thenReturn("10.1.1.0/24");
    when(network3.getPhysicalNetworkId()).thenReturn(42L);
    when(network3.getDomainId()).thenReturn(10L);
    when(network3.getAccountId()).thenReturn(42L);
    when(contrailMgr.getCanonicalName(network1)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network1.getDomainId(), network1.getAccountId())).thenReturn("testProjectId");
    vnModel = new VirtualNetworkModel(network1, uuid, "testnetwork", TrafficType.Guest);
    vnModel1 = new VirtualNetworkModel(network1, uuid1, "testnetwork", TrafficType.Guest);
    vnModel2 = new VirtualNetworkModel(network2, uuid2, "testnetwork", TrafficType.Guest);
    vnModel3 = new VirtualNetworkModel(network3, uuid3, "testnetwork", TrafficType.Guest);
}
Also used : VirtualNetworkPolicyType(net.juniper.contrail.api.types.VirtualNetworkPolicyType) VlanDao(com.cloud.dc.dao.VlanDao) NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) ArrayList(java.util.ArrayList) VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) ObjectReference(net.juniper.contrail.api.ObjectReference) Before(org.junit.Before)

Example 2 with ObjectReference

use of net.juniper.contrail.api.ObjectReference 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 3 with ObjectReference

use of net.juniper.contrail.api.ObjectReference 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 4 with ObjectReference

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

the class ServerDBSyncImpl method deleteVirtualNetwork.

public void deleteVirtualNetwork(VirtualNetwork vnet, StringBuffer syncLogMesg) throws IOException {
    final ApiConnector api = _manager.getApiConnector();
    if (_manager.isSystemDefaultNetwork(vnet)) {
        syncLogMesg.append("VN# System default virtual Network# VNC: " + vnet.getName() + " can not be deleted\n");
        return;
    }
    api.read(vnet);
    deleteInstanceIps(vnet.getInstanceIpBackRefs(), syncLogMesg);
    List<ObjectReference<ApiPropertyBase>> fipPools = vnet.getFloatingIpPools();
    if (fipPools != null && !fipPools.isEmpty()) {
        FloatingIpPool floatingIpPool = (FloatingIpPool) api.findById(FloatingIpPool.class, fipPools.get(0).getUuid());
        if (floatingIpPool != null) {
            deleteFloatingIps(floatingIpPool.getFloatingIps(), syncLogMesg);
        }
    }
    deleteVirtualMachineInterfaces(vnet.getVirtualMachineInterfaceBackRefs(), syncLogMesg);
    syncLogMesg.append("VN# DB: none; VNC: " + vnet.getName() + "(" + vnet.getUuid() + "); action: delete\n");
    api.delete(vnet);
    syncLogMesg.append("VN# VNC: " + vnet.getName() + " deleted\n");
}
Also used : FloatingIpPool(net.juniper.contrail.api.types.FloatingIpPool) ObjectReference(net.juniper.contrail.api.ObjectReference) ApiConnector(net.juniper.contrail.api.ApiConnector)

Example 5 with ObjectReference

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

the class ContrailManagerImpl method getFloatingIps.

@SuppressWarnings("unchecked")
@Override
public List<FloatingIp> getFloatingIps() {
    String fipPoolName = getDefaultPublicNetworkFQN() + ":PublicIpPool";
    FloatingIpPool fipPool = null;
    try {
        fipPool = (FloatingIpPool) _api.findByFQN(FloatingIpPool.class, fipPoolName);
    } catch (Exception ex) {
        s_logger.debug(ex);
    }
    if (fipPool == null) {
        return null;
    }
    List<ObjectReference<ApiPropertyBase>> ips = fipPool.getFloatingIps();
    if (ips != null) {
        try {
            return (List<FloatingIp>) _api.getObjects(FloatingIp.class, ips);
        } catch (IOException ex) {
            s_logger.debug(ex);
            return null;
        }
    }
    return null;
}
Also used : FloatingIpPool(net.juniper.contrail.api.types.FloatingIpPool) ObjectReference(net.juniper.contrail.api.ObjectReference) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) FloatingIp(net.juniper.contrail.api.types.FloatingIp) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException)

Aggregations

ObjectReference (net.juniper.contrail.api.ObjectReference)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ApiConnector (net.juniper.contrail.api.ApiConnector)4 VirtualNetworkPolicyType (net.juniper.contrail.api.types.VirtualNetworkPolicyType)3 VlanDao (com.cloud.dc.dao.VlanDao)2 HashSet (java.util.HashSet)2 FloatingIpPool (net.juniper.contrail.api.types.FloatingIpPool)2 VnSubnetsType (net.juniper.contrail.api.types.VnSubnetsType)2 VlanVO (com.cloud.dc.VlanVO)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 ConfigurationException (javax.naming.ConfigurationException)1 FloatingIp (net.juniper.contrail.api.types.FloatingIp)1 ServiceInstanceType (net.juniper.contrail.api.types.ServiceInstanceType)1 ServiceTemplate (net.juniper.contrail.api.types.ServiceTemplate)1 VirtualNetwork (net.juniper.contrail.api.types.VirtualNetwork)1