Search in sources :

Example 1 with PortableIpVO

use of org.apache.cloudstack.region.PortableIpVO 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)

Example 2 with PortableIpVO

use of org.apache.cloudstack.region.PortableIpVO in project cloudstack by apache.

the class ConfigurationManagerImpl method createPortableIpRange.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_PORTABLE_IP_RANGE_CREATE, eventDescription = "creating portable ip range", async = false)
public PortableIpRange createPortableIpRange(final CreatePortableIpRangeCmd cmd) throws ConcurrentOperationException {
    final Integer regionId = cmd.getRegionId();
    final String startIP = cmd.getStartIp();
    final String endIP = cmd.getEndIp();
    final String gateway = cmd.getGateway();
    final String netmask = cmd.getNetmask();
    String vlanId = cmd.getVlan();
    final RegionVO region = _regionDao.findById(regionId);
    if (region == null) {
        throw new InvalidParameterValueException("Invalid region ID: " + regionId);
    }
    if (!NetUtils.isValidIp(startIP) || !NetUtils.isValidIp(endIP) || !NetUtils.validIpRange(startIP, endIP)) {
        throw new InvalidParameterValueException("Invalid portable ip  range: " + startIP + "-" + endIP);
    }
    if (!NetUtils.sameSubnet(startIP, gateway, netmask)) {
        throw new InvalidParameterValueException("Please ensure that your start IP is in the same subnet as " + "your portable IP range's gateway and as per the IP range's netmask.");
    }
    if (!NetUtils.sameSubnet(endIP, gateway, netmask)) {
        throw new InvalidParameterValueException("Please ensure that your end IP is in the same subnet as " + "your portable IP range's gateway and as per the IP range's netmask.");
    }
    if (checkOverlapPortableIpRange(regionId, startIP, endIP)) {
        throw new InvalidParameterValueException("Ip  range: " + startIP + "-" + endIP + " overlaps with a portable" + " IP range already configured in the region " + regionId);
    }
    if (vlanId == null) {
        vlanId = Vlan.UNTAGGED;
    } else {
        if (!NetUtils.isValidVlan(vlanId)) {
            throw new InvalidParameterValueException("Invalid vlan id " + vlanId);
        }
        final List<DataCenterVO> zones = _zoneDao.listAllZones();
        if (zones != null && !zones.isEmpty()) {
            for (final DataCenterVO zone : zones) {
                // check if there is zone vlan with same id
                if (_vlanDao.findByZoneAndVlanId(zone.getId(), vlanId) != null) {
                    throw new InvalidParameterValueException("Found a VLAN id " + vlanId + " already existing in" + " zone " + zone.getUuid() + " that conflicts with VLAN id of the portable ip range being configured");
                }
                //check if there is a public ip range that overlaps with portable ip range being created
                checkOverlapPublicIpRange(zone.getId(), startIP, endIP);
            }
        }
    }
    final GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange");
    portableIpLock.lock(5);
    try {
        final String vlanIdFinal = vlanId;
        return Transaction.execute(new TransactionCallback<PortableIpRangeVO>() {

            @Override
            public PortableIpRangeVO doInTransaction(final TransactionStatus status) {
                PortableIpRangeVO portableIpRange = new PortableIpRangeVO(regionId, vlanIdFinal, gateway, netmask, startIP, endIP);
                portableIpRange = _portableIpRangeDao.persist(portableIpRange);
                long startIpLong = NetUtils.ip2Long(startIP);
                final long endIpLong = NetUtils.ip2Long(endIP);
                while (startIpLong <= endIpLong) {
                    final PortableIpVO portableIP = new PortableIpVO(regionId, portableIpRange.getId(), vlanIdFinal, gateway, netmask, NetUtils.long2Ip(startIpLong));
                    _portableIpDao.persist(portableIP);
                    startIpLong++;
                }
                // implicitly enable portable IP service for the region
                region.setPortableipEnabled(true);
                _regionDao.update(region.getId(), region);
                return portableIpRange;
            }
        });
    } finally {
        portableIpLock.unlock();
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) GlobalLock(com.cloud.utils.db.GlobalLock) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) RegionVO(org.apache.cloudstack.region.RegionVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) PortableIpRangeVO(org.apache.cloudstack.region.PortableIpRangeVO) PortableIpVO(org.apache.cloudstack.region.PortableIpVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 3 with PortableIpVO

use of org.apache.cloudstack.region.PortableIpVO in project cloudstack by apache.

the class ConfigurationManagerImpl method deletePortableIpRange.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_PORTABLE_IP_RANGE_DELETE, eventDescription = "deleting portable ip range", async = false)
public boolean deletePortableIpRange(final DeletePortableIpRangeCmd cmd) {
    final long rangeId = cmd.getId();
    final PortableIpRangeVO portableIpRange = _portableIpRangeDao.findById(rangeId);
    if (portableIpRange == null) {
        throw new InvalidParameterValueException("Please specify a valid portable IP range id.");
    }
    final List<PortableIpVO> fullIpRange = _portableIpDao.listByRangeId(portableIpRange.getId());
    final List<PortableIpVO> freeIpRange = _portableIpDao.listByRangeIdAndState(portableIpRange.getId(), PortableIp.State.Free);
    if (fullIpRange != null && freeIpRange != null) {
        if (fullIpRange.size() == freeIpRange.size()) {
            _portableIpRangeDao.expunge(portableIpRange.getId());
            final List<PortableIpRangeVO> pipranges = _portableIpRangeDao.listAll();
            if (pipranges == null || pipranges.isEmpty()) {
                final RegionVO region = _regionDao.findById(portableIpRange.getRegionId());
                region.setPortableipEnabled(false);
                _regionDao.update(region.getId(), region);
            }
            return true;
        } else {
            throw new InvalidParameterValueException("Can't delete portable IP range as there are IP's assigned.");
        }
    }
    return false;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) RegionVO(org.apache.cloudstack.region.RegionVO) PortableIpRangeVO(org.apache.cloudstack.region.PortableIpRangeVO) PortableIpVO(org.apache.cloudstack.region.PortableIpVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

DB (com.cloud.utils.db.DB)3 PortableIpVO (org.apache.cloudstack.region.PortableIpVO)3 ActionEvent (com.cloud.event.ActionEvent)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 GlobalLock (com.cloud.utils.db.GlobalLock)2 TransactionStatus (com.cloud.utils.db.TransactionStatus)2 PortableIpRangeVO (org.apache.cloudstack.region.PortableIpRangeVO)2 RegionVO (org.apache.cloudstack.region.RegionVO)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 VlanVO (com.cloud.dc.VlanVO)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 PublicIp (com.cloud.network.addr.PublicIp)1 IPAddressVO (com.cloud.network.dao.IPAddressVO)1 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)1 Ip (com.cloud.utils.net.Ip)1 Date (java.util.Date)1 PortableIp (org.apache.cloudstack.region.PortableIp)1