Search in sources :

Example 51 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class NuageVspGuestNetworkGuruTest method testReserve.

@Test
public void testReserve() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, URISyntaxException {
    final NetworkVO network = mock(NetworkVO.class);
    when(network.getId()).thenReturn(NETWORK_ID);
    when(network.getUuid()).thenReturn("aaaaaa");
    when(network.getDataCenterId()).thenReturn(NETWORK_ID);
    when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    when(network.getDomainId()).thenReturn(NETWORK_ID);
    when(network.getAccountId()).thenReturn(NETWORK_ID);
    when(network.getVpcId()).thenReturn(null);
    when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
    final DataCenterVO dataCenter = mock(DataCenterVO.class);
    when(_dataCenterDao.findById(NETWORK_ID)).thenReturn(dataCenter);
    final AccountVO networksAccount = mock(AccountVO.class);
    when(networksAccount.getId()).thenReturn(NETWORK_ID);
    when(networksAccount.getUuid()).thenReturn("aaaa-abbbb");
    when(networksAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_NORMAL);
    when(_accountDao.findById(NETWORK_ID)).thenReturn(networksAccount);
    final DomainVO networksDomain = mock(DomainVO.class);
    when(networksDomain.getId()).thenReturn(NETWORK_ID);
    when(networksDomain.getUuid()).thenReturn("aaaaa-bbbbb");
    when(_domainDao.findById(NETWORK_ID)).thenReturn(networksDomain);
    final NicVO nicvo = mock(NicVO.class);
    when(nicvo.getId()).thenReturn(NETWORK_ID);
    when(nicvo.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    when(nicvo.getUuid()).thenReturn("aaaa-fffff");
    when(nicvo.getNetworkId()).thenReturn(NETWORK_ID);
    when(nicvo.getInstanceId()).thenReturn(NETWORK_ID);
    when(_nicDao.findById(NETWORK_ID)).thenReturn(nicvo);
    when(_nicDao.findDefaultNicForVM(NETWORK_ID)).thenReturn(nicvo);
    final VirtualMachine vm = mock(VirtualMachine.class);
    when(vm.getId()).thenReturn(NETWORK_ID);
    when(vm.getType()).thenReturn(VirtualMachine.Type.User);
    final VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
    when(vmProfile.getType()).thenReturn(VirtualMachine.Type.User);
    when(vmProfile.getInstanceName()).thenReturn("");
    when(vmProfile.getUuid()).thenReturn("aaaa-bbbbb");
    when(vmProfile.getVirtualMachine()).thenReturn(vm);
    NicProfile nicProfile = mock(NicProfile.class);
    when(nicProfile.getUuid()).thenReturn("aaa-bbbb");
    when(nicProfile.getId()).thenReturn(NETWORK_ID);
    when(nicProfile.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    final NetworkOfferingVO ntwkoffering = mock(NetworkOfferingVO.class);
    when(ntwkoffering.getId()).thenReturn(NETWORK_ID);
    when(_networkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffering);
    when(_networkDao.acquireInLockTable(NETWORK_ID, 1200)).thenReturn(network);
    when(_ipAddressDao.findByVmIdAndNetworkId(NETWORK_ID, NETWORK_ID)).thenReturn(null);
    when(_domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
    final Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentManager.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
    final ReservationContext reservationContext = mock(ReservationContext.class);
    when(reservationContext.getAccount()).thenReturn(networksAccount);
    when(reservationContext.getDomain()).thenReturn(networksDomain);
    _nuageVspGuestNetworkGuru.reserve(nicProfile, network, vmProfile, mock(DeployDestination.class), reservationContext);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) AccountVO(com.cloud.user.AccountVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) Answer(com.cloud.agent.api.Answer) DeployDestination(com.cloud.deploy.DeployDestination) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine) NuageTest(com.cloud.NuageTest) Test(org.junit.Test)

Example 52 with DataCenterVO

use of com.cloud.dc.DataCenterVO 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)

Example 53 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method applyLoadBalancerRules.

@Override
public boolean applyLoadBalancerRules(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException {
    // Find the external load balancer in this zone
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    if (loadBalancingRules == null || loadBalancingRules.isEmpty()) {
        return true;
    }
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        s_logger.warn("There is no external load balancer device assigned to this network either network is not implement are already shutdown so just returning");
        return true;
    }
    HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
    boolean externalLoadBalancerIsInline = _networkMgr.isNetworkInlineMode(network);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External load balancer was asked to apply LB rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return true;
    }
    List<LoadBalancerTO> loadBalancersToApply = new ArrayList<LoadBalancerTO>();
    List<MappingState> mappingStates = new ArrayList<MappingState>();
    for (int i = 0; i < loadBalancingRules.size(); i++) {
        LoadBalancingRule rule = loadBalancingRules.get(i);
        boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke));
        String protocol = rule.getProtocol();
        String algorithm = rule.getAlgorithm();
        String uuid = rule.getUuid();
        String srcIp = rule.getSourceIp().addr();
        int srcPort = rule.getSourcePortStart();
        List<LbDestination> destinations = rule.getDestinations();
        if (externalLoadBalancerIsInline) {
            long ipId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId();
            MappingNic nic = getLoadBalancingIpNic(zone, network, ipId, revoked, null);
            mappingStates.add(nic.getState());
            Nic loadBalancingIpNic = nic.getNic();
            if (loadBalancingIpNic == null) {
                continue;
            }
            // Change the source IP address for the load balancing rule to be the load balancing IP address
            srcIp = loadBalancingIpNic.getIPv4Address();
        }
        if ((destinations != null && !destinations.isEmpty()) || rule.isAutoScaleConfig()) {
            boolean inline = _networkMgr.isNetworkInlineMode(network);
            LoadBalancerTO loadBalancer = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, rule.getStickinessPolicies(), rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol());
            if (rule.isAutoScaleConfig()) {
                loadBalancer.setAutoScaleVmGroup(rule.getAutoScaleVmGroup());
            }
            loadBalancersToApply.add(loadBalancer);
        }
    }
    try {
        if (loadBalancersToApply.size() > 0) {
            int numLoadBalancersForCommand = loadBalancersToApply.size();
            LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]);
            LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(loadBalancersForCommand, null);
            long guestVlanTag = Integer.parseInt(BroadcastDomainType.getValue(network.getBroadcastUri()));
            cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag));
            Answer answer = _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
            if (answer == null || !answer.getResult()) {
                String details = (answer != null) ? answer.getDetails() : "details unavailable";
                String msg = "Unable to apply load balancer rules to the external load balancer appliance in zone " + zone.getName() + " due to: " + details + ".";
                s_logger.error(msg);
                throw new ResourceUnavailableException(msg, DataCenter.class, network.getDataCenterId());
            }
        }
    } catch (Exception ex) {
        if (externalLoadBalancerIsInline) {
            s_logger.error("Rollbacking static nat operation of inline mode load balancing due to error on applying LB rules!");
            String existedGuestIp = loadBalancersToApply.get(0).getSrcIp();
            // Rollback static NAT operation in current session
            for (int i = 0; i < loadBalancingRules.size(); i++) {
                LoadBalancingRule rule = loadBalancingRules.get(i);
                MappingState state = mappingStates.get(i);
                boolean revoke;
                if (state == MappingState.Create) {
                    revoke = true;
                } else if (state == MappingState.Remove) {
                    revoke = false;
                } else {
                    continue;
                }
                long sourceIpId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId();
                getLoadBalancingIpNic(zone, network, sourceIpId, revoke, existedGuestIp);
            }
        }
        throw new ResourceUnavailableException(ex.getMessage(), DataCenter.class, network.getDataCenterId());
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) Nic(com.cloud.vm.Nic) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) HostVO(com.cloud.host.HostVO) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) 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) DataCenter(com.cloud.dc.DataCenter) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 54 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method applyStaticNatRules.

