Search in sources :

Example 21 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method createPrivateNetwork.

@Override
@DB
public Network createPrivateNetwork(final String networkName, final String displayText, final long physicalNetworkId, final String broadcastUriString, final String startIp, String endIp, final String gateway, final String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    final Account owner = _accountMgr.getAccount(networkOwnerId);
    // Get system network offering
    NetworkOfferingVO ntwkOff = null;
    if (networkOfferingId != null) {
        ntwkOff = _networkOfferingDao.findById(networkOfferingId);
    }
    if (ntwkOff == null) {
        ntwkOff = findSystemNetworkOffering(NetworkOffering.DefaultPrivateGatewayNetworkOffering);
    }
    // Validate physical network
    final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNtwk == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" + " having the given id");
        ex.addProxyObject(String.valueOf(physicalNetworkId), "physicalNetworkId");
        throw ex;
    }
    // if end ip is not specified, default it to startIp
    if (!NetUtils.isValidIp4(startIp)) {
        throw new InvalidParameterValueException("Invalid format for the ip address parameter");
    }
    if (endIp == null) {
        endIp = startIp;
    } else if (!NetUtils.isValidIp4(endIp)) {
        throw new InvalidParameterValueException("Invalid format for the endIp address parameter");
    }
    if (!NetUtils.isValidIp4(gateway)) {
        throw new InvalidParameterValueException("Invalid gateway");
    }
    if (!NetUtils.isValidIp4Netmask(netmask)) {
        throw new InvalidParameterValueException("Invalid netmask");
    }
    final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
    final URI uri = BroadcastDomainType.fromString(broadcastUriString);
    final String uriString = uri.toString();
    final BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri);
    // TODO make a test for any supported scheme
    if (!(tiep == BroadcastDomainType.Vlan || tiep == BroadcastDomainType.Lswitch)) {
        throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString);
    }
    final NetworkOfferingVO ntwkOffFinal = ntwkOff;
    try {
        return Transaction.execute(new TransactionCallbackWithException<Network, Exception>() {

            @Override
            public Network doInTransaction(final TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException {
                // lock datacenter as we need to get mac address seq from there
                final DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
                // check if we need to create guest network
                Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
                if (privateNetwork == null) {
                    // create Guest network
                    privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString, null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null, dc.getDns1(), dc.getDns2(), null, null, null);
                    if (privateNetwork != null) {
                        s_logger.debug("Successfully created guest network " + privateNetwork);
                    } else {
                        throw new CloudRuntimeException("Creating guest network failed");
                    }
                } else {
                    s_logger.debug("Private network already exists: " + privateNetwork);
                    // Do not allow multiple private gateways with same Vlan within a VPC
                    if (vpcId != null && vpcId.equals(privateNetwork.getVpcId())) {
                        throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr  " + cidr + "  already exists " + "for Vpc " + vpcId + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                }
                if (vpcId != null) {
                    // add entry to private_ip_address table
                    PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
                    if (privateIp != null) {
                        throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                    final Long mac = dc.getMacAddress();
                    final Long nextMac = mac + 1;
                    dc.setMacAddress(nextMac);
                    privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
                    _privateIpDao.persist(privateIp);
                    _dcDao.update(dc.getId(), dc);
                }
                s_logger.debug("Private network " + privateNetwork + " is created");
                return privateNetwork;
            }
        });
    } catch (final Exception e) {
        ExceptionUtil.rethrowRuntime(e);
        ExceptionUtil.rethrow(e, ResourceAllocationException.class);
        ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
        throw new IllegalStateException(e);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.legacymodel.user.Account) TransactionStatus(com.cloud.utils.db.TransactionStatus) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) URI(java.net.URI) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) SQLException(java.sql.SQLException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) UnknownHostException(java.net.UnknownHostException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) UnsupportedServiceException(com.cloud.legacymodel.exceptions.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) BroadcastDomainType(com.cloud.model.enumeration.BroadcastDomainType) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) DB(com.cloud.utils.db.DB)

Example 22 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method allocateIP.

