Search in sources :

Example 1 with IPAddressDao

use of com.cloud.network.dao.IPAddressDao 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 IPAddressDao

use of com.cloud.network.dao.IPAddressDao in project cloudstack by apache.

the class DhcpSubNetRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao();
    final UserVmVO vm = userVmDao.findById(_profile.getId());
    userVmDao.loadDetails(vm);
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    // check if this is not the primary subnet.
    final NicVO domrGuestNic = nicDao.findByInstanceIdAndIpAddressAndVmtype(_router.getId(), nicDao.getIpAddress(_nic.getNetworkId(), _router.getId()), VirtualMachine.Type.DomainRouter);
    // networks.
    if (!NetUtils.sameSubnet(domrGuestNic.getIPv4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
        final NicIpAliasDao nicIpAliasDao = visitor.getVirtualNetworkApplianceFactory().getNicIpAliasDao();
        final List<NicIpAliasVO> aliasIps = nicIpAliasDao.listByNetworkIdAndState(domrGuestNic.getNetworkId(), NicIpAlias.State.active);
        boolean ipInVmsubnet = false;
        for (final NicIpAliasVO alias : aliasIps) {
            // check if any of the alias ips belongs to the Vm's subnet.
            if (NetUtils.sameSubnet(alias.getIp4Address(), _nic.getIPv4Address(), _nic.getIPv4Netmask())) {
                ipInVmsubnet = true;
                break;
            }
        }
        PublicIp routerPublicIP = null;
        DataCenterDao dcDao = visitor.getVirtualNetworkApplianceFactory().getDcDao();
        final DataCenter dc = dcDao.findById(_router.getDataCenterId());
        if (ipInVmsubnet == false) {
            try {
                if (_network.getTrafficType() == TrafficType.Guest && _network.getGuestType() == GuestType.Shared) {
                    HostPodDao podDao = visitor.getVirtualNetworkApplianceFactory().getPodDao();
                    podDao.findById(vm.getPodIdToDeployIn());
                    final Account caller = CallContext.current().getCallingAccount();
                    VlanDao vlanDao = visitor.getVirtualNetworkApplianceFactory().getVlanDao();
                    final List<VlanVO> vlanList = vlanDao.listVlansByNetworkIdAndGateway(_network.getId(), _nic.getIPv4Gateway());
                    final List<Long> vlanDbIdList = new ArrayList<Long>();
                    for (final VlanVO vlan : vlanList) {
                        vlanDbIdList.add(vlan.getId());
                    }
                    IpAddressManager ipAddrMgr = visitor.getVirtualNetworkApplianceFactory().getIpAddrMgr();
                    if (dc.getNetworkType() == NetworkType.Basic) {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    } else {
                        routerPublicIP = ipAddrMgr.assignPublicIpAddressFromVlans(_router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, _nic.getNetworkId(), null, _nic.getIPv4Gateway(), false);
                    }
                    _routerAliasIp = routerPublicIP.getAddress().addr();
                }
            } catch (final InsufficientAddressCapacityException e) {
                s_logger.info(e.getMessage());
                s_logger.info("unable to configure dhcp for this VM.");
                return false;
            }
            // this means we did not create an IP alias on the router.
            _nicAlias = new NicIpAliasVO(domrGuestNic.getId(), _routerAliasIp, _router.getId(), CallContext.current().getCallingAccountId(), _network.getDomainId(), _nic.getNetworkId(), _nic.getIPv4Gateway(), _nic.getIPv4Netmask());
            _nicAlias.setAliasCount(routerPublicIP.getIpMacAddress());
            nicIpAliasDao.persist(_nicAlias);
            final boolean result = visitor.visit(this);
            if (result == false) {
                final NicIpAliasVO ipAliasVO = nicIpAliasDao.findByInstanceIdAndNetworkId(_network.getId(), _router.getId());
                final PublicIp routerPublicIPFinal = routerPublicIP;
                Transaction.execute(new TransactionCallbackNoReturn() {

                    @Override
                    public void doInTransactionWithoutResult(final TransactionStatus status) {
                        nicIpAliasDao.expunge(ipAliasVO.getId());
                        IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
                        ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId());
                    }
                });
                throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config");
            }
        }
        return true;
    }
    return true;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) NicDao(com.cloud.vm.dao.NicDao) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicIpAliasVO(com.cloud.vm.dao.NicIpAliasVO) NicIpAliasDao(com.cloud.vm.dao.NicIpAliasDao) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UserVmDao(com.cloud.vm.dao.UserVmDao) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO) VlanDao(com.cloud.dc.dao.VlanDao) PublicIp(com.cloud.network.addr.PublicIp) IPAddressDao(com.cloud.network.dao.IPAddressDao) DataCenterDao(com.cloud.dc.dao.DataCenterDao) HostPodDao(com.cloud.dc.dao.HostPodDao) IpAddressManager(com.cloud.network.IpAddressManager) DataCenter(com.cloud.dc.DataCenter)

