Search in sources :

Example 26 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class IpAddressManagerImpl method assignDedicateIpAddress.

@DB
@Override
public PublicIp assignDedicateIpAddress(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(TransactionStatus status) throws InsufficientAddressCapacityException {
                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.
                    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) {
                    Network ntwk = _networksDao.findById(guestNtwkId);
                    displayIp = ntwk.getDisplayNetwork();
                } else if (vpcId != null) {
                    VpcVO vpc = _vpcDao.findById(vpcId);
                    displayIp = vpc.isDisplay();
                }
                PublicIp ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId, displayIp);
                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.user.Account) VpcVO(com.cloud.network.vpc.VpcVO) PublicIp(com.cloud.network.addr.PublicIp) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) TransactionStatus(com.cloud.utils.db.TransactionStatus) IPAddressVO(com.cloud.network.dao.IPAddressVO) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) DB(com.cloud.utils.db.DB)

Example 27 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class IpAddressManagerImpl method disassociatePublicIpAddress.

@Override
@DB
public boolean disassociatePublicIpAddress(long addrId, long userId, Account caller) {
    boolean success = true;
    // Cleanup all ip address resources - PF/LB/Static nat rules
    if (!cleanupIpResources(addrId, userId, caller)) {
        success = false;
        s_logger.warn("Failed to release resources for ip address id=" + addrId);
    }
    IPAddressVO ip = markIpAsUnavailable(addrId);
    if (ip == null) {
        return true;
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Releasing ip id=" + addrId + "; sourceNat = " + ip.isSourceNat());
    }
    if (ip.getAssociatedWithNetworkId() != null) {
        Network network = _networksDao.findById(ip.getAssociatedWithNetworkId());
        try {
            if (!applyIpAssociations(network, rulesContinueOnErrFlag)) {
                s_logger.warn("Unable to apply ip address associations for " + network);
                success = false;
            }
        } catch (ResourceUnavailableException e) {
            throw new CloudRuntimeException("We should never get to here because we used true when applyIpAssociations", e);
        }
    } else {
        if (ip.getState() == IpAddress.State.Releasing) {
            _ipAddressDao.unassignIpAddress(ip.getId());
        }
    }
    if (success) {
        if (ip.isPortable()) {
            releasePortableIpAddress(addrId);
        }
        s_logger.debug("Released a public ip id=" + addrId);
    }
    return success;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) DB(com.cloud.utils.db.DB)

Example 28 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class IpAddressManagerImpl method disassociatePortableIPToGuestNetwork.

@DB
@Override
public IPAddressVO disassociatePortableIPToGuestNetwork(long ipId, long networkId) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException {
    Account caller = CallContext.current().getCallingAccount();
    Account owner = null;
    Network network = _networksDao.findById(networkId);
    if (network == null) {
        throw new InvalidParameterValueException("Invalid network id is given");
    }
    IPAddressVO ipToAssoc = _ipAddressDao.findById(ipId);
    if (ipToAssoc != null) {
        if (ipToAssoc.getAssociatedWithNetworkId() == null) {
            throw new InvalidParameterValueException("IP " + ipToAssoc + " is not associated with any network");
        }
        if (ipToAssoc.getAssociatedWithNetworkId() != network.getId()) {
            throw new InvalidParameterValueException("IP " + ipToAssoc + " is not associated with network id" + networkId);
        }
        DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
        if (zone.getNetworkType() == NetworkType.Advanced) {
            if (network.getGuestType() == Network.GuestType.Shared) {
                assert (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId()));
                _accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.UseEntry, false, network);
            }
        } else {
            _accountMgr.checkAccess(caller, null, true, ipToAssoc);
        }
        owner = _accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
    } else {
        s_logger.debug("Unable to find ip address by id: " + ipId);
        return null;
    }
    DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    //     - and it belongs to the system
    if (network.getAccountId() != owner.getId()) {
        if (zone.getNetworkType() != NetworkType.Basic && !(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == Network.GuestType.Shared)) {
            throw new InvalidParameterValueException("The owner of the network is not the same as owner of the IP");
        }
    }
    // Check if IP has any services (rules) associated in the network
    List<PublicIpAddress> ipList = new ArrayList<PublicIpAddress>();
    PublicIp publicIp = PublicIp.createFromAddrAndVlan(ipToAssoc, _vlanDao.findById(ipToAssoc.getVlanId()));
    ipList.add(publicIp);
    Map<PublicIpAddress, Set<Service>> ipToServices = _networkModel.getIpToServices(ipList, false, true);
    if (!ipToServices.isEmpty()) {
        Set<Service> services = ipToServices.get(publicIp);
        if (services != null && !services.isEmpty()) {
            throw new InvalidParameterValueException("IP " + ipToAssoc + " has services and rules associated in the network " + networkId);
        }
    }
    IPAddressVO ip = _ipAddressDao.findById(ipId);
    ip.setAssociatedWithNetworkId(null);
    _ipAddressDao.update(ipId, ip);
    try {
        boolean success = applyIpAssociations(network, false);
        if (success) {
            s_logger.debug("Successfully associated ip address " + ip.getAddress().addr() + " to network " + network);
        } else {
            s_logger.warn("Failed to associate ip address " + ip.getAddress().addr() + " to network " + network);
        }
        return ip;
    } finally {
    }
}
Also used : Account(com.cloud.user.Account) Set(java.util.Set) HashSet(java.util.HashSet) PublicIp(com.cloud.network.addr.PublicIp) ArrayList(java.util.ArrayList) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) RemoteAccessVpnService(com.cloud.network.vpn.RemoteAccessVpnService) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) IPAddressVO(com.cloud.network.dao.IPAddressVO) DB(com.cloud.utils.db.DB)

