Search in sources :

Example 1 with VlanDao

use of com.cloud.dc.dao.VlanDao in project cosmic by MissionCriticalCloud.

the class NetworkModelTest method testGetSourceNatIpAddressForGuestNetwork.

@Test
public void testGetSourceNatIpAddressForGuestNetwork() {
    final IPAddressDao ipAddressDao = mock(IPAddressDao.class);
    modelImpl._ipAddressDao = ipAddressDao;
    final List<IPAddressVO> fakeList = new ArrayList<>();
    final IPAddressVO fakeIp = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10, false);
    fakeList.add(fakeIp);
    final SearchBuilder<IPAddressVO> fakeSearch = mock(SearchBuilder.class);
    modelImpl.IpAddressSearch = fakeSearch;
    final VlanDao fakeVlanDao = mock(VlanDao.class);
    when(fakeVlanDao.findById(anyLong())).thenReturn(mock(VlanVO.class));
    modelImpl._vlanDao = fakeVlanDao;
    when(fakeSearch.create()).thenReturn(mock(SearchCriteria.class));
    when(ipAddressDao.search(any(SearchCriteria.class), (Filter) org.mockito.Matchers.isNull())).thenReturn(fakeList);
    when(ipAddressDao.findById(anyLong())).thenReturn(fakeIp);
    final Account fakeAccount = mock(Account.class);
    when(fakeAccount.getId()).thenReturn(1L);
    final Network fakeNetwork = mock(Network.class);
    when(fakeNetwork.getId()).thenReturn(1L);
    PublicIpAddress answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
    Assert.assertNull(answer);
    final IPAddressVO fakeIp2 = new IPAddressVO(new Ip("76.75.75.75"), 1, 0xaabb10ddeeffL, 10, true);
    fakeList.add(fakeIp2);
    when(ipAddressDao.findById(anyLong())).thenReturn(fakeIp2);
    answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
    Assert.assertNotNull(answer);
    Assert.assertEquals(answer.getAddress().addr(), "76.75.75.75");
}
Also used : Account(com.cloud.user.Account) VlanDao(com.cloud.dc.dao.VlanDao) IPAddressDao(com.cloud.network.dao.IPAddressDao) Ip(com.cloud.utils.net.Ip) ArrayList(java.util.ArrayList) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) SearchCriteria(com.cloud.utils.db.SearchCriteria) Test(org.junit.Test)

Example 2 with VlanDao

use of com.cloud.dc.dao.VlanDao 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 3 with VlanDao

use of com.cloud.dc.dao.VlanDao 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 4 with VlanDao

use of com.cloud.dc.dao.VlanDao 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 5 with VlanDao

use of com.cloud.dc.dao.VlanDao 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)

Aggregations

VlanDao (com.cloud.dc.dao.VlanDao)7 ArrayList (java.util.ArrayList)7 VlanVO (com.cloud.dc.VlanVO)6 ApiConnector (net.juniper.contrail.api.ApiConnector)4 IPAddressDao (com.cloud.network.dao.IPAddressDao)3 Account (com.cloud.user.Account)3 IOException (java.io.IOException)3 VirtualNetworkPolicyType (net.juniper.contrail.api.types.VirtualNetworkPolicyType)3 VnSubnetsType (net.juniper.contrail.api.types.VnSubnetsType)3 IPAddressVO (com.cloud.network.dao.IPAddressVO)2 SearchCriteria (com.cloud.utils.db.SearchCriteria)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Ip (com.cloud.utils.net.Ip)2 List (java.util.List)2 ObjectReference (net.juniper.contrail.api.ObjectReference)2 NetworkIpam (net.juniper.contrail.api.types.NetworkIpam)2 SubnetType (net.juniper.contrail.api.types.SubnetType)2 VirtualNetwork (net.juniper.contrail.api.types.VirtualNetwork)2 Test (org.junit.Test)2 DataCenter (com.cloud.dc.DataCenter)1