Search in sources :

Example 71 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class NetworkModelImpl method getIpToServices.

@Override
public Map<PublicIpAddress, Set<Service>> getIpToServices(final List<? extends PublicIpAddress> publicIps, final boolean postApplyRules, final boolean includingFirewall) {
    final Map<PublicIpAddress, Set<Service>> ipToServices = new HashMap<>();
    if (publicIps != null && !publicIps.isEmpty()) {
        final Set<Long> networkSNAT = new HashSet<>();
        for (final PublicIpAddress ip : publicIps) {
            Set<Service> services = ipToServices.get(ip);
            if (services == null) {
                services = new HashSet<>();
            }
            if (ip.isSourceNat()) {
                if (!networkSNAT.contains(ip.getAssociatedWithNetworkId())) {
                    services.add(Service.SourceNat);
                    networkSNAT.add(ip.getAssociatedWithNetworkId());
                } else {
                    final CloudRuntimeException ex = new CloudRuntimeException("Multiple generic soure NAT IPs provided for network");
                    // see the IPAddressVO.java class.
                    final IPAddressVO ipAddr = ApiDBUtils.findIpAddressById(ip.getAssociatedWithNetworkId());
                    String ipAddrUuid = ip.getAssociatedWithNetworkId().toString();
                    if (ipAddr != null) {
                        ipAddrUuid = ipAddr.getUuid();
                    }
                    ex.addProxyObject(ipAddrUuid, "networkId");
                    throw ex;
                }
            }
            ipToServices.put(ip, services);
            // provider
            if (ip.getState() == State.Allocating) {
                continue;
            }
            // check if any active rules are applied on the public IP
            Set<Purpose> purposes = getPublicIpPurposeInRules(ip, false, includingFirewall);
            // Firewall rules didn't cover static NAT
            if (ip.isOneToOneNat() && ip.getAssociatedWithVmId() != null) {
                if (purposes == null) {
                    purposes = new HashSet<>();
                }
                purposes.add(Purpose.StaticNat);
            }
            if (purposes == null || purposes.isEmpty()) {
                // since no active rules are there check if any rules are applied on the public IP but are in
                // revoking state
                purposes = getPublicIpPurposeInRules(ip, true, includingFirewall);
                if (ip.isOneToOneNat()) {
                    if (purposes == null) {
                        purposes = new HashSet<>();
                    }
                    purposes.add(Purpose.StaticNat);
                }
                if (purposes == null || purposes.isEmpty()) {
                    // IP is not being used for any purpose so skip IPAssoc to network service provider
                    continue;
                } else {
                    if (postApplyRules) {
                        // association with the provider
                        if (ip.isSourceNat()) {
                            s_logger.debug("Not releasing ip " + ip.getAddress().addr() + " as it is in use for SourceNat");
                        } else {
                            ip.setState(State.Releasing);
                        }
                    } else {
                        if (ip.getState() == State.Releasing) {
                            // rules are not revoked yet, so don't let the network service provider revoke the IP
                            // association
                            // mark IP is allocated so that IP association will not be removed from the provider
                            ip.setState(State.Allocated);
                        }
                    }
                }
            }
            if (purposes.contains(Purpose.StaticNat)) {
                services.add(Service.StaticNat);
            }
            if (purposes.contains(Purpose.LoadBalancing)) {
                services.add(Service.Lb);
            }
            if (purposes.contains(Purpose.PortForwarding)) {
                services.add(Service.PortForwarding);
            }
            if (purposes.contains(Purpose.Vpn)) {
                services.add(Service.Vpn);
            }
            if (purposes.contains(Purpose.Firewall)) {
                services.add(Service.Firewall);
            }
            if (services.isEmpty()) {
                continue;
            }
            ipToServices.put(ip, services);
        }
    }
    return ipToServices;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Service(com.cloud.legacymodel.network.Network.Service) Purpose(com.cloud.legacymodel.network.FirewallRule.Purpose) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) IPAddressVO(com.cloud.network.dao.IPAddressVO) HashSet(java.util.HashSet)

Example 72 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class IpAddressManagerImpl method assignSystemIp.

@Override
public IpAddress assignSystemIp(final long networkId, final Account owner, final boolean forElasticLb, final boolean forElasticIp) throws InsufficientAddressCapacityException {
    final Network guestNetwork = _networksDao.findById(networkId);
    final NetworkOffering off = _entityMgr.findById(NetworkOffering.class, guestNetwork.getNetworkOfferingId());
    IpAddress ip = null;
    if ((off.getElasticLb() && forElasticLb) || (off.getElasticIp() && forElasticIp)) {
        try {
            s_logger.debug("Allocating system IP address for load balancer rule...");
            // allocate ip
            ip = allocateIP(owner, true, guestNetwork.getDataCenterId());
            // apply ip associations
            ip = associateIPToGuestNetwork(ip.getId(), networkId, true);
        } catch (final ResourceAllocationException ex) {
            throw new CloudRuntimeException("Failed to allocate system ip due to ", ex);
        } catch (final ConcurrentOperationException ex) {
            throw new CloudRuntimeException("Failed to allocate system lb ip due to ", ex);
        } catch (final ResourceUnavailableException ex) {
            throw new CloudRuntimeException("Failed to allocate system lb ip due to ", ex);
        }
        if (ip == null) {
            throw new CloudRuntimeException("Failed to allocate system ip");
        }
    }
    return ip;
}
Also used : NetworkOffering(com.cloud.offering.NetworkOffering) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException)

