Search in sources :

Example 1 with IpAssocAnswer

use of com.cloud.agent.api.routing.IpAssocAnswer in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

public Answer execute(IpAssocCommand cmd) {
    String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    String[] results = new String[cmd.getIpAddresses().length];
    Connect conn;
    try {
        conn = LibvirtConnection.getConnection();
        List<InterfaceDef> nics = getInterfaces(conn, routerName);
        Map<String, Integer> vlanAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                vlanAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                String vlanId = getVlanIdFromBridge(nic.getBrName());
                if (vlanId != null) {
                    vlanAllocatedToVM.put(vlanId, nicPos);
                } else {
                    vlanAllocatedToVM.put(Vlan.UNTAGGED, nicPos);
                }
            }
            nicPos++;
        }
        IpAddressTO[] ips = cmd.getIpAddresses();
        int i = 0;
        String result = null;
        int nicNum = 0;
        for (IpAddressTO ip : ips) {
            if (!vlanAllocatedToVM.containsKey(ip.getVlanId())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getVlanId(), ip.getVifMacAddress());
                vlanAllocatedToVM.put(ip.getVlanId(), nicPos++);
            }
            nicNum = vlanAllocatedToVM.get(ip.getVlanId());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            result = _virtRouterResource.assignPublicIpAddress(routerName, routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getGuestIp(), nicNum);
            if (result != null) {
                results[i++] = IpAssocAnswer.errorResult;
            } else {
                results[i++] = ip.getPublicIp() + " - success";
                ;
            }
        }
        return new IpAssocAnswer(cmd, results);
    } catch (LibvirtException e) {
        return new IpAssocAnswer(cmd, results);
    } catch (InternalErrorException e) {
        return new IpAssocAnswer(cmd, results);
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)

Example 2 with IpAssocAnswer

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

the class HypervDirectConnectResource method execute.

protected Answer execute(final IpAssocCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource IPAssocCommand: " + s_gson.toJson(cmd));
    }
    int i = 0;
    final String[] results = new String[cmd.getIpAddresses().length];
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
        final String controlIp = getRouterSshControlIp(cmd);
        for (final IpAddressTO ip : ips) {
            assignPublicIpAddress(routerName, controlIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getBroadcastUri(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress());
            results[i++] = ip.getPublicIp() + " - success";
        }
        for (; i < cmd.getIpAddresses().length; i++) {
            results[i++] = IpAssocAnswer.errorResult;
        }
    } catch (final Throwable e) {
        s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
        for (; i < cmd.getIpAddresses().length; i++) {
            results[i++] = IpAssocAnswer.errorResult;
        }
    }
    return new IpAssocAnswer(cmd, results);
}
Also used : IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) IpAddressTO(com.cloud.agent.api.to.IpAddressTO)

Example 3 with IpAssocAnswer

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

the class F5BigIpResource method execute.

private synchronized Answer execute(IpAssocCommand cmd, int numRetries) {
    String[] results = new String[cmd.getIpAddresses().length];
    int i = 0;
    try {
        IpAddressTO[] ips = cmd.getIpAddresses();
        for (IpAddressTO ip : ips) {
            // is it saver to use Long.valueOf(BroadcastDomain.getValue(ip.getBroadcastUri())) ???
            // i.o.w. can this contain vlan:// then change !!!
            long guestVlanTag = Long.parseLong(ip.getBroadcastUri());
            // It's a hack, using isOneToOneNat field for indicate if it's inline or not
            boolean inline = ip.isOneToOneNat();
            String vlanSelfIp = inline ? tagAddressWithRouteDomain(ip.getVlanGateway(), guestVlanTag) : ip.getVlanGateway();
            String vlanNetmask = ip.getVlanNetmask();
            // Delete any existing guest VLAN with this tag, self IP, and netmask
            deleteGuestVlan(guestVlanTag, vlanSelfIp, vlanNetmask, inline);
            if (ip.isAdd()) {
                // Add a new guest VLAN
                addGuestVlan(guestVlanTag, vlanSelfIp, vlanNetmask, inline);
            }
            saveConfiguration();
            results[i++] = ip.getPublicIp() + " - success";
        }
    } catch (ExecutionException e) {
        s_logger.error("Failed to execute IPAssocCommand due to " + e);
        if (shouldRetry(numRetries)) {
            return retry(cmd, numRetries);
        } else {
            results[i++] = IpAssocAnswer.errorResult;
        }
    }
    return new IpAssocAnswer(cmd, results);
}
Also used : IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 4 with IpAssocAnswer

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