Example 3 with IPAddressDao

use of com.cloud.network.dao.IPAddressDao in project cloudstack by apache.

the class NetworkModelTest method testGetSourceNatIpAddressForGuestNetwork.

@Test
public void testGetSourceNatIpAddressForGuestNetwork() {
    NetworkModelImpl modelImpl = new NetworkModelImpl();
    IPAddressDao ipAddressDao = mock(IPAddressDao.class);
    modelImpl._ipAddressDao = ipAddressDao;
    List<IPAddressVO> fakeList = new ArrayList<IPAddressVO>();
    IPAddressVO fakeIp = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10, false);
    fakeList.add(fakeIp);
    SearchBuilder<IPAddressVO> fakeSearch = mock(SearchBuilder.class);
    modelImpl.IpAddressSearch = fakeSearch;
    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) isNull())).thenReturn(fakeList);
    when(ipAddressDao.findById(anyLong())).thenReturn(fakeIp);
    Account fakeAccount = mock(Account.class);
    when(fakeAccount.getId()).thenReturn(1L);
    Network fakeNetwork = mock(Network.class);
    when(fakeNetwork.getId()).thenReturn(1L);
    PublicIpAddress answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
    Assert.assertNull(answer);
    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) SearchCriteria(com.cloud.utils.db.SearchCriteria) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) Test(org.junit.Test)

Example 4 with IPAddressDao

use of com.cloud.network.dao.IPAddressDao in project cloudstack by apache.

the class NicPlugInOutRules method getNicsToChangeOnRouter.

