Search in sources :

Example 1 with IpAddressTO

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

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

the class IpAssociationConfigItem method generateConfig.

@Override
public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
    final IpAssocCommand command = (IpAssocCommand) cmd;
    final List<IpAddress> ips = new LinkedList<IpAddress>();
    for (final IpAddressTO ip : command.getIpAddresses()) {
        final IpAddress ipAddress = new IpAddress(ip.getPublicIp(), ip.isSourceNat(), ip.isAdd(), ip.isOneToOneNat(), ip.isFirstIP(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getNicDevId(), ip.isNewNic(), ip.getTrafficType().toString());
        ips.add(ipAddress);
    }
    final IpAssociation ipAssociation = new IpAssociation(ips.toArray(new IpAddress[ips.size()]));
    return generateConfigItems(ipAssociation);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) IpAddress(com.cloud.agent.resource.virtualnetwork.model.IpAddress) IpAssociation(com.cloud.agent.resource.virtualnetwork.model.IpAssociation) LinkedList(java.util.LinkedList)

Example 3 with IpAddressTO

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

the class LibvirtComputingResource method cleanupNetworkElementCommand.

protected ExecutionResult cleanupNetworkElementCommand(final IpAssocCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_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();
        final int numOfIps = ips.length;
        int nicNum = 0;
        for (final IpAddressTO ip : ips) {
            if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
                broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
            }
            nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
            if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) {
                // in isolated network eth2 is the default public interface. We don't want to delete it.
                if (nicNum != 2) {
                    vifHotUnPlug(conn, routerName, ip.getVifMacAddress());
                    networkUsage(routerIp, "deleteVif", "eth" + nicNum);
                }
            }
        }
    } 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());
    }
    return new ExecutionResult(true, null);
}
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)

Example 4 with IpAddressTO

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

the class NotAValidCommand method testIpAssocVpcCommand.

@Test
public void testIpAssocVpcCommand() {
    final VirtualRoutingResource routingResource = Mockito.mock(VirtualRoutingResource.class);
    final IpAddressTO[] ips = new IpAddressTO[0];
    final IpAssocVpcCommand ipAssociation = new IpAssocVpcCommand(ips);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getVirtualRoutingResource()).thenReturn(routingResource);
    final Answer answer = wrapper.execute(ipAssociation, citrixResourceBase);
    verify(routingResource, times(1)).executeRequest(ipAssociation);
    // Requires more testing, but the VirtualResourceRouting is quite big.
    assertNull(answer);
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) VirtualRoutingResource(com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource) IpAssocVpcCommand(com.cloud.agent.api.routing.IpAssocVpcCommand) Test(org.junit.Test)

Example 5 with IpAddressTO

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

the class ExternalLoadBalancerDeviceManagerImpl method manageGuestNetworkWithExternalLoadBalancer.

