@@ -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