1919
2020package com .cloud .network .resource ;
2121
22- import java .util .ArrayList ;
23- import java .util .List ;
2422import java .util .Map ;
2523
2624import javax .naming .ConfigurationException ;
3634import com .cloud .agent .api .ConfigurePublicIpsOnLogicalRouterCommand ;
3735import com .cloud .agent .api .ConfigureStaticNatRulesOnLogicalRouterAnswer ;
3836import com .cloud .agent .api .ConfigureStaticNatRulesOnLogicalRouterCommand ;
39- import com .cloud .agent .api .CreateLogicalRouterAnswer ;
40- import com .cloud .agent .api .CreateLogicalRouterCommand ;
4137import com .cloud .agent .api .DeleteLogicalRouterAnswer ;
4238import com .cloud .agent .api .DeleteLogicalRouterCommand ;
43- import com .cloud .agent .api .FindLogicalSwitchPortAnswer ;
44- import com .cloud .agent .api .FindLogicalSwitchPortCommand ;
4539import com .cloud .agent .api .PingCommand ;
4640import com .cloud .agent .api .StartupCommand ;
4741import com .cloud .agent .api .StartupNiciraNvpCommand ;
48- import com .cloud .agent .api .UpdateLogicalSwitchPortAnswer ;
49- import com .cloud .agent .api .UpdateLogicalSwitchPortCommand ;
5042import com .cloud .agent .api .to .PortForwardingRuleTO ;
5143import com .cloud .agent .api .to .StaticNatRuleTO ;
5244import com .cloud .host .Host ;
5345import com .cloud .host .Host .Type ;
5446import com .cloud .network .nicira .ControlClusterStatus ;
5547import com .cloud .network .nicira .DestinationNatRule ;
56- import com .cloud .network .nicira .L3GatewayAttachment ;
57- import com .cloud .network .nicira .LogicalRouter ;
5848import com .cloud .network .nicira .LogicalRouterPort ;
59- import com .cloud .network .nicira .LogicalSwitchPort ;
6049import com .cloud .network .nicira .Match ;
6150import com .cloud .network .nicira .NatRule ;
6251import com .cloud .network .nicira .NiciraNvpApi ;
6352import com .cloud .network .nicira .NiciraNvpApiException ;
6453import com .cloud .network .nicira .NiciraNvpList ;
65- import com .cloud .network .nicira .NiciraNvpTag ;
66- import com .cloud .network .nicira .PatchAttachment ;
67- import com .cloud .network .nicira .RouterNextHop ;
68- import com .cloud .network .nicira .SingleDefaultRouteImplicitRoutingConfig ;
6954import com .cloud .network .nicira .SourceNatRule ;
70- import com .cloud .network .nicira .VifAttachment ;
7155import com .cloud .network .utils .CommandRetryUtility ;
7256import com .cloud .resource .ServerResource ;
7357
@@ -205,13 +189,7 @@ public Answer executeRequest(final Command cmd) {
205189 // [TODO] Remove when all the commands are refactored.
206190 }
207191
208- if (cmd instanceof UpdateLogicalSwitchPortCommand ) {
209- return executeRequest ((UpdateLogicalSwitchPortCommand )cmd , NUM_RETRIES );
210- } else if (cmd instanceof FindLogicalSwitchPortCommand ) {
211- return executeRequest ((FindLogicalSwitchPortCommand )cmd , NUM_RETRIES );
212- } else if (cmd instanceof CreateLogicalRouterCommand ) {
213- return executeRequest ((CreateLogicalRouterCommand )cmd , NUM_RETRIES );
214- } else if (cmd instanceof DeleteLogicalRouterCommand ) {
192+ if (cmd instanceof DeleteLogicalRouterCommand ) {
215193 return executeRequest ((DeleteLogicalRouterCommand )cmd , NUM_RETRIES );
216194 } else if (cmd instanceof ConfigureStaticNatRulesOnLogicalRouterCommand ) {
217195 return executeRequest ((ConfigureStaticNatRulesOnLogicalRouterCommand )cmd , NUM_RETRIES );
@@ -237,135 +215,6 @@ public IAgentControl getAgentControl() {
237215 public void setAgentControl (final IAgentControl agentControl ) {
238216 }
239217
240- private Answer executeRequest (final UpdateLogicalSwitchPortCommand cmd , final int numRetries ) {
241- final String logicalSwitchUuid = cmd .getLogicalSwitchUuid ();
242- final String logicalSwitchPortUuid = cmd .getLogicalSwitchPortUuid ();
243- final String attachmentUuid = cmd .getAttachmentUuid ();
244-
245- try {
246- // Tags set to scope cs_account and account name
247- final List <NiciraNvpTag > tags = new ArrayList <NiciraNvpTag >();
248- tags .add (new NiciraNvpTag ("cs_account" , cmd .getOwnerName ()));
249-
250- niciraNvpApi .updateLogicalSwitchPortAttachment (logicalSwitchUuid , logicalSwitchPortUuid , new VifAttachment (attachmentUuid ));
251- return new UpdateLogicalSwitchPortAnswer (cmd , true , "Attachment for " + logicalSwitchPortUuid + " updated" , logicalSwitchPortUuid );
252- } catch (final NiciraNvpApiException e ) {
253- retryUtility .addRetry (cmd , NUM_RETRIES );
254- return retryUtility .retry (cmd , UpdateLogicalSwitchPortAnswer .class , e );
255- }
256- }
257-
258- private Answer executeRequest (final FindLogicalSwitchPortCommand cmd , final int numRetries ) {
259- final String logicalSwitchUuid = cmd .getLogicalSwitchUuid ();
260- final String logicalSwitchPortUuid = cmd .getLogicalSwitchPortUuid ();
261-
262- try {
263- final NiciraNvpList <LogicalSwitchPort > ports = niciraNvpApi .findLogicalSwitchPortsByUuid (logicalSwitchUuid , logicalSwitchPortUuid );
264- if (ports .getResultCount () == 0 ) {
265- return new FindLogicalSwitchPortAnswer (cmd , false , "Logical switchport " + logicalSwitchPortUuid + " not found" , null );
266- } else {
267- return new FindLogicalSwitchPortAnswer (cmd , true , "Logical switchport " + logicalSwitchPortUuid + " found" , logicalSwitchPortUuid );
268- }
269- } catch (final NiciraNvpApiException e ) {
270- retryUtility .addRetry (cmd , NUM_RETRIES );
271- return retryUtility .retry (cmd , FindLogicalSwitchPortAnswer .class , e );
272- }
273- }
274-
275- private Answer executeRequest (final CreateLogicalRouterCommand cmd , final int numRetries ) {
276- final String routerName = cmd .getName ();
277- final String gatewayServiceUuid = cmd .getGatewayServiceUuid ();
278- final String logicalSwitchUuid = cmd .getLogicalSwitchUuid ();
279-
280- final List <NiciraNvpTag > tags = new ArrayList <NiciraNvpTag >();
281- tags .add (new NiciraNvpTag ("cs_account" , cmd .getOwnerName ()));
282-
283- final String publicNetworkNextHopIp = cmd .getPublicNextHop ();
284- final String publicNetworkIpAddress = cmd .getPublicIpCidr ();
285- final String internalNetworkAddress = cmd .getInternalIpCidr ();
286-
287- s_logger .debug ("Creating a logical router with external ip " + publicNetworkIpAddress + " and internal ip " + internalNetworkAddress + "on gateway service " +
288- gatewayServiceUuid );
289-
290- try {
291- // Create the Router
292- LogicalRouter lrc = new LogicalRouter ();
293- lrc .setDisplayName (truncate (routerName , NAME_MAX_LEN ));
294- lrc .setTags (tags );
295- lrc .setRoutingConfig (new SingleDefaultRouteImplicitRoutingConfig (new RouterNextHop (publicNetworkNextHopIp )));
296- lrc = niciraNvpApi .createLogicalRouter (lrc );
297-
298- // store the switchport for rollback
299- LogicalSwitchPort lsp = null ;
300-
301- try {
302- // Create the outside port for the router
303- LogicalRouterPort lrpo = new LogicalRouterPort ();
304- lrpo .setAdminStatusEnabled (true );
305- lrpo .setDisplayName (truncate (routerName + "-outside-port" , NAME_MAX_LEN ));
306- lrpo .setTags (tags );
307- final List <String > outsideIpAddresses = new ArrayList <String >();
308- outsideIpAddresses .add (publicNetworkIpAddress );
309- lrpo .setIpAddresses (outsideIpAddresses );
310- lrpo = niciraNvpApi .createLogicalRouterPort (lrc .getUuid (), lrpo );
311-
312- // Attach the outside port to the gateway service on the correct VLAN
313- final L3GatewayAttachment attachment = new L3GatewayAttachment (gatewayServiceUuid );
314- if (cmd .getVlanId () != 0 ) {
315- attachment .setVlanId (cmd .getVlanId ());
316- }
317- niciraNvpApi .updateLogicalRouterPortAttachment (lrc .getUuid (), lrpo .getUuid (), attachment );
318-
319- // Create the inside port for the router
320- LogicalRouterPort lrpi = new LogicalRouterPort ();
321- lrpi .setAdminStatusEnabled (true );
322- lrpi .setDisplayName (truncate (routerName + "-inside-port" , NAME_MAX_LEN ));
323- lrpi .setTags (tags );
324- final List <String > insideIpAddresses = new ArrayList <String >();
325- insideIpAddresses .add (internalNetworkAddress );
326- lrpi .setIpAddresses (insideIpAddresses );
327- lrpi = niciraNvpApi .createLogicalRouterPort (lrc .getUuid (), lrpi );
328-
329- // Create the inside port on the lswitch
330- lsp = new LogicalSwitchPort (truncate (routerName + "-inside-port" , NAME_MAX_LEN ), tags , true );
331- lsp = niciraNvpApi .createLogicalSwitchPort (logicalSwitchUuid , lsp );
332-
333- // Attach the inside router port to the lswitch port with a PatchAttachment
334- niciraNvpApi .updateLogicalRouterPortAttachment (lrc .getUuid (), lrpi .getUuid (), new PatchAttachment (lsp .getUuid ()));
335-
336- // Attach the inside lswitch port to the router with a PatchAttachment
337- niciraNvpApi .updateLogicalSwitchPortAttachment (logicalSwitchUuid , lsp .getUuid (), new PatchAttachment (lrpi .getUuid ()));
338-
339- // Setup the source nat rule
340- final SourceNatRule snr = new SourceNatRule ();
341- snr .setToSourceIpAddressMin (publicNetworkIpAddress .split ("/" )[0 ]);
342- snr .setToSourceIpAddressMax (publicNetworkIpAddress .split ("/" )[0 ]);
343- final Match match = new Match ();
344- match .setSourceIpAddresses (internalNetworkAddress );
345- snr .setMatch (match );
346- snr .setOrder (200 );
347- niciraNvpApi .createLogicalRouterNatRule (lrc .getUuid (), snr );
348- } catch (final NiciraNvpApiException e ) {
349- // We need to destroy the router if we already created it
350- // this will also take care of any router ports and rules
351- try {
352- niciraNvpApi .deleteLogicalRouter (lrc .getUuid ());
353- if (lsp != null ) {
354- niciraNvpApi .deleteLogicalSwitchPort (logicalSwitchUuid , lsp .getUuid ());
355- }
356- } catch (final NiciraNvpApiException ex ) {
357- }
358-
359- throw e ;
360- }
361-
362- return new CreateLogicalRouterAnswer (cmd , true , "Logical Router created (uuid " + lrc .getUuid () + ")" , lrc .getUuid ());
363- } catch (final NiciraNvpApiException e ) {
364- retryUtility .addRetry (cmd , NUM_RETRIES );
365- return retryUtility .retry (cmd , CreateLogicalRouterAnswer .class , e );
366- }
367- }
368-
369218 private Answer executeRequest (final DeleteLogicalRouterCommand cmd , final int numRetries ) {
370219 try {
371220 niciraNvpApi .deleteLogicalRouter (cmd .getLogicalRouterUuid ());
0 commit comments