@Override
public boolean manageGuestNetworkWithExternalLoadBalancer(boolean add, Network guestConfig) throws ResourceUnavailableException, InsufficientCapacityException {
    if (guestConfig.getTrafficType() != TrafficType.Guest) {
        s_logger.trace("External load balancer can only be used for guest networks.");
        return false;
    }
    long zoneId = guestConfig.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    HostVO externalLoadBalancer = null;
    if (add) {
        ExternalLoadBalancerDeviceVO lbDeviceVO = null;
        // on restart network, device could have been allocated already, skip allocation if a device is assigned
        lbDeviceVO = getExternalLoadBalancerForNetwork(guestConfig);
        if (lbDeviceVO == null) {
            // allocate a load balancer device for the network
            lbDeviceVO = allocateLoadBalancerForNetwork(guestConfig);
            if (lbDeviceVO == null) {
                String msg = "failed to alloacate a external load balancer for the network " + guestConfig.getId();
                s_logger.error(msg);
                throw new InsufficientNetworkCapacityException(msg, DataCenter.class, guestConfig.getDataCenterId());
            }
        }
        externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
        s_logger.debug("Allocated external load balancer device:" + lbDeviceVO.getId() + " for the network: " + guestConfig.getId());
    } else {
        // find the load balancer device allocated for the network
        ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(guestConfig);
        if (lbDeviceVO == null) {
            s_logger.warn("Network shutdwon requested on external load balancer element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed. So just returning.");
            return true;
        }
        externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
        assert (externalLoadBalancer != null) : "There is no device assigned to this network how did shutdown network ended up here??";
    }
    // Send a command to the external load balancer to implement or shutdown the guest network
    String guestVlanTag = BroadcastDomainType.getValue(guestConfig.getBroadcastUri());
    String selfIp = null;
    String guestVlanNetmask = NetUtils.cidr2Netmask(guestConfig.getCidr());
    Integer networkRate = _networkModel.getNetworkRate(guestConfig.getId(), null);
    if (add) {
        // on restart network, network could have already been implemented. If already implemented then return
        Nic selfipNic = getPlaceholderNic(guestConfig);
        if (selfipNic != null) {
            return true;
        }
        // Acquire a self-ip address from the guest network IP address range
        selfIp = _ipAddrMgr.acquireGuestIpAddress(guestConfig, null);
        if (selfIp == null) {
            String msg = "failed to acquire guest IP address so not implementing the network on the external load balancer ";
            s_logger.error(msg);
            throw new InsufficientNetworkCapacityException(msg, Network.class, guestConfig.getId());
        }
    } else {
        // get the self-ip used by the load balancer
        Nic selfipNic = getPlaceholderNic(guestConfig);
        if (selfipNic == null) {
            s_logger.warn("Network shutdwon requested on external load balancer element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed. So just returning.");
            return true;
        }
        selfIp = selfipNic.getIPv4Address();
    }
    // It's a hack, using isOneToOneNat field for indicate if it's inline or not
    boolean inline = _networkMgr.isNetworkInlineMode(guestConfig);
    IpAddressTO ip = new IpAddressTO(guestConfig.getAccountId(), null, add, false, true, guestVlanTag, selfIp, guestVlanNetmask, null, networkRate, inline);
    IpAddressTO[] ips = new IpAddressTO[1];
    ips[0] = ip;
    IpAssocCommand cmd = new IpAssocCommand(ips);
    Answer answer = _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
    if (answer == null || !answer.getResult()) {
        String action = add ? "implement" : "shutdown";
        String answerDetails = (answer != null) ? answer.getDetails() : null;
        answerDetails = (answerDetails != null) ? " due to " + answerDetails : "";
        String msg = "External load balancer was unable to " + action + " the guest network on the external load balancer in zone " + zone.getName() + answerDetails;
        s_logger.error(msg);
        throw new ResourceUnavailableException(msg, Network.class, guestConfig.getId());
    }
    if (add) {
        // Insert a new NIC for this guest network to reserve the self IP
        _networkMgr.savePlaceholderNic(guestConfig, selfIp, null, null);
    } else {
        // release the self-ip obtained from guest network
        Nic selfipNic = getPlaceholderNic(guestConfig);
        _nicDao.remove(selfipNic.getId());
        // release the load balancer allocated for the network
        boolean releasedLB = freeLoadBalancerForNetwork(guestConfig);
        if (!releasedLB) {
            String msg = "Failed to release the external load balancer used for the network: " + guestConfig.getId();
            s_logger.error(msg);
        }
    }
    if (s_logger.isDebugEnabled()) {
        Account account = _accountDao.findByIdIncludingRemoved(guestConfig.getAccountId());
        String action = add ? "implemented" : "shut down";
        s_logger.debug("External load balancer has " + action + " the guest network for account " + account.getAccountName() + "(id = " + account.getAccountId() + ") with VLAN tag " + guestVlanTag);
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) Nic(com.cloud.vm.Nic) HostVO(com.cloud.host.HostVO) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) DestroyLoadBalancerApplianceAnswer(com.cloud.network.resource.DestroyLoadBalancerApplianceAnswer) Answer(com.cloud.agent.api.Answer) CreateLoadBalancerApplianceAnswer(com.cloud.network.resource.CreateLoadBalancerApplianceAnswer) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException)

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