3333import io .netris .model .FilterByVpc ;
3434import io .netris .model .GetSiteBody ;
3535import io .netris .model .InlineResponse2004 ;
36+ import io .netris .model .InlineResponse2004Data ;
37+ import io .netris .model .IpTree ;
38+ import io .netris .model .IpTreeAllocation ;
3639import io .netris .model .IpTreeAllocationTenant ;
3740import io .netris .model .IpTreeSubnet ;
3841import io .netris .model .IpTreeSubnetSites ;
6366import org .apache .cloudstack .agent .api .CreateNetrisVpcCommand ;
6467import org .apache .cloudstack .agent .api .DeleteNetrisVnetCommand ;
6568import org .apache .cloudstack .agent .api .DeleteNetrisVpcCommand ;
69+ import org .apache .cloudstack .agent .api .SetupNetrisPublicRangeCommand ;
6670import org .apache .cloudstack .resource .NetrisResourceObjectUtils ;
6771import org .apache .commons .collections .CollectionUtils ;
6872import org .apache .logging .log4j .LogManager ;
@@ -196,25 +200,31 @@ private VPCResponseObjectOK createVpcInternal(String vpcName, int adminTenantId,
196200 return response ;
197201 }
198202
199- private InlineResponse2004 createVpcAllocationInternal (VPCResponseObjectOK createdVpc , String cidr , int adminTenantId ,
200- String adminTenantName , String netrisIpamAllocationName ) {
201- logger .debug (String .format ("Creating Netris VPC Allocation %s for VPC %s" , cidr , createdVpc .getData ().getName ()));
203+ private InlineResponse2004Data createIpamAllocationInternal (String ipamName , String ipamPrefix , VPCListing vpc ) {
204+ logger .debug (String .format ("Creating Netris IPAM Allocation %s for VPC %s" , ipamPrefix , vpc .getName ()));
202205 try {
203206 IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
204207 AllocationBody body = new AllocationBody ();
205208 AllocationBodyVpc allocationBodyVpc = new AllocationBodyVpc ();
206- allocationBodyVpc .setId (createdVpc . getData () .getId ());
207- allocationBodyVpc .setName (createdVpc . getData () .getName ());
209+ allocationBodyVpc .setId (vpc .getId ());
210+ allocationBodyVpc .setName (vpc .getName ());
208211 body .setVpc (allocationBodyVpc );
209- body .setName (netrisIpamAllocationName );
210- body .setPrefix (cidr );
212+ body .setName (ipamName );
213+ body .setPrefix (ipamPrefix );
211214 IpTreeAllocationTenant allocationTenant = new IpTreeAllocationTenant ();
212- allocationTenant .setId (new BigDecimal (adminTenantId ));
213- allocationTenant .setName (adminTenantName );
215+ allocationTenant .setId (new BigDecimal (tenantId ));
216+ allocationTenant .setName (tenantName );
214217 body .setTenant (allocationTenant );
215- return ipamApi .apiV2IpamAllocationPost (body );
218+ InlineResponse2004 ipamResponse = ipamApi .apiV2IpamAllocationPost (body );
219+ if (ipamResponse == null || !ipamResponse .isIsSuccess ()) {
220+ String reason = ipamResponse == null ? "Empty response" : "Operation failed on Netris" ;
221+ logger .debug ("The Netris Allocation {} for VPC {} creation failed: {}" , ipamPrefix , vpc .getName (), reason );
222+ return null ;
223+ }
224+ logger .debug (String .format ("Successfully created VPC %s and its IPAM Allocation %s on Netris" , vpc .getName (), ipamPrefix ));
225+ return ipamResponse .getData ();
216226 } catch (ApiException e ) {
217- logAndThrowException (String .format ("Error creating Netris Allocation %s for VPC %s" , cidr , createdVpc . getData () .getName ()), e );
227+ logAndThrowException (String .format ("Error creating Netris IPAM Allocation %s for VPC %s" , ipamPrefix , vpc .getName ()), e );
218228 return null ;
219229 }
220230 }
@@ -231,14 +241,8 @@ public boolean createVpc(CreateNetrisVpcCommand cmd) {
231241
232242 String netrisIpamAllocationName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .IPAM_ALLOCATION , cmd .getCidr ());
233243 String vpcCidr = cmd .getCidr ();
234- InlineResponse2004 ipamResponse = createVpcAllocationInternal (createdVpc , vpcCidr , tenantId , tenantName , netrisIpamAllocationName );
235- if (ipamResponse == null || !ipamResponse .isIsSuccess ()) {
236- String reason = ipamResponse == null ? "Empty response" : "Operation failed on Netris" ;
237- logger .debug ("The Netris Allocation {} for VPC {} creation failed: {}" , vpcCidr , cmd .getName (), reason );
238- return false ;
239- }
240- logger .debug (String .format ("Successfully created VPC %s and its IPAM Allocation %s on Netris" , cmd .getName (), vpcCidr ));
241- return true ;
244+ InlineResponse2004Data createdIpamAllocation = createIpamAllocationInternal (netrisIpamAllocationName , vpcCidr , createdVpc .getData ());
245+ return createdIpamAllocation != null ;
242246 }
243247
244248 private void deleteVpcIpamAllocationInternal (VPCListing vpcResource , String vpcCidr ) {
@@ -334,12 +338,7 @@ public boolean createVnet(CreateNetrisVnetCommand cmd) {
334338 String netrisVnetName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .VNET , vNetName ) ;
335339 String netrisSubnetName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .IPAM_SUBNET , vnetCidr ) ;
336340
337- InlineResponse2004 subnetResponse = createVpcSubnetInternal (associatedVpc , vNetName , vnetCidr , netrisSubnetName );
338- if (subnetResponse == null || !subnetResponse .isIsSuccess ()) {
339- String reason = subnetResponse == null ? "Empty response" : "Operation failed on Netris" ;
340- logger .debug ("The Netris Subnet {} for network {} creation failed: {}" , vnetCidr , networkName , reason );
341- return false ;
342- }
341+ createIpamSubnetInternal (netrisSubnetName , vnetCidr , SubnetBody .PurposeEnum .COMMON , associatedVpc );
343342 logger .debug ("Successfully created IPAM Subnet {} for network {} on Netris" , netrisSubnetName , networkName );
344343
345344 VnetResAddBody vnetResponse = createVnetInternal (associatedVpc , netrisVnetName , vnetCidr );
@@ -392,6 +391,71 @@ public boolean deleteVnet(DeleteNetrisVnetCommand cmd) {
392391 return true ;
393392 }
394393
394+ protected VPCListing getSystemVpc () throws ApiException {
395+ List <VPCListing > systemVpcList = listVPCs ().stream ().filter (VPCListing ::isIsSystem ).collect (Collectors .toList ());
396+ if (CollectionUtils .isEmpty (systemVpcList )) {
397+ String msg = "Cannot find any system VPC" ;
398+ logger .error (msg );
399+ throw new CloudRuntimeException (msg );
400+ }
401+ return systemVpcList .get (0 );
402+ }
403+
404+ private BigDecimal getIpamAllocationIdByPrefixAndVpc (String superCidrPrefix , VPCListing vpc ) throws ApiException {
405+ IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
406+ FilterBySites filterBySites = new FilterBySites ();
407+ filterBySites .add (siteId );
408+ FilterByVpc filterByVpc = new FilterByVpc ();
409+ filterByVpc .add (vpc .getId ());
410+ IpTree ipamTree = ipamApi .apiV2IpamGet (filterBySites , filterByVpc );
411+ List <IpTreeAllocation > superCidrList = ipamTree .getData ().stream ()
412+ .filter (x -> x .getPrefix ().equals (superCidrPrefix ))
413+ .collect (Collectors .toList ());
414+ return CollectionUtils .isEmpty (superCidrList ) ? null : superCidrList .get (0 ).getId ();
415+ }
416+
417+ private IpTreeSubnet getIpamSubnetByAllocationAndPrefixAndPurposeAndVpc (BigDecimal ipamAllocationId , String exactCidr , IpTreeSubnet .PurposeEnum purpose , VPCListing vpc ) throws ApiException {
418+ IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
419+ FilterByVpc filterByVpc = new FilterByVpc ();
420+ filterByVpc .add (vpc .getId ());
421+ SubnetResBody subnetResBody = ipamApi .apiV2IpamSubnetsGet (filterByVpc );
422+ List <IpTreeSubnet > exactSubnetList = subnetResBody .getData ().stream ()
423+ .filter (x -> x .getAllocationID ().equals (ipamAllocationId ) && x .getPrefix ().equals (exactCidr ) && x .getPurpose () == purpose )
424+ .collect (Collectors .toList ());
425+ return CollectionUtils .isEmpty (exactSubnetList ) ? null : exactSubnetList .get (0 );
426+ }
427+
428+ @ Override
429+ public boolean setupZoneLevelPublicRange (SetupNetrisPublicRangeCommand cmd ) {
430+ String superCidr = cmd .getSuperCidr ();
431+ String exactCidr = cmd .getExactCidr ();
432+ try {
433+ VPCListing systemVpc = getSystemVpc ();
434+ logger .debug ("Checking if the Netris Public Super CIDR {} exists" , superCidr );
435+ BigDecimal ipamAllocationId = getIpamAllocationIdByPrefixAndVpc (superCidr , systemVpc );
436+ if (ipamAllocationId == null ) {
437+ String ipamName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .IPAM_ALLOCATION , superCidr );
438+ InlineResponse2004Data ipamAllocation = createIpamAllocationInternal (ipamName , superCidr , systemVpc );
439+ if (ipamAllocation == null ) {
440+ String msg = String .format ("Could not create the zone level super CIDR %s for the system VPC" , superCidr );
441+ logger .error (msg );
442+ throw new CloudRuntimeException (msg );
443+ }
444+ ipamAllocationId = new BigDecimal (ipamAllocation .getId ());
445+ }
446+ IpTreeSubnet exactSubnet = getIpamSubnetByAllocationAndPrefixAndPurposeAndVpc (ipamAllocationId , exactCidr , IpTreeSubnet .PurposeEnum .NAT , systemVpc );
447+ if (exactSubnet == null ) {
448+ String ipamSubnetName = NetrisResourceObjectUtils .retrieveNetrisResourceObjectName (cmd , NetrisResourceObjectUtils .NetrisObjectType .IPAM_SUBNET , exactCidr );
449+ createIpamSubnetInternal (ipamSubnetName , exactCidr , SubnetBody .PurposeEnum .NAT , systemVpc );
450+ }
451+ } catch (ApiException e ) {
452+ String msg = String .format ("Error setting up the Netris Public Range %s on super CIDR %s" , exactCidr , superCidr );
453+ logAndThrowException (msg , e );
454+ return false ;
455+ }
456+ return true ;
457+ }
458+
395459 private void deleteVnetInternal (VPCListing associatedVpc , FilterBySites siteFilter , FilterByVpc vpcFilter , String netrisVnetName , String vNetName ) {
396460 try {
397461 VNetApi vNetApi = apiClient .getApiStubForMethod (VNetApi .class );
@@ -433,16 +497,16 @@ private void deleteSubnetInternal(FilterByVpc vpcFilter, String netrisVnetName,
433497 }
434498 }
435499
436- private InlineResponse2004 createVpcSubnetInternal ( VPCListing associatedVpc , String vNetName , String vNetCidr , String netrisSubnetName ) {
437- logger .debug ("Creating Netris VPC Subnet {} for VPC {} for vNet {} " , vNetCidr , associatedVpc .getName (), vNetName );
500+ private InlineResponse2004Data createIpamSubnetInternal ( String subnetName , String subnetPrefix , SubnetBody . PurposeEnum purpose , VPCListing vpc ) {
501+ logger .debug ("Creating Netris IPAM Subnet {} for VPC {}" , subnetPrefix , vpc .getName ());
438502 try {
439503
440504 SubnetBody subnetBody = new SubnetBody ();
441- subnetBody .setName (netrisSubnetName );
505+ subnetBody .setName (subnetName );
442506
443507 AllocationBodyVpc vpcAllocationBody = new AllocationBodyVpc ();
444- vpcAllocationBody .setName (associatedVpc .getName ());
445- vpcAllocationBody .setId (associatedVpc .getId ());
508+ vpcAllocationBody .setName (vpc .getName ());
509+ vpcAllocationBody .setId (vpc .getId ());
446510 subnetBody .setVpc (vpcAllocationBody );
447511
448512 IpTreeAllocationTenant allocationTenant = new IpTreeAllocationTenant ();
@@ -455,12 +519,18 @@ private InlineResponse2004 createVpcSubnetInternal(VPCListing associatedVpc, Str
455519 subnetSites .setName (siteName );
456520 subnetBody .setSites (List .of (subnetSites ));
457521
458- subnetBody .setPurpose (SubnetBody . PurposeEnum . COMMON );
459- subnetBody .setPrefix (vNetCidr );
522+ subnetBody .setPurpose (purpose );
523+ subnetBody .setPrefix (subnetPrefix );
460524 IpamApi ipamApi = apiClient .getApiStubForMethod (IpamApi .class );
461- return ipamApi .apiV2IpamSubnetPost (subnetBody );
525+ InlineResponse2004 subnetResponse = ipamApi .apiV2IpamSubnetPost (subnetBody );
526+ if (subnetResponse == null || !subnetResponse .isIsSuccess ()) {
527+ String reason = subnetResponse == null ? "Empty response" : "Operation failed on Netris" ;
528+ logger .debug ("The Netris IPAM Subnet {} creation failed: {}" , subnetName , reason );
529+ throw new CloudRuntimeException (reason );
530+ }
531+ return subnetResponse .getData ();
462532 } catch (ApiException e ) {
463- logAndThrowException (String .format ("Error creating Netris Subnet %s for VPC %s" , vNetCidr , associatedVpc .getName ()), e );
533+ logAndThrowException (String .format ("Error creating Netris IPAM Subnet %s for VPC %s" , subnetPrefix , vpc .getName ()), e );
464534 return null ;
465535 }
466536 }
0 commit comments