22
22
import com .vmware .nsx .model .TransportZoneListResult ;
23
23
import com .vmware .nsx_policy .infra .DhcpRelayConfigs ;
24
24
import com .vmware .nsx_policy .infra .LbAppProfiles ;
25
+ import com .vmware .nsx_policy .infra .LbMonitorProfiles ;
25
26
import com .vmware .nsx_policy .infra .LbPools ;
26
27
import com .vmware .nsx_policy .infra .LbServices ;
27
28
import com .vmware .nsx_policy .infra .LbVirtualServers ;
44
45
import com .vmware .nsx_policy .model .ICMPTypeServiceEntry ;
45
46
import com .vmware .nsx_policy .model .L4PortSetServiceEntry ;
46
47
import com .vmware .nsx_policy .model .LBAppProfileListResult ;
48
+ import com .vmware .nsx_policy .model .LBMonitorProfileListResult ;
47
49
import com .vmware .nsx_policy .model .LBPool ;
48
50
import com .vmware .nsx_policy .model .LBPoolListResult ;
49
51
import com .vmware .nsx_policy .model .LBPoolMember ;
50
52
import com .vmware .nsx_policy .model .LBService ;
53
+ import com .vmware .nsx_policy .model .LBTcpMonitorProfile ;
54
+ import com .vmware .nsx_policy .model .LBUdpMonitorProfile ;
51
55
import com .vmware .nsx_policy .model .LBVirtualServer ;
52
56
import com .vmware .nsx_policy .model .LBVirtualServerListResult ;
53
57
import com .vmware .nsx_policy .model .LocaleServicesListResult ;
95
99
import static org .apache .cloudstack .utils .NsxControllerUtils .getServiceEntryName ;
96
100
import static org .apache .cloudstack .utils .NsxControllerUtils .getLoadBalancerName ;
97
101
import static org .apache .cloudstack .utils .NsxControllerUtils .getLoadBalancerAlgorithm ;
102
+ import static org .apache .cloudstack .utils .NsxControllerUtils .getActiveMonitorProfileName ;
98
103
99
104
public class NsxApiClient {
100
105
@@ -113,6 +118,10 @@ public class NsxApiClient {
113
118
protected static final String SEGMENTS_PATH = "/infra/segments" ;
114
119
protected static final String DEFAULT_DOMAIN = "default" ;
115
120
protected static final String GROUPS_PATH_PREFIX = "/infra/domains/default/groups" ;
121
+ // TODO: Pass as global / zone-level setting?
122
+ protected static final String NSX_LB_PASSIVE_MONITOR = "/infra/lb-monitor-profiles/default-passive-lb-monitor" ;
123
+ protected static final String TCP_MONITOR_PROFILE = "LBTcpMonitorProfile" ;
124
+ protected static final String UDP_MONITOR_PROFILE = "LBUdpMonitorProfile" ;
116
125
117
126
private enum PoolAllocation { ROUTING , LB_SMALL , LB_MEDIUM , LB_LARGE , LB_XLARGE }
118
127
@@ -549,15 +558,19 @@ List<LBPoolMember> getLbPoolMembers(List<NsxLoadBalancerMember> memberList, Stri
549
558
}
550
559
return members ;
551
560
}
552
- public void createNsxLbServerPool (List <NsxLoadBalancerMember > memberList , String tier1GatewayName , String lbServerPoolName , String algorithm ) {
561
+ public void createNsxLbServerPool (List <NsxLoadBalancerMember > memberList , String tier1GatewayName , String lbServerPoolName ,
562
+ String algorithm , String privatePort , String protocol ) {
553
563
try {
564
+ String activeMonitorPath = getLbActiveMonitorPath (lbServerPoolName , privatePort , protocol );
554
565
List <LBPoolMember > members = getLbPoolMembers (memberList , tier1GatewayName );
555
566
LbPools lbPools = (LbPools ) nsxService .apply (LbPools .class );
556
567
LBPool lbPool = new LBPool .Builder ()
557
568
.setId (lbServerPoolName )
558
569
.setDisplayName (lbServerPoolName )
559
570
.setAlgorithm (getLoadBalancerAlgorithm (algorithm ))
560
571
.setMembers (members )
572
+ .setPassiveMonitorPath (NSX_LB_PASSIVE_MONITOR )
573
+ .setActiveMonitorPaths (List .of (activeMonitorPath ))
561
574
.build ();
562
575
lbPools .patch (lbServerPoolName , lbPool );
563
576
} catch (Error error ) {
@@ -568,6 +581,32 @@ public void createNsxLbServerPool(List<NsxLoadBalancerMember> memberList, String
568
581
}
569
582
}
570
583
584
+ private String getLbActiveMonitorPath (String lbServerPoolName , String port , String protocol ) {
585
+ LbMonitorProfiles lbActiveMonitor = (LbMonitorProfiles ) nsxService .apply (LbMonitorProfiles .class );
586
+ String lbMonitorProfileId = getActiveMonitorProfileName (lbServerPoolName , port , protocol );
587
+ if ("TCP" .equals (protocol .toUpperCase (Locale .ROOT ))) {
588
+ LBTcpMonitorProfile lbTcpMonitorProfile = new LBTcpMonitorProfile .Builder (TCP_MONITOR_PROFILE )
589
+ .setDisplayName (lbMonitorProfileId )
590
+ .setMonitorPort (Long .parseLong (port ))
591
+ .build ();
592
+ lbActiveMonitor .patch (lbMonitorProfileId , lbTcpMonitorProfile );
593
+ } else if ("UDP" .equals (protocol .toUpperCase (Locale .ROOT ))) {
594
+ LBUdpMonitorProfile lbUdpMonitorProfile = new LBUdpMonitorProfile .Builder (UDP_MONITOR_PROFILE )
595
+ .setDisplayName (lbMonitorProfileId )
596
+ .setMonitorPort (Long .parseLong (port ))
597
+ .build ();
598
+ lbActiveMonitor .patch (lbMonitorProfileId , lbUdpMonitorProfile );
599
+ }
600
+
601
+ LBMonitorProfileListResult listResult = listLBActiveMonitors (lbActiveMonitor );
602
+ Optional <Structure > monitorProfile = listResult .getResults ().stream ().filter (profile -> profile ._getDataValue ().getField ("id" ).toString ().equals (lbMonitorProfileId )).findFirst ();
603
+ return monitorProfile .map (structure -> structure ._getDataValue ().getField ("path" ).toString ()).orElse (null );
604
+ }
605
+
606
+ LBMonitorProfileListResult listLBActiveMonitors (LbMonitorProfiles lbActiveMonitor ) {
607
+ return lbActiveMonitor .list (null , false , null , null , null , null );
608
+ }
609
+
571
610
public void createNsxLoadBalancer (String tier1GatewayName ) {
572
611
try {
573
612
String lbName = getLoadBalancerName (tier1GatewayName );
@@ -593,10 +632,10 @@ public void createNsxLoadBalancer(String tier1GatewayName) {
593
632
}
594
633
595
634
public void createAndAddNsxLbVirtualServer (String tier1GatewayName , long lbId , String publicIp , String publicPort ,
596
- List <NsxLoadBalancerMember > memberList , String algorithm , String protocol ) {
635
+ List <NsxLoadBalancerMember > memberList , String algorithm , String protocol , String privatePort ) {
597
636
try {
598
637
String lbServerPoolName = getServerPoolName (tier1GatewayName , lbId );
599
- createNsxLbServerPool (memberList , tier1GatewayName , lbServerPoolName , algorithm );
638
+ createNsxLbServerPool (memberList , tier1GatewayName , lbServerPoolName , algorithm , privatePort , protocol );
600
639
createNsxLoadBalancer (tier1GatewayName );
601
640
602
641
String lbVirtualServerName = getVirtualServerName (tier1GatewayName , lbId );
@@ -632,6 +671,14 @@ public void deleteNsxLbResources(String tier1GatewayName, long lbId) {
632
671
String lbServerPoolName = getServerPoolName (tier1GatewayName , lbId );
633
672
lbPools .delete (lbServerPoolName , false );
634
673
674
+ // delete associated LB Active monitor profile
675
+ LbMonitorProfiles lbActiveMonitor = (LbMonitorProfiles ) nsxService .apply (LbMonitorProfiles .class );
676
+ LBMonitorProfileListResult listResult = listLBActiveMonitors (lbActiveMonitor );
677
+ List <String > profileIds = listResult .getResults ().stream ().filter (profile -> profile ._getDataValue ().getField ("id" ).toString ().contains (lbServerPoolName ))
678
+ .map (profile -> profile ._getDataValue ().getField ("id" ).toString ()).collect (Collectors .toList ());
679
+ for (String profileId : profileIds ) {
680
+ lbActiveMonitor .delete (profileId , true );
681
+ }
635
682
// Delete load balancer
636
683
LBVirtualServerListResult lbVsListResult = lbVirtualServers .list (null , null , null , null , null , null );
637
684
LBPoolListResult lbPoolListResult = lbPools .list (null , null , null , null , null , null );
0 commit comments