private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final NetworkTopologyVisitor visitor) {
    // 1) check which nics need to be plugged/unplugged and plug/unplug them
    final Map<String, PublicIpAddress> nicsToPlug = new HashMap<String, PublicIpAddress>();
    final Map<String, PublicIpAddress> nicsToUnplug = new HashMap<String, PublicIpAddress>();
    VpcManager vpcMgr = visitor.getVirtualNetworkApplianceFactory().getVpcMgr();
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    IPAddressDao ipAddressDao = visitor.getVirtualNetworkApplianceFactory().getIpAddressDao();
    FirewallRulesDao rulesDao = visitor.getVirtualNetworkApplianceFactory().getFirewallRulesDao();
    // find out nics to unplug
    for (PublicIpAddress ip : _ipAddresses) {
        long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Releasing) {
            NicVO nic = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), ip.getAddress().addr());
            if (nic != null) {
                final List<IPAddressVO> allIps = ipAddressDao.listByAssociatedVpc(ip.getVpcId(), null);
                boolean ipUpdated = false;
                for (IPAddressVO allIp : allIps) {
                    if (allIp.getId() != ip.getId() && allIp.getVlanId() == ip.getVlanId() && (allIp.isSourceNat() || rulesDao.countRulesByIpIdAndState(allIp.getId(), FirewallRule.State.Active) > 0 || (allIp.isOneToOneNat() && allIp.getRuleState() == null))) {
                        s_logger.debug("Updating the nic " + nic + " with new ip address " + allIp.getAddress().addr());
                        nic.setIPv4Address(allIp.getAddress().addr());
                        nicDao.update(nic.getId(), nic);
                        ipUpdated = true;
                        break;
                    }
                }
                if (!ipUpdated) {
                    nicsToUnplug.put(ip.getVlanTag(), ip);
                    s_logger.debug("Need to unplug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
                }
            }
        }
    }
    // find out nics to plug
    for (PublicIpAddress ip : _ipAddresses) {
        URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
        long publicNtwkId = ip.getNetworkId();
        // rules, release it on the backend
        if (!vpcMgr.isIpAllocatedToVpc(ip)) {
            ip.setState(IpAddress.State.Releasing);
        }
        if (ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) {
            // nic has to be plugged only when there are no nics for this
            // vlan tag exist on VR
            Nic nic = nicDao.findByNetworkIdInstanceIdAndBroadcastUri(publicNtwkId, _router.getId(), broadcastUri.toString());
            if (nic == null && nicsToPlug.get(ip.getVlanTag()) == null) {
                nicsToPlug.put(ip.getVlanTag(), ip);
                s_logger.debug("Need to plug the nic for ip=" + ip + "; vlan=" + ip.getVlanTag() + " in public network id =" + publicNtwkId);
            } else {
                final PublicIpAddress nicToUnplug = nicsToUnplug.get(ip.getVlanTag());
                if (nicToUnplug != null) {
                    NicVO nicVO = nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, _router.getId(), nicToUnplug.getAddress().addr());
                    nicVO.setIPv4Address(ip.getAddress().addr());
                    nicDao.update(nicVO.getId(), nicVO);
                    s_logger.debug("Updated the nic " + nicVO + " with the new ip address " + ip.getAddress().addr());
                    nicsToUnplug.remove(ip.getVlanTag());
                }
            }
        }
    }
    Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>>(nicsToPlug, nicsToUnplug);
    return nicsToChange;
}
Also used : HashMap(java.util.HashMap) NicDao(com.cloud.vm.dao.NicDao) IPAddressDao(com.cloud.network.dao.IPAddressDao) Nic(com.cloud.vm.Nic) FirewallRulesDao(com.cloud.network.dao.FirewallRulesDao) URI(java.net.URI) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcManager(com.cloud.network.vpc.VpcManager) IPAddressVO(com.cloud.network.dao.IPAddressVO) NicVO(com.cloud.vm.NicVO) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.cloud.utils.Pair)

Aggregations

IPAddressDao (com.cloud.network.dao.IPAddressDao)4 VlanVO (com.cloud.dc.VlanVO)3 VlanDao (com.cloud.dc.dao.VlanDao)3 IPAddressVO (com.cloud.network.dao.IPAddressVO)3 Account (com.cloud.user.Account)3 ArrayList (java.util.ArrayList)3 SearchCriteria (com.cloud.utils.db.SearchCriteria)2 Ip (com.cloud.utils.net.Ip)2 NicVO (com.cloud.vm.NicVO)2 NicDao (com.cloud.vm.dao.NicDao)2 Test (org.junit.Test)2 DataCenter (com.cloud.dc.DataCenter)1 DataCenterDao (com.cloud.dc.dao.DataCenterDao)1 HostPodDao (com.cloud.dc.dao.HostPodDao)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 IpAddressManager (com.cloud.network.IpAddressManager)1 PublicIpAddress (com.cloud.network.PublicIpAddress)1 PublicIp (com.cloud.network.addr.PublicIp)1 FirewallRulesDao (com.cloud.network.dao.FirewallRulesDao)1 VpcManager (com.cloud.network.vpc.VpcManager)1