Search in sources :

Example 31 with IpAddressTO

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) {
    Connect conn;
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final IpAddressTO[] ips = cmd.getIpAddresses();
        Integer devNum = 0;
        final Map<String, Integer> broadcastUriToNicNum = new HashMap<String, Integer>();
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            final String pluggedVlan = pluggedNic.getBrName();
            if (pluggedVlan.equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriToNicNum.put("LinkLocal", devNum);
            } else if (pluggedVlan.equalsIgnoreCase(_publicBridgeName) || pluggedVlan.equalsIgnoreCase(_privBridgeName) || pluggedVlan.equalsIgnoreCase(_guestBridgeName)) {
                broadcastUriToNicNum.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), devNum);
            } else {
                broadcastUriToNicNum.put(getBroadcastUriFromBridge(pluggedVlan), devNum);
            }
            devNum++;
        }
        for (final IpAddressTO ip : ips) {
            ip.setNicDevId(broadcastUriToNicNum.get(ip.getBroadcastUri()));
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult)

Example 32 with IpAddressTO

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

the class NetscalerResource method execute.

private synchronized Answer execute(final IpAssocCommand cmd, final int numRetries) {
    if (_isSdx) {
        return Answer.createUnsupportedCommandAnswer(cmd);
    }
    final String[] results = new String[cmd.getIpAddresses().length];
    int i = 0;
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        for (final IpAddressTO ip : ips) {
            final long guestVlanTag = Long.parseLong(ip.getBroadcastUri());
            final String vlanSelfIp = ip.getVlanGateway();
            final String vlanNetmask = ip.getVlanNetmask();
            if (ip.isAdd()) {
                // Add a new guest VLAN and its subnet and bind it to private interface
                addGuestVlanAndSubnet(guestVlanTag, vlanSelfIp, vlanNetmask, true);
            } else {
                // Check and delete guest VLAN with this tag, self IP, and netmask
                deleteGuestVlan(guestVlanTag, vlanSelfIp, vlanNetmask);
            }
            saveConfiguration();
            results[i++] = ip.getPublicIp() + " - success";
            final String action = ip.isAdd() ? "associate" : "remove";
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Netscaler load balancer " + _ip + " successfully executed IPAssocCommand to " + action + " IP " + ip);
            }
        }
    } catch (final ExecutionException e) {
        s_logger.error("Netscaler loadbalancer " + _ip + " failed to execute IPAssocCommand due to " + e.getMessage());
        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 33 with IpAddressTO

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

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        for (final IpAddressTO ip : ips) {
            final VM router = getVM(conn, routerName);
            final NicTO nic = new NicTO();
            nic.setMac(ip.getVifMacAddress());
            nic.setType(ip.getTrafficType());
            if (ip.getBroadcastUri() == null) {
                nic.setBroadcastType(BroadcastDomainType.Native);
            } else {
                final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
                nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
                nic.setBroadcastUri(uri);
            }
            nic.setDeviceId(0);
            nic.setNetworkRateMbps(ip.getNetworkRate());
            nic.setName(ip.getNetworkName());
            final Network network = getNetwork(conn, nic);
            // Determine the correct VIF on DomR to associate/disassociate
            // the
            // IP address with
            VIF correctVif = getCorrectVif(conn, router, network);
            // If we are associating an IP address and DomR doesn't have a
            // VIF
            // for the specified vlan ID, we need to add a VIF
            // If we are disassociating the last IP address in the VLAN, we
            // need
            // to remove a VIF
            boolean addVif = false;
            if (ip.isAdd() && correctVif == null) {
                addVif = true;
            }
            if (addVif) {
                // Add a new VIF to DomR
                final String vifDeviceNum = getLowestAvailableVIFDeviceNum(conn, router);
                if (vifDeviceNum == null) {
                    throw new InternalErrorException("There were no more available slots for a new VIF on router: " + router.getNameLabel(conn));
                }
                nic.setDeviceId(Integer.parseInt(vifDeviceNum));
                correctVif = createVif(conn, routerName, router, null, nic);
                correctVif.plug(conn);
                // Add iptables rule for network usage
                networkUsage(conn, routerIp, "addVif", "eth" + correctVif.getDevice(conn));
            }
            if (ip.isAdd() && correctVif == null) {
                throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
            }
            if (correctVif != null) {
                ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
                ip.setNewNic(addVif);
            }
        }
    } catch (final InternalErrorException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final Exception e) {
        return new ExecutionResult(false, e.getMessage());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) NicTO(com.cloud.agent.api.to.NicTO)

Example 34 with IpAddressTO

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final SetSourceNatCommand cmd) {
    Connect conn;
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final IpAddressTO pubIP = cmd.getIpAddress();
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        Integer devNum = 0;
        final String pubVlan = pubIP.getBroadcastUri();
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            final String pluggedVlanBr = pluggedNic.getBrName();
            final String pluggedVlanId = getBroadcastUriFromBridge(pluggedVlanBr);
            if (pubVlan.equalsIgnoreCase(Vlan.UNTAGGED) && pluggedVlanBr.equalsIgnoreCase(_publicBridgeName)) {
                break;
            } else if (pluggedVlanBr.equalsIgnoreCase(_linkLocalBridgeName)) {
            /*skip over, no physical bridge device exists*/
            } else if (pluggedVlanId == null) {
                /*this should only be true in the case of link local bridge*/
                return new ExecutionResult(false, "unable to find the vlan id for bridge " + pluggedVlanBr + " when attempting to set up" + pubVlan + " on router " + routerName);
            } else if (pluggedVlanId.equals(pubVlan)) {
                break;
            }
            devNum++;
        }
        pubIP.setNicDevId(devNum);
        return new ExecutionResult(true, "success");
    } catch (final LibvirtException e) {
        final String msg = "Ip SNAT failure due to " + e.toString();
        s_logger.error(msg, e);
        return new ExecutionResult(false, msg);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult)

Example 35 with IpAddressTO

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

public ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    Connect conn;
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> nics = getInterfaces(conn, routerName);
        final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (final InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
                    broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
                } else {
                    final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
                    broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
                }
            }
            nicPos++;
        }
        final IpAddressTO[] ips = cmd.getIpAddresses();
        int nicNum = 0;
        for (final IpAddressTO ip : ips) {
            boolean newNic = false;
            if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
                broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
                newNic = true;
            }
            nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            ip.setNicDevId(nicNum);
            ip.setNewNic(newNic);
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final InternalErrorException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    }
}
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) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)

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