Example 73 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class Ipv6AddressManagerImpl method assignDirectIp6Address.

@Override
public UserIpv6Address assignDirectIp6Address(final long dcId, final Account owner, final Long networkId, final String requestedIp6) throws InsufficientAddressCapacityException {
    final Network network = _networkDao.findById(networkId);
    if (network == null) {
        return null;
    }
    final List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(networkId);
    if (vlans == null) {
        s_logger.debug("Cannot find related vlan attached to network " + networkId);
        return null;
    }
    String ip = null;
    Vlan ipVlan = null;
    if (requestedIp6 == null) {
        if (!_networkModel.isIP6AddressAvailableInNetwork(networkId)) {
            throw new InsufficientAddressCapacityException("There is no more address available in the network " + network.getName(), DataCenter.class, network.getDataCenterId());
        }
        for (final Vlan vlan : vlans) {
            if (!_networkModel.isIP6AddressAvailableInVlan(vlan.getId())) {
                continue;
            }
            ip = NetUtils.getIp6FromRange(vlan.getIp6Range());
            int count = 0;
            while (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
                ip = NetUtils.getNextIp6InRange(ip, vlan.getIp6Range());
                count++;
                // It's an arbitrate number to prevent the infinite loop
                if (count > _ipv6RetryMax) {
                    ip = null;
                    break;
                }
            }
            if (ip != null) {
                ipVlan = vlan;
            }
        }
        if (ip == null) {
            throw new InsufficientAddressCapacityException("Cannot find a usable IP in the network " + network.getName() + " after " + _ipv6RetryMax + "(network.ipv6.search.retry.max) times retry!", DataCenter.class, network.getDataCenterId());
        }
    } else {
        for (final Vlan vlan : vlans) {
            if (NetUtils.isIp6InRange(requestedIp6, vlan.getIp6Range())) {
                ipVlan = vlan;
                break;
            }
        }
        if (ipVlan == null) {
            throw new CloudRuntimeException("Requested IPv6 is not in the predefined range!");
        }
        ip = requestedIp6;
        if (_ipv6Dao.findByNetworkIdAndIp(networkId, ip) != null) {
            throw new CloudRuntimeException("The requested IP is already taken!");
        }
    }
    final Zone zone = zoneRepository.findById(dcId).orElse(null);
    final Long mac = zone.getMacAddress();
    final Long nextMac = mac + 1;
    zone.setMacAddress(nextMac);
    zoneRepository.save(zone);
    final String macAddress = NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(mac));
    final UserIpv6AddressVO ipVO = new UserIpv6AddressVO(ip, dcId, macAddress, ipVlan.getId());
    ipVO.setPhysicalNetworkId(network.getPhysicalNetworkId());
    ipVO.setSourceNetworkId(networkId);
    ipVO.setState(UserIpv6Address.State.Allocated);
    ipVO.setDomainId(owner.getDomainId());
    ipVO.setAccountId(owner.getAccountId());
    _ipv6Dao.persist(ipVO);
    return ipVO;
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Zone(com.cloud.db.model.Zone) Network(com.cloud.legacymodel.network.Network) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) Vlan(com.cloud.legacymodel.dc.Vlan) VlanVO(com.cloud.dc.VlanVO)

Example 74 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class NetworkModelImpl method getSystemAccountNetworkOfferings.

@Override
public List<NetworkOfferingVO> getSystemAccountNetworkOfferings(final String... offeringNames) {
    final List<NetworkOfferingVO> offerings = new ArrayList<>(offeringNames.length);
    for (final String offeringName : offeringNames) {
        final NetworkOfferingVO network = _systemNetworks.get(offeringName);
        if (network == null) {
            throw new CloudRuntimeException("Unable to find system network profile for " + offeringName);
        }
        offerings.add(network);
    }
    return offerings;
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ArrayList(java.util.ArrayList) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO)

Example 75 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class StorageNetworkManagerImpl method checkOverlapPrivateIpRange.

private void checkOverlapPrivateIpRange(final long podId, final String startIp, final String endIp) {
    final HostPodVO pod = _podDao.findById(podId);
    if (pod == null) {
        throw new CloudRuntimeException("Cannot find pod " + podId);
    }
    final String[] IpRange = pod.getDescription().split("-");
    if ((IpRange[0] == null || IpRange[1] == null) || (!NetUtils.isValidIp4(IpRange[0]) || !NetUtils.isValidIp4(IpRange[1]))) {
        return;
    }
    if (NetUtils.ipRangesOverlap(startIp, endIp, IpRange[0], IpRange[1])) {
        throw new InvalidParameterValueException("The Storage network Start IP and endIP address range overlap with private IP :" + IpRange[0] + ":" + IpRange[1]);
    }
}
Also used : InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) HostPodVO(com.cloud.dc.HostPodVO)

Aggregations

CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)587 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)159 ArrayList (java.util.ArrayList)110 DB (com.cloud.utils.db.DB)90 Account (com.cloud.legacymodel.user.Account)84 SQLException (java.sql.SQLException)84 ActionEvent (com.cloud.event.ActionEvent)73 ConfigurationException (javax.naming.ConfigurationException)73 PreparedStatement (java.sql.PreparedStatement)68 HashMap (java.util.HashMap)68 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)62 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)52 HostVO (com.cloud.host.HostVO)50 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)50 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)47 Answer (com.cloud.legacymodel.communication.answer.Answer)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)45 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 IOException (java.io.IOException)44