Search in sources :

Example 21 with PublicIpAddress

use of com.cloud.network.PublicIpAddress in project cloudstack by apache.

the class NiciraNvpElementTest method applyIpTest.

@Test
public void applyIpTest() throws ResourceUnavailableException {
    final Network network = mock(Network.class);
    when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch);
    when(network.getId()).thenReturn(NETWORK_ID);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    final NetworkOffering offering = mock(NetworkOffering.class);
    when(offering.getId()).thenReturn(NETWORK_ID);
    when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
    when(offering.getGuestType()).thenReturn(GuestType.Isolated);
    final List<PublicIpAddress> ipAddresses = new ArrayList<PublicIpAddress>();
    final PublicIpAddress pipReleased = mock(PublicIpAddress.class);
    final PublicIpAddress pipAllocated = mock(PublicIpAddress.class);
    final Ip ipReleased = new Ip("42.10.10.10");
    final Ip ipAllocated = new Ip("10.10.10.10");
    when(pipAllocated.getState()).thenReturn(IpAddress.State.Allocated);
    when(pipAllocated.getAddress()).thenReturn(ipAllocated);
    when(pipAllocated.getNetmask()).thenReturn("255.255.255.0");
    when(pipReleased.getState()).thenReturn(IpAddress.State.Releasing);
    when(pipReleased.getAddress()).thenReturn(ipReleased);
    when(pipReleased.getNetmask()).thenReturn("255.255.255.0");
    ipAddresses.add(pipAllocated);
    ipAddresses.add(pipReleased);
    final Set<Service> services = new HashSet<Service>();
    services.add(Service.SourceNat);
    services.add(Service.StaticNat);
    services.add(Service.PortForwarding);
    final List<NiciraNvpDeviceVO> deviceList = new ArrayList<NiciraNvpDeviceVO>();
    final NiciraNvpDeviceVO nndVO = mock(NiciraNvpDeviceVO.class);
    final NiciraNvpRouterMappingVO nnrmVO = mock(NiciraNvpRouterMappingVO.class);
    when(niciraNvpRouterMappingDao.findByNetworkId(NETWORK_ID)).thenReturn(nnrmVO);
    when(nnrmVO.getLogicalRouterUuid()).thenReturn("abcde");
    when(nndVO.getHostId()).thenReturn(NETWORK_ID);
    final HostVO hvo = mock(HostVO.class);
    when(hvo.getId()).thenReturn(NETWORK_ID);
    when(hvo.getDetail("l3gatewayserviceuuid")).thenReturn("abcde");
    when(hostDao.findById(NETWORK_ID)).thenReturn(hvo);
    deviceList.add(nndVO);
    when(niciraNvpDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(deviceList);
    final ConfigurePublicIpsOnLogicalRouterAnswer answer = mock(ConfigurePublicIpsOnLogicalRouterAnswer.class);
    when(answer.getResult()).thenReturn(true);
    when(agentManager.easySend(eq(NETWORK_ID), any(ConfigurePublicIpsOnLogicalRouterCommand.class))).thenReturn(answer);
    assertTrue(element.applyIps(network, ipAddresses, services));
    verify(agentManager, atLeast(1)).easySend(eq(NETWORK_ID), argThat(new ArgumentMatcher<ConfigurePublicIpsOnLogicalRouterCommand>() {

        @Override
        public boolean matches(final ConfigurePublicIpsOnLogicalRouterCommand command) {
            return command.getPublicCidrs().size() == 1;
        }
    }));
}
Also used : NetworkOffering(com.cloud.offering.NetworkOffering) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) NiciraNvpDeviceVO(com.cloud.network.NiciraNvpDeviceVO) ArrayList(java.util.ArrayList) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) HostVO(com.cloud.host.HostVO) PublicIpAddress(com.cloud.network.PublicIpAddress) NiciraNvpRouterMappingVO(com.cloud.network.NiciraNvpRouterMappingVO) Network(com.cloud.network.Network) ArgumentMatcher(org.mockito.ArgumentMatcher) ConfigurePublicIpsOnLogicalRouterAnswer(com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer) ConfigurePublicIpsOnLogicalRouterCommand(com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with PublicIpAddress

use of com.cloud.network.PublicIpAddress in project cloudstack by apache.

the class CommandSetupHelper method createVpcAssociatePublicIPCommands.

public void createVpcAssociatePublicIPCommands(final VirtualRouter router, final List<? extends PublicIpAddress> ips, final Commands cmds, final Map<String, String> vlanMacAddress) {
    final String ipAssocCommand = "IPAssocVpcCommand";
    if (router.getIsRedundantRouter()) {
        createRedundantAssociateIPCommands(router, ips, cmds, ipAssocCommand, true);
        return;
    }
    Pair<IpAddressTO, Long> sourceNatIpAdd = null;
    Boolean addSourceNat = null;
    // Ensure that in multiple vlans case we first send all ip addresses of
    // vlan1, then all ip addresses of vlan2, etc..
    final Map<String, ArrayList<PublicIpAddress>> vlanIpMap = new HashMap<String, ArrayList<PublicIpAddress>>();
    for (final PublicIpAddress ipAddress : ips) {
        final String vlanTag = ipAddress.getVlanTag();
        ArrayList<PublicIpAddress> ipList = vlanIpMap.get(vlanTag);
        if (ipList == null) {
            ipList = new ArrayList<PublicIpAddress>();
        }
        // state
        if (ipAddress.isSourceNat() && ipAddress.getState() == IpAddress.State.Releasing) {
            ipAddress.setState(IpAddress.State.Allocated);
        }
        ipList.add(ipAddress);
        vlanIpMap.put(vlanTag, ipList);
    }
    Long guestNetworkId = null;
    final List<NicVO> nics = _nicDao.listByVmId(router.getId());
    for (final NicVO nic : nics) {
        final NetworkVO nw = _networkDao.findById(nic.getNetworkId());
        if (nw.getTrafficType() == TrafficType.Guest) {
            guestNetworkId = nw.getId();
            break;
        }
    }
    Map<String, Boolean> vlanLastIpMap = getVlanLastIpMap(router.getVpcId(), guestNetworkId);
    for (final Map.Entry<String, ArrayList<PublicIpAddress>> vlanAndIp : vlanIpMap.entrySet()) {
        final String vlanTagKey = vlanAndIp.getKey();
        final List<PublicIpAddress> ipAddrList = vlanAndIp.getValue();
        // Source nat ip address should always be sent first
        Collections.sort(ipAddrList, new Comparator<PublicIpAddress>() {

            @Override
            public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
                final boolean s1 = o1.isSourceNat();
                final boolean s2 = o2.isSourceNat();
                return s1 ^ s2 ? s1 ^ true ? 1 : -1 : 0;
            }
        });
        // Get network rate - required for IpAssoc
        final Integer networkRate = _networkModel.getNetworkRate(ipAddrList.get(0).getNetworkId(), router.getId());
        final Network network = _networkModel.getNetwork(ipAddrList.get(0).getNetworkId());
        final IpAddressTO[] ipsToSend = new IpAddressTO[ipAddrList.size()];
        int i = 0;
        boolean firstIP = true;
        for (final PublicIpAddress ipAddr : ipAddrList) {
            final boolean add = ipAddr.getState() == IpAddress.State.Releasing ? false : true;
            final String macAddress = vlanMacAddress.get(BroadcastDomainType.getValue(BroadcastDomainType.fromString(ipAddr.getVlanTag())));
            final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, ipAddr.isSourceNat(), BroadcastDomainType.fromString(ipAddr.getVlanTag()).toString(), ipAddr.getGateway(), ipAddr.getNetmask(), macAddress, networkRate, ipAddr.isOneToOneNat());
            setIpAddressNetworkParams(ip, network, router);
            ipsToSend[i++] = ip;
            if (ipAddr.isSourceNat()) {
                sourceNatIpAdd = new Pair<IpAddressTO, Long>(ip, ipAddr.getNetworkId());
                addSourceNat = add;
            }
            // want to set sourcenat to true for all ips to delete source nat rules.
            if (!firstIP || add) {
                firstIP = false;
            }
        }
        final IpAssocVpcCommand cmd = new IpAssocVpcCommand(ipsToSend);
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, _routerControlHelper.getRouterIpInNetwork(ipAddrList.get(0).getNetworkId(), router.getId()));
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
        final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
        cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
        setAccessDetailNetworkLastPublicIp(vlanLastIpMap, vlanTagKey, cmd);
        cmds.addCommand(ipAssocCommand, cmd);
    }
    // set source nat ip
    if (sourceNatIpAdd != null) {
        final IpAddressTO sourceNatIp = sourceNatIpAdd.first();
        final SetSourceNatCommand cmd = new SetSourceNatCommand(sourceNatIp, addSourceNat);
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, _routerControlHelper.getRouterControlIp(router.getId()));
        cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
        final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
        cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
        cmds.addCommand("SetSourceNatCommand", cmd);
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SetSourceNatCommand(com.cloud.agent.api.routing.SetSourceNatCommand) PublicIpAddress(com.cloud.network.PublicIpAddress) Network(com.cloud.network.Network) NicVO(com.cloud.vm.NicVO) DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Map(java.util.Map) HashMap(java.util.HashMap)

