Search in sources :

Example 6 with SetSourceNatCommand

use of com.cloud.agent.api.routing.SetSourceNatCommand 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)

Aggregations

SetSourceNatCommand (com.cloud.agent.api.routing.SetSourceNatCommand)6 Answer (com.cloud.agent.api.Answer)4 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)4 IpAssocVpcCommand (com.cloud.agent.api.routing.IpAssocVpcCommand)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)1 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)1 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)1 CheckNetworkCommand (com.cloud.agent.api.CheckNetworkCommand)1 CheckRouterCommand (com.cloud.agent.api.CheckRouterCommand)1 CheckS2SVpnConnectionsCommand (com.cloud.agent.api.CheckS2SVpnConnectionsCommand)1 CheckVirtualMachineCommand (com.cloud.agent.api.CheckVirtualMachineCommand)1 CleanupNetworkRulesCmd (com.cloud.agent.api.CleanupNetworkRulesCmd)1 ComputeChecksumCommand (com.cloud.agent.api.ComputeChecksumCommand)1 CreatePrivateTemplateFromSnapshotCommand (com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand)1 CreatePrivateTemplateFromVolumeCommand (com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand)1 CreateStoragePoolCommand (com.cloud.agent.api.CreateStoragePoolCommand)1 CreateVMSnapshotCommand (com.cloud.agent.api.CreateVMSnapshotCommand)1