Search in sources :

Example 11 with IpAddressTO

use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.

the class HypervDirectConnectResource method execute.

protected SetSourceNatAnswer execute(final SetSourceNatCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource SetSourceNatCommand " + s_gson.toJson(cmd));
    }
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = getRouterSshControlIp(cmd);
    final IpAddressTO pubIp = cmd.getIpAddress();
    try {
        final int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, pubIp.getVifMacAddress());
        String args = "";
        args += " -A ";
        args += " -l ";
        args += pubIp.getPublicIp();
        args += " -c ";
        args += "eth" + ethDeviceNum;
        final String command = String.format("%s%s %s", "/opt/cloud/bin/", VRScripts.VPC_SOURCE_NAT, args);
        final Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command);
        if (!result.first()) {
            final String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.second();
            s_logger.error(msg);
            return new SetSourceNatAnswer(cmd, false, msg);
        }
        return new SetSourceNatAnswer(cmd, true, "success");
    } catch (final Exception e) {
        final String msg = "Ip SNAT failure due to " + e.toString();
        s_logger.error(msg, e);
        return new SetSourceNatAnswer(cmd, false, msg);
    }
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) SetSourceNatAnswer(com.cloud.agent.api.routing.SetSourceNatAnswer)

Example 12 with IpAddressTO

use of com.cloud.agent.api.to.IpAddressTO 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 13 with IpAddressTO

use of com.cloud.agent.api.to.IpAddressTO in project cloudstack by apache.

the class HypervDirectConnectResource method prepareNetworkElementCommand.

private ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        for (final IpAddressTO ip : ips) {
            final URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
            if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
                throw new InternalErrorException("Invalid Broadcast URI " + ip.getBroadcastUri());
            }
            final String vlanId = BroadcastDomainType.getValue(broadcastUri);
            int publicNicInfo = -1;
            publicNicInfo = getVmNics(routerName, vlanId);
            if (publicNicInfo < 0) {
                if (ip.isAdd()) {
                    throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
                } else {
                    s_logger.debug("VIF to deassociate IP with does not exist, return success");
                    continue;
                }
            }
            ip.setNicDevId(publicNicInfo);
        }
    } catch (final Exception e) {
        s_logger.error("Prepare Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.toString());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) URI(java.net.URI) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException)

Example 14 with IpAddressTO

use of com.cloud.agent.api.to.IpAddressTO 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 15 with IpAddressTO

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

Aggregations

IpAddressTO (com.cloud.agent.api.to.IpAddressTO)41 ExecutionResult (com.cloud.utils.ExecutionResult)16 InternalErrorException (com.cloud.exception.InternalErrorException)14 IOException (java.io.IOException)12 ConfigurationException (javax.naming.ConfigurationException)12 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)9 IpAssocCommand (com.cloud.agent.api.routing.IpAssocCommand)8 ArrayList (java.util.ArrayList)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 URISyntaxException (java.net.URISyntaxException)7 HashMap (java.util.HashMap)7 Answer (com.cloud.agent.api.Answer)6 ConnectException (java.net.ConnectException)6 RemoteException (java.rmi.RemoteException)6 IpAssocVpcCommand (com.cloud.agent.api.routing.IpAssocVpcCommand)5 URI (java.net.URI)5 Connect (org.libvirt.Connect)5 LibvirtException (org.libvirt.LibvirtException)5 DataCenterVO (com.cloud.dc.DataCenterVO)4 ExecutionException (com.cloud.utils.exception.ExecutionException)4