kioie commented on a change in pull request #3997: [WIP] New API endpoint:
UpdateVlanIpRange
URL: https://github.com/apache/cloudstack/pull/3997#discussion_r399774124
##########
File path:
server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java
##########
@@ -4075,6 +4080,149 @@ public VlanVO doInTransaction(final TransactionStatus
status) {
}
+ @Override
+ public Vlan updateVlanAndPublicIpRange(UpdateVlanIpRangeCmd cmd) throws
ConcurrentOperationException,
+ ResourceUnavailableException,ResourceAllocationException{
+
+ return updateVlanAndPublicIpRange(cmd.getId(),
cmd.getStartIp(),cmd.getEndIp(),
+ cmd.getGateway(),cmd.getNetmask());
+ }
+
+ @DB
+ @ActionEvent(eventType = EventTypes.EVENT_VLAN_IP_RANGE_UPDATE,
eventDescription = "update vlan ip Range", async
+ = false)
+ public Vlan updateVlanAndPublicIpRange(final long id, String startIp,
+ String endIp,
+ String gateway,
+ String netmask) throws
ConcurrentOperationException{
+
+ VlanVO vlanRange = _vlanDao.findById(id);
+ if (vlanRange == null) {
+ throw new InvalidParameterValueException("Please specify a valid
IP range id.");
+ }
+
+ if (gateway == null) {
+ gateway = vlanRange.getVlanGateway();
+ }
+
+ if (netmask == null) {
+ netmask = vlanRange.getVlanNetmask();
+ }
+
+ final String[] existingVlanIPRangeArray =
vlanRange.getIpRange().split("-");
+ final String currentStartIP= existingVlanIPRangeArray[0];
+ final String currentEndIP = existingVlanIPRangeArray [1];
+
+ if(startIp == null){
+ startIp= currentStartIP;
+ }
+
+ if(endIp == null){
+ endIp= currentEndIP;
+ }
+
+ final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(id);
+ checkAllocatedIpsAreWithinVlanRange(ips,startIp,endIp);
+
+ final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
+ final String cidrAddress = getCidrAddress(cidr);
+ final long cidrSize = getCidrSize(cidr);
+
+ checkIpRange(currentStartIP,currentEndIP,cidrAddress,cidrSize);
+
+ checkIpRange(startIp, endIp, cidrAddress, cidrSize);
+
+ checkGatewayOverlap(startIp,endIp,gateway);
+
+ VlanVO range;
+ try {
+ final String newStartIP= startIp;
+ final String newEndIP= endIp;
+
+ range = _vlanDao.acquireInLockTable(id, 30);
+ if (range == null) {
+ throw new CloudRuntimeException("Unable to acquire vlan
configuration: " + id);
+ }
+
+ if (s_logger.isDebugEnabled()) {
+ s_logger.debug("lock vlan " + id + " is acquired");
+ }
+
+ commitUpdateVlanAndIpRange(id, newStartIP, newEndIP,
currentStartIP, currentEndIP,true);
+
+ } catch (final Exception e) {
+ s_logger.error("Unable to edit VlanRange due to " +
e.getMessage(), e);
+ throw new CloudRuntimeException("Failed to edit VlanRange. Please
contact Cloud Support.");
+ }finally {
+ _vlanDao.releaseFromLockTable(id);
+ }
+
+ return vlanRange;
+ }
+ private VlanVO commitUpdateVlanAndIpRange(final Long id, final String
newStartIP, final String newEndIP,final String currentStartIP, final String
currentEndIP, final boolean ipv4) {
+ return Transaction.execute(new TransactionCallback<VlanVO>() {
+ @Override
+ public VlanVO doInTransaction(final TransactionStatus status) {
+ VlanVO vlanRange = _vlanDao.findById(id);
+ s_logger.debug("Updating vlan range " + vlanRange.getId());
+
vlanRange.setIpRange(vlanRange.getIpRange().replace(currentStartIP+"-",
newStartIP+"-").replace(currentEndIP,
+ newEndIP));
+ _vlanDao.update(vlanRange.getId(), vlanRange);
+
+ final boolean isRangeForSystemVM =
checkIfVlanRangeIsForSystemVM(id);
+ if (ipv4) {
+ if
(!updatePublicIPRange(newStartIP,currentStartIP,newEndIP,currentEndIP,vlanRange.getDataCenterId(),vlanRange.getId(),
vlanRange.getNetworkId(), vlanRange.getPhysicalNetworkId(),
isRangeForSystemVM)) {
+ throw new CloudRuntimeException("Failed to update IPv4
range. Please contact Cloud Support.");
+ }
+ }
+ return vlanRange;
+ }
+ });
+
+ }
+ private boolean checkIfVlanRangeIsForSystemVM(final long vlanId) {
+ List<IPAddressVO> existingPublicIPs =
_publicIpAddressDao.listByVlanId(vlanId);
+ boolean initialIsSystemVmValue =
existingPublicIPs.get(0).isForSystemVms();
+ for(IPAddressVO existingIPs : existingPublicIPs){
+ if(initialIsSystemVmValue != existingIPs.isForSystemVms()){
+ throw new CloudRuntimeException("Your \"For System VM\" value
seems to be inconsistent with the rest of the recods. Please contact Cloud
Support");
+ }
+ }
+ return initialIsSystemVmValue;
+ }
+
+ private void checkAllocatedIpsAreWithinVlanRange(List<IPAddressVO> ips,
String startIp, String endIp){
+ // Check if the VLAN has any allocated public IPs
+ List<IPAddressVO> listAllocatedIPs = new ArrayList<>();
+ for (final IPAddressVO ip : ips) {
+ if (ip.getState()== IpAddress.State.Allocated){
+ listAllocatedIPs.add(ip);
+ }
+ }
+ Collections.sort(listAllocatedIPs,
Comparator.comparing(IPAddressVO::getAddress));
+ for (IPAddressVO allocatedIP : listAllocatedIPs){
+ if (!Strings.isNullOrEmpty(startIp) && NetUtils.ip2Long(startIp) >
NetUtils.ip2Long(allocatedIP.getAddress().addr())) {
+ throw new InvalidParameterValueException("The start IP address
must have a lower IP address value " +
+ "than "+ allocatedIP.getAddress() + " which is already
in use. The end IP must have a " +
+ "higher IP address than "+
listAllocatedIPs.get(listAllocatedIPs.size() - 1).getAddress()+" .IPs " +
+ "already allocated in this range:
"+listAllocatedIPs.size());
+ }
+ if (!Strings.isNullOrEmpty(endIp) && NetUtils.ip2Long(endIp) <
NetUtils.ip2Long(allocatedIP.getAddress().addr())) {
+ throw new InvalidParameterValueException("The start IP address
must have a lower IP address value " +
+ "than "+ listAllocatedIPs.get(0).getAddress() + "
which is already in use. The end IP must have a " +
+ "higher IP address than "+ allocatedIP.getAddress()+"
.IPs " +
+ "already allocated in this range:
"+listAllocatedIPs.size());
+ }
+ }
+ }
+
+ private void checkGatewayOverlap(String startIp, String endIp, String
gateway){
Review comment:
Method to check overlap
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services