2626import com .vmware .nsx .model .TransportZone ;
2727import com .vmware .nsx .model .TransportZoneListResult ;
2828import com .vmware .nsx_policy .infra .DhcpRelayConfigs ;
29+ import com .vmware .nsx_policy .infra .IpDiscoveryProfiles ;
2930import com .vmware .nsx_policy .infra .LbAppProfiles ;
3031import com .vmware .nsx_policy .infra .LbMonitorProfiles ;
3132import com .vmware .nsx_policy .infra .LbPools ;
3233import com .vmware .nsx_policy .infra .LbServices ;
3334import com .vmware .nsx_policy .infra .LbVirtualServers ;
35+ import com .vmware .nsx_policy .infra .MacDiscoveryProfiles ;
36+ import com .vmware .nsx_policy .infra .SegmentSecurityProfiles ;
3437import com .vmware .nsx_policy .infra .Segments ;
3538import com .vmware .nsx_policy .infra .Services ;
3639import com .vmware .nsx_policy .infra .Sites ;
3942import com .vmware .nsx_policy .infra .domains .SecurityPolicies ;
4043import com .vmware .nsx_policy .infra .domains .groups .members .SegmentPorts ;
4144import com .vmware .nsx_policy .infra .domains .security_policies .Rules ;
45+ import com .vmware .nsx_policy .infra .segments .SegmentDiscoveryProfileBindingMaps ;
46+ import com .vmware .nsx_policy .infra .segments .SegmentSecurityProfileBindingMaps ;
4247import com .vmware .nsx_policy .infra .sites .EnforcementPoints ;
4348import com .vmware .nsx_policy .infra .tier_0s .LocaleServices ;
4449import com .vmware .nsx_policy .infra .tier_1s .nat .NatRules ;
4954import com .vmware .nsx_policy .model .Group ;
5055import com .vmware .nsx_policy .model .GroupListResult ;
5156import com .vmware .nsx_policy .model .ICMPTypeServiceEntry ;
57+ import com .vmware .nsx_policy .model .IPDiscoveryProfile ;
5258import com .vmware .nsx_policy .model .L4PortSetServiceEntry ;
5359import com .vmware .nsx_policy .model .LBAppProfileListResult ;
5460import com .vmware .nsx_policy .model .LBIcmpMonitorProfile ;
6167import com .vmware .nsx_policy .model .LBVirtualServer ;
6268import com .vmware .nsx_policy .model .LBVirtualServerListResult ;
6369import com .vmware .nsx_policy .model .LocaleServicesListResult ;
70+ import com .vmware .nsx_policy .model .MacDiscoveryProfile ;
6471import com .vmware .nsx_policy .model .PathExpression ;
6572import com .vmware .nsx_policy .model .PolicyGroupMembersListResult ;
6673import com .vmware .nsx_policy .model .PolicyNatRule ;
6976import com .vmware .nsx_policy .model .Rule ;
7077import com .vmware .nsx_policy .model .SecurityPolicy ;
7178import com .vmware .nsx_policy .model .Segment ;
79+ import com .vmware .nsx_policy .model .SegmentDiscoveryProfileBindingMap ;
80+ import com .vmware .nsx_policy .model .SegmentSecurityProfile ;
81+ import com .vmware .nsx_policy .model .SegmentSecurityProfileBindingMap ;
7282import com .vmware .nsx_policy .model .SegmentSubnet ;
7383import com .vmware .nsx_policy .model .ServiceListResult ;
7484import com .vmware .nsx_policy .model .Site ;
90100import org .apache .cloudstack .resource .NsxNetworkRule ;
91101import org .apache .cloudstack .utils .NsxControllerUtils ;
92102import org .apache .commons .collections .CollectionUtils ;
103+ import org .apache .commons .lang3 .BooleanUtils ;
104+ import org .apache .commons .lang3 .StringUtils ;
93105import org .apache .logging .log4j .LogManager ;
94106import org .apache .logging .log4j .Logger ;
95- import org .apache .commons .lang3 .BooleanUtils ;
96107
97108import java .util .ArrayList ;
98109import java .util .List ;
@@ -128,6 +139,8 @@ public class NsxApiClient {
128139 private static final String TIER_1_RESOURCE_TYPE = "Tier1" ;
129140 private static final String TIER_1_LOCALE_SERVICE_ID = "default" ;
130141 private static final String SEGMENT_RESOURCE_TYPE = "Segment" ;
142+ private static final String SEGMENT_DISCOVERY_PROFILE_BINDING_ID = "cloudstack-discovery-profile-binding" ;
143+ private static final String SEGMENT_SECURITY_PROFILE_BINDING_ID = "cloudstack-security-profile-binding" ;
131144 private static final String TIER_0_GATEWAY_PATH_PREFIX = "/infra/tier-0s/" ;
132145 private static final String TIER_1_GATEWAY_PATH_PREFIX = "/infra/tier-1s/" ;
133146 protected static final String SEGMENTS_PATH = "/infra/segments" ;
@@ -466,7 +479,16 @@ public TransportZoneListResult getTransportZones() {
466479
467480 public void createSegment (String segmentName , String tier1GatewayName , String gatewayAddress , String enforcementPointPath ,
468481 List <TransportZone > transportZones ) {
482+ createSegment (segmentName , tier1GatewayName , gatewayAddress , enforcementPointPath , transportZones , null , null , null );
483+ }
484+
485+ public void createSegment (String segmentName , String tier1GatewayName , String gatewayAddress , String enforcementPointPath ,
486+ List <TransportZone > transportZones , String ipDiscoveryProfileId , String macDiscoveryProfileId ,
487+ String segmentSecurityProfileId ) {
469488 try {
489+ String ipDiscoveryProfilePath = getIpDiscoveryProfilePath (ipDiscoveryProfileId );
490+ String macDiscoveryProfilePath = getMacDiscoveryProfilePath (macDiscoveryProfileId );
491+ String segmentSecurityProfilePath = getSegmentSecurityProfilePath (segmentSecurityProfileId );
470492 Segments segmentService = (Segments ) nsxService .apply (Segments .class );
471493 SegmentSubnet subnet = new SegmentSubnet .Builder ()
472494 .setGatewayAddress (gatewayAddress )
@@ -481,6 +503,7 @@ public void createSegment(String segmentName, String tier1GatewayName, String ga
481503 .setTransportZonePath (enforcementPointPath + "/transport-zones/" + transportZones .get (0 ).getId ())
482504 .build ();
483505 segmentService .patch (segmentName , segment );
506+ bindSegmentProfiles (segmentName , ipDiscoveryProfilePath , macDiscoveryProfilePath , segmentSecurityProfilePath );
484507 } catch (Error error ) {
485508 ApiError ae = error .getData ()._convertTo (ApiError .class );
486509 String msg = String .format ("Error creating segment %s: %s" , segmentName , ae .getErrorMessage ());
@@ -489,6 +512,63 @@ public void createSegment(String segmentName, String tier1GatewayName, String ga
489512 }
490513 }
491514
515+ protected String getIpDiscoveryProfilePath (String profileId ) {
516+ if (StringUtils .isBlank (profileId )) {
517+ return null ;
518+ }
519+ IpDiscoveryProfiles profiles = (IpDiscoveryProfiles ) nsxService .apply (IpDiscoveryProfiles .class );
520+ IPDiscoveryProfile profile = profiles .get (profileId );
521+ return validateProfilePath (profileId , profile .getPath ());
522+ }
523+
524+ protected String getMacDiscoveryProfilePath (String profileId ) {
525+ if (StringUtils .isBlank (profileId )) {
526+ return null ;
527+ }
528+ MacDiscoveryProfiles profiles = (MacDiscoveryProfiles ) nsxService .apply (MacDiscoveryProfiles .class );
529+ MacDiscoveryProfile profile = profiles .get (profileId );
530+ return validateProfilePath (profileId , profile .getPath ());
531+ }
532+
533+ protected String getSegmentSecurityProfilePath (String profileId ) {
534+ if (StringUtils .isBlank (profileId )) {
535+ return null ;
536+ }
537+ SegmentSecurityProfiles profiles = (SegmentSecurityProfiles ) nsxService .apply (SegmentSecurityProfiles .class );
538+ SegmentSecurityProfile profile = profiles .get (profileId );
539+ return validateProfilePath (profileId , profile .getPath ());
540+ }
541+
542+ protected String validateProfilePath (String profileId , String profilePath ) {
543+ if (StringUtils .isBlank (profilePath )) {
544+ throw new CloudRuntimeException (String .format ("NSX profile %s did not return a canonical resource path" , profileId ));
545+ }
546+ return profilePath ;
547+ }
548+
549+ protected void bindSegmentProfiles (String segmentName , String ipDiscoveryProfilePath , String macDiscoveryProfilePath ,
550+ String segmentSecurityProfilePath ) {
551+ if (StringUtils .isNotBlank (ipDiscoveryProfilePath ) || StringUtils .isNotBlank (macDiscoveryProfilePath )) {
552+ SegmentDiscoveryProfileBindingMaps discoveryBindings =
553+ (SegmentDiscoveryProfileBindingMaps ) nsxService .apply (SegmentDiscoveryProfileBindingMaps .class );
554+ SegmentDiscoveryProfileBindingMap binding = new SegmentDiscoveryProfileBindingMap .Builder ()
555+ .setId (SEGMENT_DISCOVERY_PROFILE_BINDING_ID )
556+ .setIpDiscoveryProfilePath (ipDiscoveryProfilePath )
557+ .setMacDiscoveryProfilePath (macDiscoveryProfilePath )
558+ .build ();
559+ discoveryBindings .patch (segmentName , SEGMENT_DISCOVERY_PROFILE_BINDING_ID , binding );
560+ }
561+ if (StringUtils .isNotBlank (segmentSecurityProfilePath )) {
562+ SegmentSecurityProfileBindingMaps securityBindings =
563+ (SegmentSecurityProfileBindingMaps ) nsxService .apply (SegmentSecurityProfileBindingMaps .class );
564+ SegmentSecurityProfileBindingMap binding = new SegmentSecurityProfileBindingMap .Builder ()
565+ .setId (SEGMENT_SECURITY_PROFILE_BINDING_ID )
566+ .setSegmentSecurityProfilePath (segmentSecurityProfilePath )
567+ .build ();
568+ securityBindings .patch (segmentName , SEGMENT_SECURITY_PROFILE_BINDING_ID , binding );
569+ }
570+ }
571+
492572 public void deleteSegment (long zoneId , long domainId , long accountId , Long vpcId , long networkId , String segmentName ) {
493573 try {
494574 removeSegmentDistributedFirewallRules (segmentName );
0 commit comments