@Override
@ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "allocating Ip", create = true)
public IpAddress allocateIP(final Account ipOwner, final long zoneId, final Long networkId, final Boolean displayIp) throws ResourceAllocationException, InsufficientAddressCapacityException, ConcurrentOperationException {
    final Account caller = CallContext.current().getCallingAccount();
    final long callerUserId = CallContext.current().getCallingUserId();
    final DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (networkId != null) {
        final Network network = _networksDao.findById(networkId);
        if (network == null) {
            throw new InvalidParameterValueException("Invalid network id is given");
        }
        if (network.getGuestType() == GuestType.Shared) {
            if (zone == null) {
                throw new InvalidParameterValueException("Invalid zone Id is given");
            }
            // if shared network in the advanced zone, then check the caller against the network for 'AccessType.UseNetwork'
            if (zone.getNetworkType() == NetworkType.Advanced) {
                if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
                    _accountMgr.checkAccess(caller, AccessType.UseEntry, false, network);
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Associate IP address called by the user " + callerUserId + " account " + ipOwner.getId());
                    }
                    return _ipAddrMgr.allocateIp(ipOwner, false, caller, callerUserId, zone, displayIp);
                } else {
                    throw new InvalidParameterValueException("Associate IP address can only be called on the shared networks in the advanced zone" + " with Firewall/Source Nat/Static Nat/Port Forwarding/Load balancing services enabled");
                }
            }
        }
    } else {
        _accountMgr.checkAccess(caller, null, false, ipOwner);
    }
    return _ipAddrMgr.allocateIp(ipOwner, false, caller, callerUserId, zone, displayIp);
}
Also used : Account(com.cloud.legacymodel.user.Account) DataCenter(com.cloud.legacymodel.dc.DataCenter) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Network(com.cloud.legacymodel.network.Network) ActionEvent(com.cloud.event.ActionEvent)

Example 23 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class StorageNetworkGuru method release.

@Override
public boolean release(final NicProfile nic, final VirtualMachineProfile vm, final String reservationId) {
    final Network nw = _nwDao.findById(nic.getNetworkId());
    if (!_sNwMgr.isStorageIpRangeAvailable(nw.getDataCenterId())) {
        return super.release(nic, vm, reservationId);
    }
    _sNwMgr.releaseIpAddress(nic.getIPv4Address());
    s_logger.debug("Release an storage ip " + nic.getIPv4Address());
    nic.deallocate();
    return true;
}
Also used : Network(com.cloud.legacymodel.network.Network)

Example 24 with Network

use of com.cloud.legacymodel.network.Network in project cosmic by MissionCriticalCloud.

the class IpAddressManagerImpl method assignDedicateIpAddress.

@DB
@Override
public PublicIp assignDedicateIpAddress(final Account owner, final Long guestNtwkId, final Long vpcId, final long dcId, final boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException {
    final long ownerId = owner.getId();
    PublicIp ip = null;
    try {
        ip = Transaction.execute(new TransactionCallbackWithException<PublicIp, InsufficientAddressCapacityException>() {

            @Override
            public PublicIp doInTransaction(final TransactionStatus status) throws InsufficientAddressCapacityException {
                final Account owner = _accountDao.acquireInLockTable(ownerId);
                if (owner == null) {
                    // this ownerId comes from owner or type Account. See the class "AccountVO" and the annotations in that class
                    // to get the table name and field name that is queried to fill this ownerid.
                    final ConcurrentOperationException ex = new ConcurrentOperationException("Unable to lock account");
                    throw ex;
                }
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("lock account " + ownerId + " is acquired");
                }
                boolean displayIp = true;
                if (guestNtwkId != null) {
                    final Network ntwk = _networksDao.findById(guestNtwkId);
                    displayIp = ntwk.getDisplayNetwork();
                } else if (vpcId != null) {
                    final VpcVO vpc = _vpcDao.findById(vpcId);
                    displayIp = vpc.isDisplay();
                }
                final PublicIp ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId, displayIp);
                final IPAddressVO publicIp = ip.ip();
                markPublicIpAsAllocated(publicIp);
                _ipAddressDao.update(publicIp.getId(), publicIp);
                return ip;
            }
        });
        return ip;
    } finally {
        if (owner != null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Releasing lock account " + ownerId);
            }
            _accountDao.releaseFromLockTable(ownerId);
        }
        if (ip == null) {
            s_logger.error("Unable to get source nat ip address for account " + ownerId);
        }
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) VpcVO(com.cloud.network.vpc.VpcVO) PublicIp(com.cloud.network.addr.PublicIp) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) Network(com.cloud.legacymodel.network.Network) TransactionStatus(com.cloud.utils.db.TransactionStatus) IPAddressVO(com.cloud.network.dao.IPAddressVO) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) DB(com.cloud.utils.db.DB)

Example 25 with Network

use of com.cloud.legacymodel.network.Network 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)

Aggregations

Network (com.cloud.legacymodel.network.Network)160 ArrayList (java.util.ArrayList)57 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)56 Account (com.cloud.legacymodel.user.Account)46 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)42 NetworkOffering (com.cloud.offering.NetworkOffering)36 PhysicalNetwork (com.cloud.network.PhysicalNetwork)34 IPAddressVO (com.cloud.network.dao.IPAddressVO)32 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)30 NetworkVO (com.cloud.network.dao.NetworkVO)28 List (java.util.List)28 Zone (com.cloud.db.model.Zone)27 DB (com.cloud.utils.db.DB)27 NicProfile (com.cloud.vm.NicProfile)26 Nic (com.cloud.legacymodel.network.Nic)21 DataCenter (com.cloud.legacymodel.dc.DataCenter)20 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)20 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)19 DomainRouterVO (com.cloud.vm.DomainRouterVO)18 ActionEvent (com.cloud.event.ActionEvent)17