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);
}
List<DataCenterVO> zones= _zoneDao.listAllZones();
if (zones != null && !zones.isEmpty()) {
for (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);
}
}
}
GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange");
portableIpLock.lock(5);
try {
final String vlanIdFinal = vlanId;
return Transaction.execute(new TransactionCallback<PortableIpRangeVO>() {
@Override
public PortableIpRangeVO doInTransaction(TransactionStatus status) {
PortableIpRangeVO portableIpRange = new PortableIpRangeVO(regionId, vlanIdFinal, gateway, netmask, startIP, endIP);
portableIpRange = _portableIpRangeDao.persist(portableIpRange);
long startIpLong = NetUtils.ip2Long(startIP);
long endIpLong = NetUtils.ip2Long(endIP);
while (startIpLong <= endIpLong) {
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 {