Search in sources :

Example 1 with IpAssocVpcCommand

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

the class CommandSetupHelper method createRedundantAssociateIPCommands.

public void createRedundantAssociateIPCommands(final VirtualRouter router, final List<? extends PublicIpAddress> ips, final Commands cmds, final String ipAssocCommand, final boolean isVPC) {
    // 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>();
        }
        // the state
        if (ipAddress.isSourceNat() && ipAddress.getState() == IpAddress.State.Releasing) {
            ipAddress.setState(IpAddress.State.Allocated);
        }
        ipList.add(ipAddress);
        vlanIpMap.put(vlanTag, ipList);
    }
    final List<NicVO> nics = _nicDao.listByVmId(router.getId());
    String baseMac = null;
    Map<String, String> vlanMacAddress = new HashMap<String, String>();
    ;
    Long guestNetworkId = null;
    for (final NicVO nic : nics) {
        final NetworkVO nw = _networkDao.findById(nic.getNetworkId());
        if (nw.getTrafficType() == TrafficType.Public) {
            if (baseMac == null) {
                baseMac = nic.getMacAddress();
            }
            final String vlanTag = BroadcastDomainType.getValue(nic.getBroadcastUri());
            vlanMacAddress.put(vlanTag, nic.getMacAddress());
        } else if (nw.getTrafficType() == TrafficType.Guest && guestNetworkId == null) {
            guestNetworkId = nw.getId();
        }
    }
    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;
            boolean sourceNat = ipAddr.isSourceNat();
            /* enable sourceNAT for the first ip of the public interface */
            if (firstIP) {
                sourceNat = true;
            }
            final String vlanId = ipAddr.getVlanTag();
            final String vlanGateway = ipAddr.getGateway();
            final String vlanNetmask = ipAddr.getNetmask();
            String vifMacAddress = null;
            final String vlanTag = BroadcastDomainType.getValue(BroadcastDomainType.fromString(ipAddr.getVlanTag()));
            if (vlanMacAddress.containsKey(vlanTag)) {
                vifMacAddress = vlanMacAddress.get(vlanTag);
            } else {
                if (ipAddr.getVlanId() != 0) {
                    vifMacAddress = NetUtils.generateMacOnIncrease(baseMac, ipAddr.getVlanId());
                } else {
                    vifMacAddress = ipAddr.getMacAddress();
                }
                vlanMacAddress.put(vlanTag, vifMacAddress);
            }
            final IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, networkRate, ipAddr.isOneToOneNat());
            setIpAddressNetworkParams(ip, network, router);
            if (router.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
                Map<String, String> details = new HashMap<>();
                String defaultSystemVmNicAdapterType = _configDao.getValue(Config.VmwareSystemVmNicDeviceType.key());
                if (defaultSystemVmNicAdapterType == null) {
                    defaultSystemVmNicAdapterType = Config.VmwareSystemVmNicDeviceType.getDefaultValue();
                }
                details.put(VmDetailConstants.NIC_ADAPTER, defaultSystemVmNicAdapterType);
                ip.setDetails(details);
            }
            ipsToSend[i++] = ip;
            /*
                 * send the firstIP = true for the first Add, this is to create
                 * primary on interface
                 */
            if (!firstIP || add) {
                firstIP = false;
            }
        }
        final IpAssocCommand cmd;
        if (router.getVpcId() != null) {
            cmd = new IpAssocVpcCommand(ipsToSend);
        } else {
            cmd = new IpAssocCommand(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);
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PublicIpAddress(com.cloud.network.PublicIpAddress) Network(com.cloud.network.Network) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) 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 2 with IpAssocVpcCommand

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

the class LibvirtNetworkElementCommandWrapperTest method testPrepareIpAssocVpcCommand.

@Test
public void testPrepareIpAssocVpcCommand() throws LibvirtException {
    IpAddressTO ip = new IpAddressTO(1, "171.31.1.3", true, false, false, "vlan://untagged", "172.31.1.1", "255.255.0.0", "02:00:7c:98:00:02", 0, true);
    ip.setTrafficType(Networks.TrafficType.Public);
    IpAddressTO[] ips = new IpAddressTO[] { ip };
    final IpAssocVpcCommand command = new IpAssocVpcCommand(ips);
    command.setAccessDetail(NetworkElementCommand.ROUTER_IP, "127.0.0.1");
    ExecutionResult result = res.prepareCommand(command);
    assertEquals(1, ips[0].getNicDevId().intValue());
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) ExecutionResult(com.cloud.utils.ExecutionResult) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with IpAssocVpcCommand

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

the class Ovm3VirtualRoutingResourceTest method prepareVpcCommandFailHeavierTest.

@Test
public void prepareVpcCommandFailHeavierTest() throws ConfigurationException {
    prepare();
    con.removeMethodResponse("list_vms");
    IpAssocVpcCommand vpc = generateIpAssocVpcCommand(xen.getVmNicMac().replace("0", "F"));
    results.basicBooleanTest(hypervisor.executeRequest(vpc).getResult(), false);
}
Also used : IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Test(org.junit.Test) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) CloudStackPluginTest(com.cloud.hypervisor.ovm3.objects.CloudStackPluginTest) LinuxTest(com.cloud.hypervisor.ovm3.objects.LinuxTest) NetworkTest(com.cloud.hypervisor.ovm3.objects.NetworkTest) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest) Ovm3ConfigurationTest(com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)

Example 4 with IpAssocVpcCommand

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

the class Ovm3VirtualRoutingResourceTest method prepareVpcCommandTest.

@Test
public void prepareVpcCommandTest() throws ConfigurationException {
    prepare();
    IpAssocVpcCommand vpc = generateIpAssocVpcCommand(xen.getVmNicMac());
    results.basicBooleanTest(hypervisor.executeRequest(vpc).getResult());
}
Also used : IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Test(org.junit.Test) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) CloudStackPluginTest(com.cloud.hypervisor.ovm3.objects.CloudStackPluginTest) LinuxTest(com.cloud.hypervisor.ovm3.objects.LinuxTest) NetworkTest(com.cloud.hypervisor.ovm3.objects.NetworkTest) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest) Ovm3ConfigurationTest(com.cloud.hypervisor.ovm3.resources.helpers.Ovm3ConfigurationTest)

Example 5 with IpAssocVpcCommand

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

the class Ovm3VirtualRoutingResourceTest method generateIpAssocVpcCommand.

private IpAssocVpcCommand generateIpAssocVpcCommand(String mac) {
    IpAssocVpcCommand cmd = new IpAssocVpcCommand(getIp(mac));
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, xen.getVmName());
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, routerip);
    // wrong as it doesn't know enough to tell
    return cmd;
}
Also used : IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand)

Aggregations

IpAssocVpcCommand (com.cloud.agent.api.routing.IpAssocVpcCommand)15 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Answer (com.cloud.agent.api.Answer)3 DataCenterVO (com.cloud.dc.DataCenterVO)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 Network (com.cloud.network.Network)3 SetSourceNatCommand (com.cloud.agent.api.routing.SetSourceNatCommand)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)1