public boolean applyStaticNatRules(Network network, List<? extends StaticNat> rules) throws ResourceUnavailableException {
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
    HostVO externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
    assert (externalFirewall != null);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External firewall was asked to apply firewall rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return true;
    }
    List<StaticNatRuleTO> staticNatRules = new ArrayList<StaticNatRuleTO>();
    for (StaticNat rule : rules) {
        IpAddress sourceIp = _networkModel.getIp(rule.getSourceIpAddressId());
        Vlan vlan = _vlanDao.findById(sourceIp.getVlanId());
        StaticNatRuleTO ruleTO = new StaticNatRuleTO(0, vlan.getVlanTag(), sourceIp.getAddress().addr(), -1, -1, rule.getDestIpAddress(), -1, -1, "any", rule.isForRevoke(), false);
        staticNatRules.add(ruleTO);
    }
    sendStaticNatRules(staticNatRules, zone, externalFirewall.getId());
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) StaticNatRuleTO(com.cloud.agent.api.to.StaticNatRuleTO) ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) ArrayList(java.util.ArrayList) Vlan(com.cloud.dc.Vlan) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat)

Example 55 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method manageRemoteAccessVpnUsers.

public boolean manageRemoteAccessVpnUsers(Network network, RemoteAccessVpn vpn, List<? extends VpnUser> vpnUsers) throws ResourceUnavailableException {
    ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
    HostVO externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
    if (externalFirewall == null) {
        return false;
    }
    List<VpnUser> addUsers = new ArrayList<VpnUser>();
    List<VpnUser> removeUsers = new ArrayList<VpnUser>();
    for (VpnUser user : vpnUsers) {
        if (user.getState() == VpnUser.State.Add || user.getState() == VpnUser.State.Active) {
            addUsers.add(user);
        } else if (user.getState() == VpnUser.State.Revoke) {
            removeUsers.add(user);
        }
    }
    VpnUsersCfgCommand addUsersCmd = new VpnUsersCfgCommand(addUsers, removeUsers);
    addUsersCmd.setAccessDetail(NetworkElementCommand.ACCOUNT_ID, String.valueOf(network.getAccountId()));
    addUsersCmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR, network.getCidr());
    Answer answer = _agentMgr.easySend(externalFirewall.getId(), addUsersCmd);
    if (answer == null || !answer.getResult()) {
        String details = (answer != null) ? answer.getDetails() : "details unavailable";
        DataCenterVO zone = _dcDao.findById(network.getDataCenterId());
        String msg = "External firewall was unable to add remote access users in zone " + zone.getName() + " due to: " + details + ".";
        s_logger.error(msg);
        throw new ResourceUnavailableException(msg, DataCenter.class, zone.getId());
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) VpnUsersCfgCommand(com.cloud.agent.api.routing.VpnUsersCfgCommand) Answer(com.cloud.agent.api.Answer) ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) HostVO(com.cloud.host.HostVO)

Aggregations

DataCenterVO (com.cloud.dc.DataCenterVO)214 ArrayList (java.util.ArrayList)60 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)53 HostVO (com.cloud.host.HostVO)42 Account (com.cloud.user.Account)37 NetworkVO (com.cloud.network.dao.NetworkVO)35 DomainRouterVO (com.cloud.vm.DomainRouterVO)33 HostPodVO (com.cloud.dc.HostPodVO)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)30 ClusterVO (com.cloud.dc.ClusterVO)27 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)27 DB (com.cloud.utils.db.DB)26 Network (com.cloud.network.Network)25 HashMap (java.util.HashMap)25 ConfigurationException (javax.naming.ConfigurationException)25 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)20 Test (org.junit.Test)20 NicProfile (com.cloud.vm.NicProfile)19 ActionEvent (com.cloud.event.ActionEvent)18