Example 23 with PublicIpAddress

use of com.cloud.network.PublicIpAddress in project cloudstack by apache.

the class VpcVirtualNetworkApplianceManagerImpl method getNicsToChangeOnRouter.

protected Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNicsToChangeOnRouter(final List<? extends PublicIpAddress> publicIps, final VirtualRouter router) {
    // 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>();
    // find out nics to unplug
    for (final PublicIpAddress ip : publicIps) {
        final 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) {
            final Nic nic = _nicDao.findByIp4AddressAndNetworkIdAndInstanceId(publicNtwkId, router.getId(), ip.getAddress().addr());
            if (nic != null) {
                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 (final PublicIpAddress ip : publicIps) {
        final URI broadcastUri = BroadcastDomainType.Vlan.toUri(ip.getVlanTag());
        final 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
            final 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) {
                    final 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());
                }
            }
        }
    }
    final Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> nicsToChange = new Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>>(nicsToPlug, nicsToUnplug);
    return nicsToChange;
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) HashMap(java.util.HashMap) Nic(com.cloud.vm.Nic) URI(java.net.URI) NicVO(com.cloud.vm.NicVO) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.cloud.utils.Pair)

Example 24 with PublicIpAddress

use of com.cloud.network.PublicIpAddress 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)

Example 25 with PublicIpAddress

