From 7f318032328cd827f287586d35fdc9cd93836d60 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Wed, 5 Feb 2025 10:37:46 -0500 Subject: [PATCH] Validate if given CIDR belongs to a bigger allocation in Netris before creating the zone-level allocation (#48) * Validate if given CIDR belongs to a bigger allocation in Netris before creating * rename method --- .../cloudstack/service/NetrisApiClientImpl.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java index 7f80947974d..e61482a3564 100644 --- a/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java +++ b/plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java @@ -18,6 +18,8 @@ package org.apache.cloudstack.service; import com.cloud.utils.Pair; import com.cloud.utils.exception.CloudRuntimeException; +import inet.ipaddr.IPAddress; +import inet.ipaddr.IPAddressString; import io.netris.ApiClient; import io.netris.ApiException; import io.netris.ApiResponse; @@ -815,11 +817,19 @@ public class NetrisApiClientImpl implements NetrisApiClient { filterByVpc.add(vpc.getId()); IpTree ipamTree = ipamApi.apiV2IpamGet(filterBySites, filterByVpc); List superCidrList = ipamTree.getData().stream() - .filter(x -> x.getPrefix().equals(superCidrPrefix)) + .filter(x -> x.getPrefix().equals(superCidrPrefix) || isAllocationPartOfBiggerAllocation(x.getPrefix(), superCidrPrefix)) .collect(Collectors.toList()); return CollectionUtils.isEmpty(superCidrList) ? null : superCidrList.get(0).getId(); } + private boolean isAllocationPartOfBiggerAllocation(String netrisAllocation, String providedAllocation) { + IPAddress biggerAllocation = new IPAddressString(netrisAllocation).getAddress(); + IPAddress smallerAllocation = new IPAddressString(providedAllocation).getAddress(); + + return biggerAllocation.contains(smallerAllocation); + + } + private IpTreeSubnet getIpamSubnetByAllocationAndPrefixAndPurposeAndVpc(BigDecimal ipamAllocationId, String exactCidr, IpTreeSubnet.PurposeEnum purpose, VPCListing vpc) throws ApiException { IpamApi ipamApi = apiClient.getApiStubForMethod(IpamApi.class); FilterByVpc filterByVpc = new FilterByVpc();