Example 29 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class NetworkModelImpl method checkIpForService.

@Override
public boolean checkIpForService(IpAddress userIp, Service service, Long networkId) {
    if (networkId == null) {
        networkId = userIp.getAssociatedWithNetworkId();
    }
    NetworkVO network = _networksDao.findById(networkId);
    NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
    if (offering.getGuestType() != GuestType.Isolated) {
        return true;
    }
    IPAddressVO ipVO = _ipAddressDao.findById(userIp.getId());
    PublicIp publicIp = PublicIp.createFromAddrAndVlan(ipVO, _vlanDao.findById(userIp.getVlanId()));
    if (!canIpUsedForService(publicIp, service, networkId)) {
        return false;
    }
    if (!offering.isConserveMode()) {
        return canIpUsedForNonConserveService(publicIp, service);
    }
    return true;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) PublicIp(com.cloud.network.addr.PublicIp) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Example 30 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class IpAddressManagerImpl method allocatePortableIp.

@Override
@DB
public IpAddress allocatePortableIp(final Account ipOwner, Account caller, final long dcId, final Long networkId, final Long vpcID) throws ConcurrentOperationException, ResourceAllocationException, InsufficientAddressCapacityException {
    GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange");
    IPAddressVO ipaddr;
    try {
        portableIpLock.lock(5);
        ipaddr = Transaction.execute(new TransactionCallbackWithException<IPAddressVO, InsufficientAddressCapacityException>() {

            @Override
            public IPAddressVO doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException {
                PortableIpVO allocatedPortableIp;
                List<PortableIpVO> portableIpVOs = _portableIpDao.listByRegionIdAndState(1, PortableIp.State.Free);
                if (portableIpVOs == null || portableIpVOs.isEmpty()) {
                    InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available portable IP addresses", Region.class, new Long(1));
                    throw ex;
                }
                // allocate first portable IP to the user
                allocatedPortableIp = portableIpVOs.get(0);
                allocatedPortableIp.setAllocatedTime(new Date());
                allocatedPortableIp.setAllocatedToAccountId(ipOwner.getAccountId());
                allocatedPortableIp.setAllocatedInDomainId(ipOwner.getDomainId());
                allocatedPortableIp.setState(PortableIp.State.Allocated);
                _portableIpDao.update(allocatedPortableIp.getId(), allocatedPortableIp);
                // To make portable IP available as a zone level resource we need to emulate portable IP's (which are
                // provisioned at region level) as public IP provisioned in a zone. user_ip_address and vlan combo give the
                // identity of a public IP in zone. Create entry for portable ip in these tables.
                // provision portable IP range VLAN into the zone
                long physicalNetworkId = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(dcId, TrafficType.Public).getId();
                Network network = _networkModel.getSystemNetworkByZoneAndTrafficType(dcId, TrafficType.Public);
                String range = allocatedPortableIp.getAddress() + "-" + allocatedPortableIp.getAddress();
                VlanVO vlan = new VlanVO(VlanType.VirtualNetwork, allocatedPortableIp.getVlan(), allocatedPortableIp.getGateway(), allocatedPortableIp.getNetmask(), dcId, range, network.getId(), physicalNetworkId, null, null, null);
                vlan = _vlanDao.persist(vlan);
                // provision the portable IP in to user_ip_address table
                IPAddressVO ipaddr = new IPAddressVO(new Ip(allocatedPortableIp.getAddress()), dcId, networkId, vpcID, physicalNetworkId, network.getId(), vlan.getId(), true);
                ipaddr.setState(State.Allocated);
                ipaddr.setAllocatedTime(new Date());
                ipaddr.setAllocatedInDomainId(ipOwner.getDomainId());
                ipaddr.setAllocatedToAccountId(ipOwner.getId());
                ipaddr = _ipAddressDao.persist(ipaddr);
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_PORTABLE_IP_ASSIGN, ipaddr.getId(), ipaddr.getDataCenterId(), ipaddr.getId(), ipaddr.getAddress().toString(), ipaddr.isSourceNat(), null, ipaddr.getSystem(), ipaddr.getClass().getName(), ipaddr.getUuid());
                return ipaddr;
            }
        });
    } finally {
        portableIpLock.unlock();
    }
    return ipaddr;
}
Also used : TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) PortableIp(org.apache.cloudstack.region.PortableIp) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) TransactionStatus(com.cloud.utils.db.TransactionStatus) PortableIpVO(org.apache.cloudstack.region.PortableIpVO) Date(java.util.Date) GlobalLock(com.cloud.utils.db.GlobalLock) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DB(com.cloud.utils.db.DB)

Aggregations

IPAddressVO (com.cloud.network.dao.IPAddressVO)109 ArrayList (java.util.ArrayList)43 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)42 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)39 Account (com.cloud.user.Account)37 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 DB (com.cloud.utils.db.DB)28 TransactionStatus (com.cloud.utils.db.TransactionStatus)26 Network (com.cloud.network.Network)25 PublicIp (com.cloud.network.addr.PublicIp)22 DataCenter (com.cloud.dc.DataCenter)17 VlanVO (com.cloud.dc.VlanVO)16 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)16 List (java.util.List)15 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)14 Ip (com.cloud.utils.net.Ip)14 NetworkOffering (com.cloud.offering.NetworkOffering)13 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)13 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)12 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)11