Search in sources :

Example 11 with IpAssocVpcCommand

use of com.cloud.agent.api.routing.IpAssocVpcCommand 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, 0);
        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);
    }
    for (final Map.Entry<String, ArrayList<PublicIpAddress>> vlanAndIp : vlanIpMap.entrySet()) {
        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;
            boolean sourceNat = ipAddr.isSourceNat();
            /* enable sourceNAT for the first ip of the public interface
                * For additional public subnet source nat rule needs to be added for vm to reach ips in that subnet
                */
            if (firstIP) {
                sourceNat = true;
            }
            final String macAddress = vlanMacAddress.get(BroadcastDomainType.getValue(BroadcastDomainType.fromString(ipAddr.getVlanTag())));
            final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, BroadcastDomainType.fromString(ipAddr.getVlanTag()).toString(), ipAddr.getGateway(), ipAddr.getNetmask(), macAddress, networkRate, ipAddr.isOneToOneNat());
            ip.setTrafficType(network.getTrafficType());
            ip.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
            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());
        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 : DataCenterVO(com.cloud.dc.DataCenterVO) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) SetSourceNatCommand(com.cloud.agent.api.routing.SetSourceNatCommand) PublicIpAddress(com.cloud.network.PublicIpAddress) Network(com.cloud.network.Network) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with IpAssocVpcCommand

use of com.cloud.agent.api.routing.IpAssocVpcCommand in project cloudstack by apache.

the class CommandSetupHelper method createVpcAssociatePrivateIPCommands.

public void createVpcAssociatePrivateIPCommands(final VirtualRouter router, final List<PrivateIpAddress> ips, final Commands cmds, final boolean add) {
    // 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<PrivateIpAddress>> vlanIpMap = new HashMap<String, ArrayList<PrivateIpAddress>>();
    for (final PrivateIpAddress ipAddress : ips) {
        final String vlanTag = ipAddress.getBroadcastUri();
        ArrayList<PrivateIpAddress> ipList = vlanIpMap.get(vlanTag);
        if (ipList == null) {
            ipList = new ArrayList<PrivateIpAddress>();
        }
        ipList.add(ipAddress);
        vlanIpMap.put(vlanTag, ipList);
    }
    for (final Map.Entry<String, ArrayList<PrivateIpAddress>> vlanAndIp : vlanIpMap.entrySet()) {
        final List<PrivateIpAddress> ipAddrList = vlanAndIp.getValue();
        final IpAddressTO[] ipsToSend = new IpAddressTO[ipAddrList.size()];
        int i = 0;
        for (final PrivateIpAddress ipAddr : ipAddrList) {
            final Network network = _networkModel.getNetwork(ipAddr.getNetworkId());
            final IpAddressTO ip = new IpAddressTO(Account.ACCOUNT_ID_SYSTEM, ipAddr.getIpAddress(), add, false, ipAddr.getSourceNat(), ipAddr.getBroadcastUri(), ipAddr.getGateway(), ipAddr.getNetmask(), ipAddr.getMacAddress(), null, false);
            ip.setTrafficType(network.getTrafficType());
            ip.setNetworkName(_networkModel.getNetworkTag(router.getHypervisorType(), network));
            ipsToSend[i++] = ip;
        }
        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());
        cmds.addCommand("IPAssocVpcCommand", cmd);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PrivateIpAddress(com.cloud.network.vpc.PrivateIpAddress) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Network(com.cloud.network.Network) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

IpAssocVpcCommand (com.cloud.agent.api.routing.IpAssocVpcCommand)12 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Answer (com.cloud.agent.api.Answer)3 CloudStackPluginTest (com.cloud.hypervisor.ovm3.objects.CloudStackPluginTest)3 ConnectionTest (com.cloud.hypervisor.ovm3.objects.ConnectionTest)3 LinuxTest (com.cloud.hypervisor.ovm3.objects.LinuxTest)3 NetworkTest (com.cloud.hypervisor.ovm3.objects.NetworkTest)3 XenTest (com.cloud.hypervisor.ovm3.objects.XenTest)3 XmlTestResultTest (com.cloud.hypervisor.ovm3.objects.XmlTestResultTest)3 Ovm3ConfigurationTest (com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)3 Ovm3SupportTest (com.cloud.hypervisor.ovm3.support.Ovm3SupportTest)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 SetSourceNatCommand (com.cloud.agent.api.routing.SetSourceNatCommand)2 DataCenterVO (com.cloud.dc.DataCenterVO)2 Network (com.cloud.network.Network)2 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)1 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)1