Search in sources :

Example 86 with VlanVO

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

the class VlanDaoImpl method listVlansForPodByType.

@Override
public List<VlanVO> listVlansForPodByType(long podId, VlanType vlanType) {
    // FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
    List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
    List<VlanVO> result = new ArrayList<VlanVO>();
    for (PodVlanMapVO pvmvo : vlanMaps) {
        VlanVO vlan = findById(pvmvo.getVlanDbId());
        if (vlan.getVlanType() == vlanType) {
            result.add(vlan);
        }
    }
    return result;
}
Also used : PodVlanMapVO(com.cloud.dc.PodVlanMapVO) ArrayList(java.util.ArrayList) VlanVO(com.cloud.dc.VlanVO)

Example 87 with VlanVO

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

the class CiscoVnmcElementTest method applyStaticNatsTest.

@Test
public void applyStaticNatsTest() throws ResourceUnavailableException {
    URI uri = URI.create("vlan://123");
    Network network = mock(Network.class);
    when(network.getId()).thenReturn(1L);
    when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
    when(network.getDataCenterId()).thenReturn(1L);
    when(network.getBroadcastUri()).thenReturn(uri);
    when(network.getCidr()).thenReturn("1.1.1.0/24");
    when(network.getState()).thenReturn(Network.State.Implemented);
    Ip ip = mock(Ip.class);
    when(ip.addr()).thenReturn("1.2.3.4");
    IpAddress ipAddress = mock(IpAddress.class);
    when(ipAddress.getAddress()).thenReturn(ip);
    when(ipAddress.getVlanId()).thenReturn(1L);
    when(_networkModel.getIp(anyLong())).thenReturn(ipAddress);
    when(_networkModel.isProviderSupportServiceInNetwork(network.getId(), Service.StaticNat, Provider.CiscoVnmc)).thenReturn(true);
    List<CiscoVnmcControllerVO> devices = new ArrayList<CiscoVnmcControllerVO>();
    devices.add(mock(CiscoVnmcControllerVO.class));
    when(_ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId())).thenReturn(devices);
    when(_networkAsa1000vMapDao.findByNetworkId(network.getId())).thenReturn(mock(NetworkAsa1000vMapVO.class));
    HostVO hostVO = mock(HostVO.class);
    when(hostVO.getId()).thenReturn(1L);
    when(_hostDao.findById(anyLong())).thenReturn(hostVO);
    VlanVO vlanVO = mock(VlanVO.class);
    when(vlanVO.getVlanTag()).thenReturn(null);
    when(_vlanDao.findById(anyLong())).thenReturn(vlanVO);
    StaticNat rule = mock(StaticNat.class);
    when(rule.getSourceIpAddressId()).thenReturn(1L);
    when(rule.getDestIpAddress()).thenReturn("1.2.3.4");
    when(rule.isForRevoke()).thenReturn(false);
    List<StaticNat> rules = new ArrayList<StaticNat>();
    rules.add(rule);
    Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentMgr.easySend(anyLong(), any(SetStaticNatRulesCommand.class))).thenReturn(answer);
    assertTrue(_element.applyStaticNats(network, rules));
}
Also used : SetStaticNatRulesCommand(com.cloud.agent.api.routing.SetStaticNatRulesCommand) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) URI(java.net.URI) HostVO(com.cloud.host.HostVO) StaticNat(com.cloud.network.rules.StaticNat) Answer(com.cloud.agent.api.Answer) Network(com.cloud.network.Network) NetworkAsa1000vMapVO(com.cloud.network.cisco.NetworkAsa1000vMapVO) IpAddress(com.cloud.network.IpAddress) CiscoVnmcControllerVO(com.cloud.network.cisco.CiscoVnmcControllerVO) VlanVO(com.cloud.dc.VlanVO) Test(org.junit.Test)

Example 88 with VlanVO

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

the class ResourceLimitManagerImpl method calculatePublicIpForAccount.

private long calculatePublicIpForAccount(long accountId) {
    Long dedicatedCount = 0L;
    Long allocatedCount = 0L;
    List<VlanVO> dedicatedVlans = _vlanDao.listDedicatedVlans(accountId);
    for (VlanVO dedicatedVlan : dedicatedVlans) {
        List<IPAddressVO> ips = _ipAddressDao.listByVlanId(dedicatedVlan.getId());
        dedicatedCount += new Long(ips.size());
    }
    allocatedCount = _ipAddressDao.countAllocatedIPsForAccount(accountId);
    if (dedicatedCount > allocatedCount) {
        return dedicatedCount;
    } else {
        return allocatedCount;
    }
}
Also used : IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO)

