1616// under the License.
1717package org .apache .cloudstack .resource ;
1818
19- import static org .apache .cloudstack .utils .NsxApiClientUtils .PoolAllocation .ROUTING ;
20- import static org .apache .cloudstack .utils .NsxApiClientUtils .HAMode .ACTIVE_STANDBY ;
21- import static org .apache .cloudstack .utils .NsxApiClientUtils .createApiClient ;
22-
2319import com .cloud .agent .IAgentControl ;
24- import com .cloud .agent .api .StartupCommand ;
25- import com .cloud .agent .api .Command ;
26- import com .cloud .agent .api .ReadyCommand ;
27- import com .cloud .agent .api .ReadyAnswer ;
2820import com .cloud .agent .api .Answer ;
21+ import com .cloud .agent .api .Command ;
2922import com .cloud .agent .api .PingCommand ;
23+ import com .cloud .agent .api .ReadyAnswer ;
24+ import com .cloud .agent .api .ReadyCommand ;
25+ import com .cloud .agent .api .StartupCommand ;
3026import com .cloud .exception .InvalidParameterValueException ;
3127import com .cloud .host .Host ;
3228import com .cloud .resource .ServerResource ;
3329import com .cloud .utils .exception .CloudRuntimeException ;
30+ import com .vmware .nsx .EdgeClusters ;
31+ import com .vmware .nsx .model .EdgeCluster ;
32+ import com .vmware .nsx_policy .infra .Segments ;
3433import com .vmware .nsx_policy .infra .Tier1s ;
34+ import com .vmware .nsx_policy .infra .segments .ServiceSegments ;
3535import com .vmware .nsx_policy .infra .tier_0s .LocaleServices ;
3636import com .vmware .nsx_policy .model .ApiError ;
3737import com .vmware .nsx_policy .model .ChildLocaleServices ;
3838import com .vmware .nsx_policy .model .LocaleServicesListResult ;
39+ import com .vmware .nsx_policy .model .Segment ;
40+ import com .vmware .nsx_policy .model .SegmentSubnet ;
41+ import com .vmware .nsx_policy .model .ServiceSegmentListResult ;
3942import com .vmware .nsx_policy .model .Tier1 ;
4043import com .vmware .vapi .bindings .Service ;
4144import com .vmware .vapi .std .errors .Error ;
4245import org .apache .cloudstack .NsxAnswer ;
4346import org .apache .cloudstack .StartupNsxCommand ;
47+ import org .apache .cloudstack .agent .api .CreateNsxSegmentCommand ;
4448import org .apache .cloudstack .agent .api .CreateNsxTier1GatewayCommand ;
49+ import org .apache .cloudstack .agent .api .DeleteNsxSegmentCommand ;
4550import org .apache .cloudstack .agent .api .DeleteNsxTier1GatewayCommand ;
4651import org .apache .cloudstack .service .NsxApi ;
52+ import org .apache .cloudstack .utils .NsxApiClientUtils ;
4753import org .apache .log4j .Logger ;
4854
49-
5055import javax .naming .ConfigurationException ;
5156import java .util .List ;
5257import java .util .Map ;
5358import java .util .function .Function ;
5459
60+ import static java .util .Objects .isNull ;
61+ import static org .apache .cloudstack .utils .NsxApiClientUtils .HAMode .ACTIVE_STANDBY ;
62+ import static org .apache .cloudstack .utils .NsxApiClientUtils .FailoverMode .PREEMPTIVE ;
63+ import static org .apache .cloudstack .utils .NsxApiClientUtils .PoolAllocation .ROUTING ;
64+ import static org .apache .cloudstack .utils .NsxApiClientUtils .createApiClient ;
65+
5566public class NsxResource implements ServerResource {
5667 private static final Logger LOGGER = Logger .getLogger (NsxResource .class );
5768 private static final String TIER_0_GATEWAY_PATH_PREFIX = "/infra/tier-0s/" ;
69+ private static final String TIER_1_GATEWAY_PATH_PREFIX = "/infra/tier-1s/" ;
5870 private static final String TIER_1_RESOURCE_TYPE = "Tier1" ;
59- // private static final String ROUTING = "ROUTING";
71+ private static final String SEGMENT_RESOURCE_TYPE = "Segment" ;
72+
6073 private String name ;
6174 protected String hostname ;
6275 protected String username ;
@@ -98,7 +111,11 @@ public Answer executeRequest(Command cmd) {
98111 return executeRequest ((ReadyCommand ) cmd );
99112 } else if (cmd instanceof DeleteNsxTier1GatewayCommand ) {
100113 return executeRequest ((DeleteNsxTier1GatewayCommand ) cmd );
101- } else if (cmd instanceof CreateNsxTier1GatewayCommand ) {
114+ } else if (cmd instanceof DeleteNsxSegmentCommand ) {
115+ return executeRequest ((DeleteNsxSegmentCommand ) cmd );
116+ } else if (cmd instanceof CreateNsxSegmentCommand ) {
117+ return executeRequest ((CreateNsxSegmentCommand ) cmd );
118+ } else if (cmd instanceof CreateNsxTier1GatewayCommand ) {
102119 return executeRequest ((CreateNsxTier1GatewayCommand ) cmd );
103120 } else {
104121 return Answer .createUnsupportedCommandAnswer (cmd );
@@ -206,15 +223,13 @@ private Answer executeRequest(ReadyCommand cmd) {
206223 }
207224
208225 private Function <Class , Service > nsxService = svcClass -> { return nsxApi .getApiClient ().createStub (svcClass ); };
209- private Service getService (Class svcClass ) {
210- return nsxApi .getApiClient ().createStub (svcClass );
211- }
212226 private Answer executeRequest (CreateNsxTier1GatewayCommand cmd ) {
213227 String name = getTier1GatewayName (cmd );
214228 Tier1 tier1 = getTier1Gateway (name );
215229 if (tier1 != null ) {
216230 throw new InvalidParameterValueException (String .format ("VPC network with name %s exists in NSX zone: %s and account %s" , name , cmd .getZoneName (), cmd .getAccountName ()));
217231 }
232+
218233 List <com .vmware .nsx_policy .model .LocaleServices > localeServices = getTier0LocalServices (tier0Gateway );
219234 String tier0GatewayPath = TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway ;
220235
@@ -224,14 +239,15 @@ private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
224239 .setResourceType (TIER_1_RESOURCE_TYPE )
225240 .setPoolAllocation (ROUTING .name ())
226241 .setHaMode (ACTIVE_STANDBY .name ())
242+ .setFailoverMode (PREEMPTIVE .name ())
227243 .setId (name )
228244 .setDisplayName (name )
229245 .setChildren (
230246 List .of (new ChildLocaleServices .Builder ("ChildLocaleServices" )
231247 .setLocaleServices (
232248 new com .vmware .nsx_policy .model .LocaleServices .Builder ()
233249 .setEdgeClusterPath (localeServices .get (0 ).getEdgeClusterPath ())
234- .setId ( localeServices . get ( 0 ). getId ( ))
250+ .setParentPath ( TIER_1_GATEWAY_PATH_PREFIX + getTier1GatewayName ( cmd ))
235251 .setResourceType ("LocaleServices" )
236252 .build ()
237253 ).build ())).build ();
@@ -254,16 +270,69 @@ private Answer executeRequest(DeleteNsxTier1GatewayCommand cmd) {
254270 return new NsxAnswer (cmd , true , null );
255271 }
256272
273+ private Answer executeRequest (CreateNsxSegmentCommand cmd ) {
274+ try {
275+ String segmentName = getSegmentName (cmd );
276+ Segments segmentService = (Segments ) nsxService .apply (Segments .class );
277+ SegmentSubnet subnet = new SegmentSubnet .Builder ()
278+ .setGatewayAddress (cmd .getTierNetwork ().getGateway () + "/" + cmd .getTierNetwork ().getCidr ().split ("/" )[1 ]).build ();
279+ Segment segment = new Segment .Builder ()
280+ .setResourceType (SEGMENT_RESOURCE_TYPE )
281+ .setId (segmentName )
282+ .setDisplayName (segmentName )
283+ .setConnectivityPath (isNull (cmd .getVpcName ()) ? TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway
284+ : TIER_1_GATEWAY_PATH_PREFIX + getTier1GatewayName (cmd ))
285+ .setAdminState (NsxApiClientUtils .AdminState .UP .name ())
286+ .setSubnets (List .of (subnet ))
287+ .build ();
288+ segmentService .patch (segmentName , segment );
289+ } catch (Exception e ) {
290+ LOGGER .error (String .format ("Failed to create network: %s" , cmd .getTierNetwork ().getName ()));
291+ return new NsxAnswer (cmd , new CloudRuntimeException (e .getMessage ()));
292+ }
293+ return new NsxAnswer (cmd , true , null );
294+ }
295+
296+ private NsxAnswer executeRequest (DeleteNsxSegmentCommand cmd ) {
297+ try {
298+ String segmentName = getSegmentName (cmd );
299+ Segments segmentService = (Segments ) nsxService .apply (Segments .class );
300+ segmentService .delete (segmentName );
301+ } catch (Exception e ) {
302+ LOGGER .error (String .format ("Failed to delete NSX segment: %s" , getSegmentName (cmd )) );
303+ return new NsxAnswer (cmd , new CloudRuntimeException (e .getMessage ()));
304+ }
305+ return new NsxAnswer (cmd , true , null );
306+ }
307+
257308 private List <com .vmware .nsx_policy .model .LocaleServices > getTier0LocalServices (String tier0Gateway ) {
258309 try {
259310 LocaleServices tier0LocaleServices = (LocaleServices ) nsxService .apply (LocaleServices .class );
260- LocaleServicesListResult result =tier0LocaleServices .list (tier0Gateway , null , false , null , null , null , null );
311+ LocaleServicesListResult result = tier0LocaleServices .list (tier0Gateway , null , false , null , null , null , null );
261312 return result .getResults ();
262313 } catch (Exception e ) {
263314 throw new CloudRuntimeException (String .format ("Failed to fetch locale services for tier gateway %s due to %s" , tier0Gateway , e .getMessage ()));
264315 }
265316 }
266317
318+ private EdgeCluster getEdgeClusterDetails (String edgeClusterName ) {
319+ try {
320+ EdgeClusters edgeClusterService = (EdgeClusters ) nsxService .apply (EdgeClusters .class );
321+ return edgeClusterService .get (edgeClusterName );
322+ } catch (Exception e ) {
323+ throw new CloudRuntimeException (String .format ("Failed to fetch details of edge cluster: %s, due to: %s" , edgeClusterName , e .getMessage ()));
324+ }
325+ }
326+
327+ private ServiceSegmentListResult listServiceSegments () {
328+ try {
329+ ServiceSegments serviceSegmentSvc = (ServiceSegments ) nsxService .apply (ServiceSegments .class );
330+ return serviceSegmentSvc .list (null , null , null , true , null );
331+ } catch (Exception e ) {
332+ throw new CloudRuntimeException (String .format ("Failed to fetch service segment list due to %s" , e .getMessage ()));
333+ }
334+ }
335+
267336 private Tier1 getTier1Gateway (String tier1GatewayId ) {
268337 try {
269338 Tier1s tier1service = (Tier1s ) nsxService .apply (Tier1s .class );
@@ -278,6 +347,10 @@ private String getTier1GatewayName(CreateNsxTier1GatewayCommand cmd) {
278347 return cmd .getZoneName () + "-" + cmd .getAccountName () + "-" + cmd .getVpcName ();
279348 }
280349
350+ private String getSegmentName (CreateNsxSegmentCommand cmd ) {
351+ return cmd .getAccountName () + "-" + cmd .getTierNetwork ().getName ();
352+ }
353+
281354 @ Override
282355 public boolean start () {
283356 return true ;
0 commit comments