use of com.cloud.network.PublicIpAddress in project cloudstack by apache.

the class VpcIpAssociationRules method accept.

@Override
public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException {
    _router = router;
    _vlanMacAddress = new HashMap<String, String>();
    _ipsToSend = new ArrayList<PublicIpAddress>();
    NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao();
    for (PublicIpAddress ipAddr : _ipAddresses) {
        String broadcastURI = BroadcastDomainType.Vlan.toUri(ipAddr.getVlanTag()).toString();
        Nic nic = nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ipAddr.getNetworkId(), _router.getId(), broadcastURI);
        String macAddress = null;
        if (nic == null) {
            if (ipAddr.getState() != IpAddress.State.Releasing) {
                throw new CloudRuntimeException("Unable to find the nic in network " + ipAddr.getNetworkId() + "  to apply the ip address " + ipAddr + " for");
            }
            s_logger.debug("Not sending release for ip address " + ipAddr + " as its nic is already gone from VPC router " + _router);
        } else {
            macAddress = nic.getMacAddress();
            _vlanMacAddress.put(BroadcastDomainType.getValue(BroadcastDomainType.fromString(ipAddr.getVlanTag())), macAddress);
            _ipsToSend.add(ipAddr);
        }
    }
    return visitor.visit(this);
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) NicDao(com.cloud.vm.dao.NicDao) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Nic(com.cloud.vm.Nic)

Aggregations

PublicIpAddress (com.cloud.network.PublicIpAddress)26 ArrayList (java.util.ArrayList)15 Network (com.cloud.network.Network)12 HashMap (java.util.HashMap)9 Map (java.util.Map)8 NicVO (com.cloud.vm.NicVO)7 IPAddressVO (com.cloud.network.dao.IPAddressVO)6 PublicIp (com.cloud.network.addr.PublicIp)5 Pair (com.cloud.utils.Pair)5 Ip (com.cloud.utils.net.Ip)5 Nic (com.cloud.vm.Nic)5 URI (java.net.URI)5 ConfigurePublicIpsOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer)4 ConfigurePublicIpsOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand)4 Commands (com.cloud.agent.manager.Commands)4 DataCenterVO (com.cloud.dc.DataCenterVO)4 NicDao (com.cloud.vm.dao.NicDao)4 HostVO (com.cloud.host.HostVO)3 NiciraNvpDeviceVO (com.cloud.network.NiciraNvpDeviceVO)3 NiciraNvpRouterMappingVO (com.cloud.network.NiciraNvpRouterMappingVO)3