Search in sources :

Example 1 with PortableIpRangeVO

use of org.apache.cloudstack.region.PortableIpRangeVO 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.isValidIp4(startIP) || !NetUtils.isValidIp4(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 2 with PortableIpRangeVO

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

the class ConfigurationManagerImpl method checkOverlapPortableIpRange.

private boolean checkOverlapPortableIpRange(final int regionId, final String newStartIpStr, final String newEndIpStr) {
    final long newStartIp = NetUtils.ip2Long(newStartIpStr);
    final long newEndIp = NetUtils.ip2Long(newEndIpStr);
    final List<PortableIpRangeVO> existingPortableIPRanges = _portableIpRangeDao.listByRegionId(regionId);
    if (existingPortableIPRanges == null || existingPortableIPRanges.isEmpty()) {
        return false;
    }
    for (final PortableIpRangeVO portableIpRange : existingPortableIPRanges) {
        final String ipRangeStr = portableIpRange.getIpRange();
        final String[] range = ipRangeStr.split("-");
        final long startip = NetUtils.ip2Long(range[0]);
        final long endIp = NetUtils.ip2Long(range[1]);
        if (newStartIp >= startip && newStartIp <= endIp || newEndIp >= startip && newEndIp <= endIp) {
            return true;
        }
        if (startip >= newStartIp && startip <= newEndIp || endIp >= newStartIp && endIp <= newEndIp) {
            return true;
        }
    }
    return false;
}
Also used : PortableIpRangeVO(org.apache.cloudstack.region.PortableIpRangeVO)

Example 3 with PortableIpRangeVO

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

Example 4 with PortableIpRangeVO

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

the class ConfigurationManagerImpl method listPortableIpRanges.

@Override
public List<? extends PortableIpRange> listPortableIpRanges(final ListPortableIpRangesCmd cmd) {
    final Integer regionId = cmd.getRegionIdId();
    final Long rangeId = cmd.getPortableIpRangeId();
    final List<PortableIpRangeVO> ranges = new ArrayList<PortableIpRangeVO>();
    if (regionId != null) {
        final Region region = _regionDao.findById(regionId);
        if (region == null) {
            throw new InvalidParameterValueException("Invalid region ID: " + regionId);
        }
        return _portableIpRangeDao.listByRegionId(regionId);
    }
    if (rangeId != null) {
        final PortableIpRangeVO range = _portableIpRangeDao.findById(rangeId);
        if (range == null) {
            throw new InvalidParameterValueException("Invalid portable IP range ID: " + regionId);
        }
        ranges.add(range);
        return ranges;
    }
    return _portableIpRangeDao.listAll();
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) Region(org.apache.cloudstack.region.Region) PortableIpRangeVO(org.apache.cloudstack.region.PortableIpRangeVO)

Aggregations

PortableIpRangeVO (org.apache.cloudstack.region.PortableIpRangeVO)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 ActionEvent (com.cloud.event.ActionEvent)2 DB (com.cloud.utils.db.DB)2 PortableIpVO (org.apache.cloudstack.region.PortableIpVO)2 RegionVO (org.apache.cloudstack.region.RegionVO)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 GlobalLock (com.cloud.utils.db.GlobalLock)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 ArrayList (java.util.ArrayList)1 Region (org.apache.cloudstack.region.Region)1