Search in sources :

Example 11 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();
    // 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) {
            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 (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) Nic(com.cloud.vm.Nic) URI(java.net.URI) PublicIpAddress(com.cloud.network.PublicIpAddress) VpcManager(com.cloud.network.vpc.VpcManager) NicVO(com.cloud.vm.NicVO) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.cloud.utils.Pair)

Example 12 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 13 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 Object argument) {
            final ConfigurePublicIpsOnLogicalRouterCommand command = (ConfigurePublicIpsOnLogicalRouterCommand) argument;
            if (command.getPublicCidrs().size() == 1)
                return true;
            return false;
        }
    }));
}
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 14 with PublicIpAddress

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

the class MidoNetElement method associatePublicIP.

public boolean associatePublicIP(Network network, final List<? extends PublicIpAddress> ipAddress) throws ResourceUnavailableException {
    s_logger.debug("associatePublicIP called with network: " + network.toString());
    /*
         * Get Mido Router for this network and set source rules
         * These should only be allocated inside the for loop, because
         * this function could be called as a part of network cleanup. In
         * that case, we do not want to recreate the guest network or
         * any ports.
         */
    boolean resources = false;
    Router tenantRouter = null;
    Router providerRouter = null;
    RouterPort[] ports = null;
    RouterPort tenantUplink = null;
    RouterPort providerDownlink = null;
    RuleChain preNat = null;
    RuleChain post = null;
    String accountIdStr = null;
    String routerName = null;
    // Set Source NAT rules on router
    for (PublicIpAddress ip : ipAddress) {
        // ip is the external one we sourcenat to
        if (ip.isSourceNat()) {
            if (resources == false) {
                tenantRouter = getOrCreateGuestNetworkRouter(network);
                providerRouter = api.getRouter(_providerRouterId);
                ports = getOrCreateProviderRouterPorts(tenantRouter, providerRouter);
                tenantUplink = ports[0];
                providerDownlink = ports[1];
                accountIdStr = getAccountUuid(network);
                boolean isVpc = getIsVpc(network);
                long id = getRouterId(network, isVpc);
                routerName = getRouterName(isVpc, id);
                preNat = getChain(accountIdStr, routerName, RuleChainCode.TR_PRENAT);
                post = api.getChain(tenantRouter.getOutboundFilterId());
                resources = true;
            }
            applySourceNat(// Routers
            tenantRouter, // Routers
            providerRouter, // Ports
            tenantUplink, // Ports
            providerDownlink, // Chains
            preNat, // Chains
            post, // The IP
            ip);
        }
    }
    return true;
}
Also used : PublicIpAddress(com.cloud.network.PublicIpAddress) RuleChain(org.midonet.client.resource.RuleChain) Router(org.midonet.client.resource.Router) RouterPort(org.midonet.client.resource.RouterPort)

Aggregations

PublicIpAddress (com.cloud.network.PublicIpAddress)14 Network (com.cloud.network.Network)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 PublicIp (com.cloud.network.addr.PublicIp)3 IPAddressVO (com.cloud.network.dao.IPAddressVO)3 Pair (com.cloud.utils.Pair)3 Nic (com.cloud.vm.Nic)3 NicVO (com.cloud.vm.NicVO)3 URI (java.net.URI)3 ConfigurePublicIpsOnLogicalRouterAnswer (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer)2 ConfigurePublicIpsOnLogicalRouterCommand (com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand)2 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)2 Commands (com.cloud.agent.manager.Commands)2 DataCenterVO (com.cloud.dc.DataCenterVO)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 HostVO (com.cloud.host.HostVO)2 NiciraNvpDeviceVO (com.cloud.network.NiciraNvpDeviceVO)2 NiciraNvpRouterMappingVO (com.cloud.network.NiciraNvpRouterMappingVO)2