Skip to content

Commit 8e55dbf

Browse files
kioiekioieddy
authored andcommitted
Added additional checks to confirm if IP Range contains taken addresses. Added logic to update op_dc_ip_address_alloc table. Also bried cleanup
1 parent 791923c commit 8e55dbf

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

engine/schema/src/main/java/com/cloud/dc/dao/DataCenterIpAddressDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public interface DataCenterIpAddressDao extends GenericDao<DataCenterIpAddressVO
4141

4242
List<DataCenterIpAddressVO> listByPodIdDcId(long podId, long dcId);
4343

44+
List<DataCenterIpAddressVO> listIpAddressUsage(final long podId, final long dcId, final boolean onlyListAllocated);
45+
4446
int countIPs(long podId, long dcId, boolean onlyCountAllocated);
4547

4648
int countIPs(long dcId, boolean onlyCountAllocated);

engine/schema/src/main/java/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ public int countIpAddressUsage(final String ipAddress, final long podId, final l
290290
return result.size();
291291
}
292292

293+
@Override
294+
public List<DataCenterIpAddressVO> listIpAddressUsage(final long podId, final long dcId, final boolean onlyListAllocated) {
295+
SearchCriteria<DataCenterIpAddressVO> sc = createSearchCriteria();
296+
297+
if(onlyListAllocated) {
298+
sc.addAnd("takenAt", SearchCriteria.Op.NNULL);
299+
}
300+
301+
sc.addAnd("podId", SearchCriteria.Op.EQ, podId);
302+
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, dcId);
303+
304+
return listBy(sc);
305+
}
306+
293307
public DataCenterIpAddressDaoImpl() {
294308
super();
295309

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
14041404

14051405
@Override
14061406
public Pod editPod(final UpdatePodCmd cmd) {
1407-
return editPod(cmd.getId(), cmd.getPodName(), null, null, cmd.getGateway(), cmd.getNetmask(), cmd.getAllocationState());
1407+
return editPod(cmd.getId(), cmd.getPodName(), cmd.getStartIp(), cmd.getEndIp(), cmd.getGateway(), cmd.getNetmask(), cmd.getAllocationState());
14081408
}
14091409

14101410
@Override
@@ -1502,10 +1502,33 @@ public Pod editPod(final long id, String name, String startIp, String endIp, Str
15021502
// Check if the IP range overlaps with the public ip.
15031503
checkOverlapPublicIpRange(zoneId, startIp, endIp);
15041504

1505+
// Check if the gateway is in the CIDR subnet
1506+
if (!NetUtils.getCidrSubNet(gateway, cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) {
1507+
throw new InvalidParameterValueException("The gateway is not in the CIDR subnet.");
1508+
}
1509+
15051510
if (NetUtils.ipRangesOverlap(startIp, endIp, gateway, gateway)) {
15061511
throw new InvalidParameterValueException("The gateway shouldn't overlap start/end ip addresses");
15071512
}
15081513

1514+
checkIpRangeContainsTakenAddresses(pod,startIp,endIp);
1515+
1516+
long newStartIPLong = NetUtils.ip2Long(startIp);
1517+
long newEndIPLong = NetUtils.ip2Long(endIp);
1518+
long currentStartIPLong = NetUtils.ip2Long(currentStartIP);
1519+
long currentEndIPLong = NetUtils.ip2Long(currentEndIP);
1520+
1521+
List<Long> currentIPRange = new ArrayList<>();
1522+
List<Long> newIPRange = new ArrayList<>();
1523+
while (newStartIPLong<=newEndIPLong){
1524+
newIPRange.add(newStartIPLong);
1525+
newStartIPLong++;
1526+
}
1527+
while (currentStartIPLong<=currentEndIPLong){
1528+
currentIPRange.add(currentStartIPLong);
1529+
currentStartIPLong++;
1530+
}
1531+
15091532
try {
15101533
final String allocationStateStrFinal = allocationStateStr;
15111534
final String nameFinal = name;
@@ -1522,15 +1545,28 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
15221545
pod.setGateway(gatewayFinal);
15231546
pod.setCidrAddress(getCidrAddress(cidr));
15241547
pod.setCidrSize(getCidrSize(cidr));
1525-
pod.setDescription(pod.getDescription().replaceFirst(currentEndIP, newEndIP).replaceFirst(currentStartIP,
1526-
newStartIP));
1548+
pod.setDescription(pod.getDescription().replace(currentStartIP+"-", newStartIP+"-").replace(currentEndIP,
1549+
newEndIP));
15271550

15281551
Grouping.AllocationState allocationState = null;
15291552
if (allocationStateStrFinal != null && !allocationStateStrFinal.isEmpty()) {
15301553
allocationState = Grouping.AllocationState.valueOf(allocationStateStrFinal);
15311554
pod.setAllocationState(allocationState);
15321555
}
1533-
1556+
List<Long> iPAddressesToAdd = new ArrayList(newIPRange);
1557+
iPAddressesToAdd.removeAll(currentIPRange);
1558+
if (iPAddressesToAdd.size()>0){
1559+
for(Long startIP : iPAddressesToAdd){
1560+
_zoneDao.addPrivateIpAddress(zoneId, pod.getId(), NetUtils.long2Ip(startIP), NetUtils.long2Ip(startIP), false, null);
1561+
}
1562+
}else {
1563+
currentIPRange.removeAll(newIPRange);
1564+
if(currentIPRange.size()>0){
1565+
for (Long startIP: currentIPRange){
1566+
_privateIpAddressDao.deleteIpAddressByPodDc(NetUtils.long2Ip(startIP),pod.getId(),zoneId);
1567+
}
1568+
}
1569+
}
15341570
_podDao.update(id, pod);
15351571
}
15361572
});
@@ -1542,6 +1578,28 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
15421578
return pod;
15431579
}
15441580

1581+
private void checkIpRangeContainsTakenAddresses(final HostPodVO pod,final String startIp, final String endIp ){
1582+
final List<DataCenterIpAddressVO> takenIps = _privateIpAddressDao.listIpAddressUsage(pod.getId(),pod.getDataCenterId(),true);
1583+
List<Long> newIPRange = new ArrayList<>();
1584+
List<Long> takenIpsList = new ArrayList<>();
1585+
1586+
long newStartIPLong = NetUtils.ip2Long(startIp);
1587+
long newEndIPLong = NetUtils.ip2Long(endIp);
1588+
1589+
while (newStartIPLong<=newEndIPLong){
1590+
newIPRange.add(newStartIPLong);
1591+
newStartIPLong++;
1592+
}
1593+
for (int i=0;i<takenIps.size();i++){
1594+
takenIpsList.add(NetUtils.ip2Long(takenIps.get(i).getIpAddress()));
1595+
}
1596+
1597+
if(!newIPRange.containsAll(takenIpsList)){
1598+
throw new InvalidParameterValueException("The IP range does not contain some IP addresses that have "
1599+
+ "already been taken. Please adjust your IP range to include all IP addresses already taken.");
1600+
}
1601+
}
1602+
15451603
@Override
15461604
public Pod createPod(final long zoneId, final String name, final String startIp, final String endIp, final String gateway, final String netmask, String allocationState) {
15471605
// Check if the gateway is a valid IP address

0 commit comments

Comments
 (0)