the class PaloAltoResource method execute.

private Answer execute(IpAssocCommand cmd, int numRetries) {
    String[] results = new String[cmd.getIpAddresses().length];
    int i = 0;
    try {
        IpAddressTO ip;
        if (cmd.getIpAddresses().length != 1) {
            throw new ExecutionException("Received an invalid number of guest IPs to associate.");
        } else {
            ip = cmd.getIpAddresses()[0];
        }
        String sourceNatIpAddress = null;
        GuestNetworkType type = GuestNetworkType.INTERFACE_NAT;
        if (ip.isSourceNat()) {
            type = GuestNetworkType.SOURCE_NAT;
            if (ip.getPublicIp() == null) {
                throw new ExecutionException("Source NAT IP address must not be null.");
            } else {
                sourceNatIpAddress = ip.getPublicIp();
            }
        }
        long guestVlanTag = Long.parseLong(cmd.getAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG));
        String guestVlanGateway = cmd.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY);
        String cidr = cmd.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR);
        long cidrSize = NetUtils.cidrToLong(cidr)[1];
        String guestVlanSubnet = NetUtils.getCidrSubNet(guestVlanGateway, cidrSize);
        Long publicVlanTag = null;
        if (ip.getBroadcastUri() != null) {
            String parsedVlanTag = parsePublicVlanTag(ip.getBroadcastUri());
            if (!parsedVlanTag.equals("untagged")) {
                try {
                    publicVlanTag = Long.parseLong(parsedVlanTag);
                } catch (Exception e) {
                    throw new ExecutionException("Could not parse public VLAN tag: " + parsedVlanTag);
                }
            }
        }
        ArrayList<IPaloAltoCommand> commandList = new ArrayList<IPaloAltoCommand>();
        if (ip.isAdd()) {
            // Implement the guest network for this VLAN
            implementGuestNetwork(commandList, type, publicVlanTag, sourceNatIpAddress, guestVlanTag, guestVlanGateway, guestVlanSubnet, cidrSize);
        } else {
            // Remove the guest network:
            shutdownGuestNetwork(commandList, type, publicVlanTag, sourceNatIpAddress, guestVlanTag, guestVlanGateway, guestVlanSubnet, cidrSize);
        }
        boolean status = requestWithCommit(commandList);
        results[i++] = ip.getPublicIp() + " - success";
    } catch (ExecutionException e) {
        s_logger.error(e);
        if (numRetries > 0 && refreshPaloAltoConnection()) {
            int numRetriesRemaining = numRetries - 1;
            s_logger.debug("Retrying IPAssocCommand. Number of retries remaining: " + numRetriesRemaining);
            return execute(cmd, numRetriesRemaining);
        } else {
            results[i++] = IpAssocAnswer.errorResult;
        }
    }
    return new IpAssocAnswer(cmd, results);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) ArrayList(java.util.ArrayList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ExecutionException(com.cloud.utils.exception.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) IOException(java.io.IOException) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 5 with IpAssocAnswer

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

the class NetScalerControlCenterResource method execute.

private synchronized Answer execute(IpAssocCommand cmd, int numRetries) {
    String[] results = new String[cmd.getIpAddresses().length];
    int i = 0;
    IpAddressTO[] ips = cmd.getIpAddresses();
    for (IpAddressTO ip : ips) {
        results[i++] = ip.getPublicIp() + " - success";
    }
    return new IpAssocAnswer(cmd, results);
}
Also used : IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) IpAddressTO(com.cloud.agent.api.to.IpAddressTO)

Aggregations

IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)10 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)10 ExecutionException (com.cloud.utils.exception.ExecutionException)4 IpAssocCommand (com.cloud.agent.api.routing.IpAssocCommand)2 IOException (java.io.IOException)2 ConfigurationException (javax.naming.ConfigurationException)2 Test (org.junit.Test)2 InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1