Example 89 with VlanVO

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

the class NiciraNvpElement method sharedNetworkSupportNumericalVlanId.

private boolean sharedNetworkSupportNumericalVlanId(Network network, String lSwitchUuid, String ownerName, HostVO niciraNvpHost) {
    List<VlanVO> networkVlans = vlanDao.listVlansByNetworkId(network.getId());
    if (networkVlans.size() == 1) {
        for (VlanVO vlanVO : networkVlans) {
            long vlanId = Long.parseLong(vlanVO.getVlanTag());
            String l2GatewayServiceUuid = niciraNvpHost.getDetail("l2gatewayserviceuuid");
            if (l2GatewayServiceUuid == null) {
                throw new CloudRuntimeException("No L2 Gateway Service Uuid found on " + niciraNvpHost.getName());
            }
            ConfigureSharedNetworkVlanIdCommand cmd = new ConfigureSharedNetworkVlanIdCommand(lSwitchUuid, l2GatewayServiceUuid, vlanId, ownerName, network.getId());
            ConfigureSharedNetworkVlanIdAnswer answer = (ConfigureSharedNetworkVlanIdAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
            if (answer.getResult() == false) {
                s_logger.error("Failed to configure Shared network " + network.getDisplayText());
                return false;
            }
        }
    }
    return true;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConfigureSharedNetworkVlanIdCommand(com.cloud.agent.api.ConfigureSharedNetworkVlanIdCommand) VlanVO(com.cloud.dc.VlanVO) ConfigureSharedNetworkVlanIdAnswer(com.cloud.agent.api.ConfigureSharedNetworkVlanIdAnswer)

Example 90 with VlanVO

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

the class ExternalFirewallDeviceManagerImpl method manageGuestNetworkWithExternalFirewall.

@Override
public boolean manageGuestNetworkWithExternalFirewall(boolean add, Network network) throws ResourceUnavailableException, InsufficientCapacityException {
    if (network.getTrafficType() != TrafficType.Guest) {
        s_logger.trace("External firewall can only be used for add/remove guest networks.");
        return false;
    }
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    HostVO externalFirewall = null;
    if (add) {
        GlobalLock deviceMapLock = GlobalLock.getInternLock("NetworkFirewallDeviceMap");
        try {
            if (deviceMapLock.lock(120)) {
                try {
                    ExternalFirewallDeviceVO device = findSuitableFirewallForNetwork(network);
                    long externalFirewallId = device.getId();
                    NetworkExternalFirewallVO networkFW = new NetworkExternalFirewallVO(network.getId(), externalFirewallId);
                    _networkExternalFirewallDao.persist(networkFW);
                    externalFirewall = _hostDao.findById(device.getHostId());
                } finally {
                    deviceMapLock.unlock();
                }
            }
        } finally {
            deviceMapLock.releaseRef();
        }
    } else {
        ExternalFirewallDeviceVO fwDeviceVO = getExternalFirewallForNetwork(network);
        if (fwDeviceVO == null) {
            s_logger.warn("Network shutdown requested on external firewall element, which did not implement the network." + " Either network implement failed half way through or already network shutdown is completed.");
            return true;
        }
        externalFirewall = _hostDao.findById(fwDeviceVO.getHostId());
    }
    Account account = _accountDao.findByIdIncludingRemoved(network.getAccountId());
    NetworkOffering offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
    boolean sharedSourceNat = offering.isSharedSourceNat();
    IPAddressVO sourceNatIp = null;
    if (!sharedSourceNat) {
        // Get the source NAT IP address for this network
        List<? extends IpAddress> sourceNatIps = _networkModel.listPublicIpsAssignedToAccount(network.getAccountId(), zoneId, true);
        for (IpAddress ipAddress : sourceNatIps) {
            if (ipAddress.getAssociatedWithNetworkId().longValue() == network.getId()) {
                sourceNatIp = _ipAddressDao.findById(ipAddress.getId());
                break;
            }
        }
        if (sourceNatIp == null) {
            String errorMsg = "External firewall was unable to find the source NAT IP address for network " + network.getName();
            s_logger.error(errorMsg);
            return true;
        }
    }
    // Send a command to the external firewall to implement or shutdown the guest network
    long guestVlanTag = Long.parseLong(BroadcastDomainType.getValue(network.getBroadcastUri()));
    String guestVlanGateway = network.getGateway();
    String guestVlanCidr = network.getCidr();
    String sourceNatIpAddress = null;
    String publicVlanTag = null;
    if (sourceNatIp != null) {
        sourceNatIpAddress = sourceNatIp.getAddress().addr();
        VlanVO publicVlan = _vlanDao.findById(sourceNatIp.getVlanId());
        publicVlanTag = publicVlan.getVlanTag();
    }
    // Get network rate
    Integer networkRate = _networkModel.getNetworkRate(network.getId(), null);
    IpAddressTO ip = new IpAddressTO(account.getAccountId(), sourceNatIpAddress, add, false, !sharedSourceNat, publicVlanTag, null, null, null, networkRate, false);
    IpAddressTO[] ips = new IpAddressTO[1];
    ips[0] = ip;
    IpAssocCommand cmd = new IpAssocCommand(ips);
    cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_GATEWAY, guestVlanGateway);
    cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR, guestVlanCidr);
    cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag));
    Answer answer = _agentMgr.easySend(externalFirewall.getId(), cmd);
    List<String> reservedIpAddressesForGuestNetwork = _nicDao.listIpAddressInNetwork(network.getId());
    if (answer == null || !answer.getResult()) {
        String action = add ? "implement" : "shutdown";
        String answerDetails = (answer != null) ? answer.getDetails() : "answer was null";
        String msg = "External firewall was unable to " + action + " the guest network on the external firewall in zone " + zone.getName() + " due to " + answerDetails;
        s_logger.error(msg);
        if (!add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
            // If we failed the implementation as well, then just return, no complain
            s_logger.error("Skip the shutdown of guest network on SRX because it seems we didn't implement it as well");
            return true;
        }
        throw new ResourceUnavailableException(msg, DataCenter.class, zoneId);
    }
    if (add && (!reservedIpAddressesForGuestNetwork.contains(network.getGateway()))) {
        // Insert a new NIC for this guest network to reserve the gateway address
        _networkMgr.savePlaceholderNic(network, network.getGateway(), null, null);
    }
    // Delete any mappings used for inline external load balancers in this network
    List<NicVO> nicsInNetwork = _nicDao.listByNetworkId(network.getId());
    for (NicVO nic : nicsInNetwork) {
        InlineLoadBalancerNicMapVO mapping = _inlineLoadBalancerNicMapDao.findByNicId(nic.getId());
        if (mapping != null) {
            _nicDao.expunge(mapping.getNicId());
            _inlineLoadBalancerNicMapDao.expunge(mapping.getId());
        }
    }
    // on network shutdown, delete placeHolder nics used for the firewall device
    if (!add) {
        List<NicVO> nics = _nicDao.listByNetworkId(network.getId());
        for (NicVO nic : nics) {
            if (nic.getVmType() == null && nic.getReservationStrategy().equals(ReservationStrategy.PlaceHolder) && nic.getIPv4Address().equals(network.getGateway())) {
                s_logger.debug("Removing placeholder nic " + nic + " for the network " + network);
                _nicDao.remove(nic.getId());
            }
        }
        freeFirewallForNetwork(network);
    }
    String action = add ? "implemented" : "shut down";
    s_logger.debug("External firewall 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) ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) NetworkOffering(com.cloud.offering.NetworkOffering) InlineLoadBalancerNicMapVO(com.cloud.network.dao.InlineLoadBalancerNicMapVO) HostVO(com.cloud.host.HostVO) GlobalLock(com.cloud.utils.db.GlobalLock) Answer(com.cloud.agent.api.Answer) NetworkExternalFirewallVO(com.cloud.network.dao.NetworkExternalFirewallVO) IpAssocCommand(com.cloud.agent.api.routing.IpAssocCommand) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) NicVO(com.cloud.vm.NicVO)

Aggregations

VlanVO (com.cloud.dc.VlanVO)103 ArrayList (java.util.ArrayList)39 IPAddressVO (com.cloud.network.dao.IPAddressVO)25 DB (com.cloud.utils.db.DB)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)21 TransactionStatus (com.cloud.utils.db.TransactionStatus)18 Network (com.cloud.network.Network)17 Account (com.cloud.user.Account)17 AccountVlanMapVO (com.cloud.dc.AccountVlanMapVO)15 List (java.util.List)15 DomainVlanMapVO (com.cloud.dc.DomainVlanMapVO)12 ActionEvent (com.cloud.event.ActionEvent)12 HostVO (com.cloud.host.HostVO)12 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)12 Vlan (com.cloud.dc.Vlan)11 NetworkVO (com.cloud.network.dao.NetworkVO)11 DataCenter (com.cloud.dc.DataCenter)10 DataCenterVO (com.cloud.dc.DataCenterVO)10 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)10