cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wid...@apache.org
Subject [13/50] [abbrv] git commit: updated refs/heads/reporter to 28300b5
Date Wed, 08 Jul 2015 19:19:48 GMT
CLOUDSTACK-8590 - Refactoring NiciraNVP resource

   - Refactoring NiciraNvpResource
   - Added NiciraNvpUtilities and NiciraNvpCreateLogicalSwitchCommandWrapper
   - Removing 1 execute methods form NiciraNvpResource
   - Added 1 unit test

Signed-off-by: wilderrodrigues <wrodrigues@schubergphilis.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/27c9651b
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/27c9651b
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/27c9651b

Branch: refs/heads/reporter
Commit: 27c9651b324926f11ced1ccbee29877f310b016b
Parents: 6a51294
Author: wilderrodrigues <wrodrigues@schubergphilis.com>
Authored: Tue Jun 23 10:47:23 2015 +0200
Committer: wilderrodrigues <wrodrigues@schubergphilis.com>
Committed: Wed Jul 1 15:04:56 2015 +0200

----------------------------------------------------------------------
 .../network/resource/NiciraNvpResource.java     | 58 ++++++----------
 .../network/resource/NiciraNvpUtilities.java    | 42 +++++++++++
 ...iraNvpCreateLogicalSwitchCommandWrapper.java | 73 ++++++++++++++++++++
 .../resource/NiciraNvpRequestWrapperTest.java   | 41 +++++++++++
 4 files changed, 176 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/27c9651b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpResource.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpResource.java
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpResource.java
index 9af541f..5242154 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpResource.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpResource.java
@@ -38,8 +38,6 @@ import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer;
 import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand;
 import com.cloud.agent.api.CreateLogicalRouterAnswer;
 import com.cloud.agent.api.CreateLogicalRouterCommand;
-import com.cloud.agent.api.CreateLogicalSwitchAnswer;
-import com.cloud.agent.api.CreateLogicalSwitchCommand;
 import com.cloud.agent.api.CreateLogicalSwitchPortAnswer;
 import com.cloud.agent.api.CreateLogicalSwitchPortCommand;
 import com.cloud.agent.api.DeleteLogicalRouterAnswer;
@@ -64,7 +62,6 @@ import com.cloud.network.nicira.DestinationNatRule;
 import com.cloud.network.nicira.L3GatewayAttachment;
 import com.cloud.network.nicira.LogicalRouter;
 import com.cloud.network.nicira.LogicalRouterPort;
-import com.cloud.network.nicira.LogicalSwitch;
 import com.cloud.network.nicira.LogicalSwitchPort;
 import com.cloud.network.nicira.Match;
 import com.cloud.network.nicira.NatRule;
@@ -76,21 +73,22 @@ import com.cloud.network.nicira.PatchAttachment;
 import com.cloud.network.nicira.RouterNextHop;
 import com.cloud.network.nicira.SingleDefaultRouteImplicitRoutingConfig;
 import com.cloud.network.nicira.SourceNatRule;
-import com.cloud.network.nicira.TransportZoneBinding;
 import com.cloud.network.nicira.VifAttachment;
 import com.cloud.resource.ServerResource;
 
 public class NiciraNvpResource implements ServerResource {
-    private static final int NAME_MAX_LEN = 40;
 
     private static final Logger s_logger = Logger.getLogger(NiciraNvpResource.class);
 
+    public static final int NAME_MAX_LEN = 40;
+
     private String name;
     private String guid;
     private String zoneId;
     private int numRetries;
 
     private NiciraNvpApi niciraNvpApi;
+    private NiciraNvpUtilities niciraNvpUtilities;
 
     protected NiciraNvpApi createNiciraNvpApi() {
         return new NiciraNvpApi();
@@ -131,6 +129,8 @@ public class NiciraNvpResource implements ServerResource {
             throw new ConfigurationException("Unable to find admin password");
         }
 
+        niciraNvpUtilities = NiciraNvpUtilities.getInstance();
+
         niciraNvpApi = createNiciraNvpApi();
         niciraNvpApi.setControllerAddress(ip);
         niciraNvpApi.setAdminCredentials(adminuser, adminpass);
@@ -138,6 +138,18 @@ public class NiciraNvpResource implements ServerResource {
         return true;
     }
 
+    public NiciraNvpApi getNiciraNvpApi() {
+        return niciraNvpApi;
+    }
+
+    public NiciraNvpUtilities getNiciraNvpUtilities() {
+        return niciraNvpUtilities;
+    }
+
+    public int getNumRetries() {
+        return numRetries;
+    }
+
     @Override
     public boolean start() {
         return true;
@@ -202,9 +214,7 @@ public class NiciraNvpResource implements ServerResource {
             // [TODO] Remove when all the commands are refactored.
         }
 
-        if (cmd instanceof CreateLogicalSwitchCommand) {
-            return executeRequest((CreateLogicalSwitchCommand)cmd, numRetries);
-        } else if (cmd instanceof DeleteLogicalSwitchCommand) {
+        if (cmd instanceof DeleteLogicalSwitchCommand) {
             return executeRequest((DeleteLogicalSwitchCommand)cmd, numRetries);
         } else if (cmd instanceof CreateLogicalSwitchPortCommand) {
             return executeRequest((CreateLogicalSwitchPortCommand)cmd, numRetries);
@@ -242,34 +252,6 @@ public class NiciraNvpResource implements ServerResource {
     public void setAgentControl(final IAgentControl agentControl) {
     }
 
-    private Answer executeRequest(final CreateLogicalSwitchCommand cmd, int numRetries) {
-        LogicalSwitch logicalSwitch = new LogicalSwitch();
-        logicalSwitch.setDisplayName(truncate("lswitch-" + cmd.getName(), NAME_MAX_LEN));
-        logicalSwitch.setPortIsolationEnabled(false);
-
-        // Set transport binding
-        final List<TransportZoneBinding> ltzb = new ArrayList<TransportZoneBinding>();
-        ltzb.add(new TransportZoneBinding(cmd.getTransportUuid(), cmd.getTransportType()));
-        logicalSwitch.setTransportZones(ltzb);
-
-        // Tags set to scope cs_account and account name
-        final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
-        tags.add(new NiciraNvpTag("cs_account", cmd.getOwnerName()));
-        logicalSwitch.setTags(tags);
-
-        try {
-            logicalSwitch = niciraNvpApi.createLogicalSwitch(logicalSwitch);
-            return new CreateLogicalSwitchAnswer(cmd, true, "Logicalswitch " + logicalSwitch.getUuid()
+ " created", logicalSwitch.getUuid());
-        } catch (final NiciraNvpApiException e) {
-            if (numRetries > 0) {
-                return retry(cmd, --numRetries);
-            } else {
-                return new CreateLogicalSwitchAnswer(cmd, e);
-            }
-        }
-
-    }
-
     private Answer executeRequest(final DeleteLogicalSwitchCommand cmd, int numRetries) {
         try {
             niciraNvpApi.deleteLogicalSwitch(cmd.getLogicalSwitchUuid());
@@ -646,7 +628,7 @@ public class NiciraNvpResource implements ServerResource {
 
     }
 
-    private Answer retry(final Command cmd, final int numRetries) {
+    public Answer retry(final Command cmd, final int numRetries) {
         s_logger.warn("Retrying " + cmd.getClass().getSimpleName() + ". Number of retries
remaining: " + numRetries);
         return executeRequest(cmd, numRetries);
     }
@@ -687,7 +669,7 @@ public class NiciraNvpResource implements ServerResource {
         return natRuleStr.toString();
     }
 
-    private String truncate(final String string, final int length) {
+    public String truncate(final String string, final int length) {
         if (string.length() <= length) {
             return string;
         } else {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/27c9651b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpUtilities.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpUtilities.java
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpUtilities.java
new file mode 100644
index 0000000..69cacab
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/NiciraNvpUtilities.java
@@ -0,0 +1,42 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+package com.cloud.network.resource;
+
+import com.cloud.network.nicira.LogicalSwitch;
+
+public class NiciraNvpUtilities {
+
+    private static NiciraNvpUtilities instance;
+
+    static {
+        instance = new NiciraNvpUtilities();
+    }
+
+    private NiciraNvpUtilities() {
+    }
+
+    public static NiciraNvpUtilities getInstance() {
+        return instance;
+    }
+
+    public LogicalSwitch createLogicalSwitch() {
+        final LogicalSwitch logicalSwitch = new LogicalSwitch();
+        return logicalSwitch;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/27c9651b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java
new file mode 100644
index 0000000..6fc6cf0
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java
@@ -0,0 +1,73 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.resource.wrapper;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.CreateLogicalSwitchAnswer;
+import com.cloud.agent.api.CreateLogicalSwitchCommand;
+import com.cloud.network.nicira.LogicalSwitch;
+import com.cloud.network.nicira.NiciraNvpApi;
+import com.cloud.network.nicira.NiciraNvpApiException;
+import com.cloud.network.nicira.NiciraNvpTag;
+import com.cloud.network.nicira.TransportZoneBinding;
+import com.cloud.network.resource.NiciraNvpResource;
+import com.cloud.network.resource.NiciraNvpUtilities;
+import com.cloud.resource.CommandWrapper;
+import com.cloud.resource.ResourceWrapper;
+
+@ResourceWrapper(handles =  CreateLogicalSwitchCommand.class)
+public final class NiciraNvpCreateLogicalSwitchCommandWrapper extends CommandWrapper<CreateLogicalSwitchCommand,
Answer, NiciraNvpResource> {
+
+    @Override
+    public Answer execute(final CreateLogicalSwitchCommand command, final NiciraNvpResource
niciraNvpResource) {
+        final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities();
+
+        LogicalSwitch logicalSwitch = niciraNvpUtilities.createLogicalSwitch();
+        logicalSwitch.setDisplayName(niciraNvpResource.truncate("lswitch-" + command.getName(),
NiciraNvpResource.NAME_MAX_LEN));
+        logicalSwitch.setPortIsolationEnabled(false);
+
+        // Set transport binding
+        final List<TransportZoneBinding> ltzb = new ArrayList<TransportZoneBinding>();
+        ltzb.add(new TransportZoneBinding(command.getTransportUuid(), command.getTransportType()));
+        logicalSwitch.setTransportZones(ltzb);
+
+        // Tags set to scope cs_account and account name
+        final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
+        tags.add(new NiciraNvpTag("cs_account", command.getOwnerName()));
+        logicalSwitch.setTags(tags);
+
+        try {
+            final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
+            logicalSwitch = niciraNvpApi.createLogicalSwitch(logicalSwitch);
+            final String switchUuid = logicalSwitch.getUuid();
+            return new CreateLogicalSwitchAnswer(command, true, "Logicalswitch " + switchUuid
+ " created", switchUuid);
+        } catch (final NiciraNvpApiException e) {
+            int numRetries = niciraNvpResource.getNumRetries();
+            if (numRetries > 0) {
+                return niciraNvpResource.retry(command, --numRetries);
+            } else {
+                return new CreateLogicalSwitchAnswer(command, e);
+            }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/27c9651b/plugins/network-elements/nicira-nvp/test/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/test/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java
b/plugins/network-elements/nicira-nvp/test/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java
index ba22d18..a7349ad 100644
--- a/plugins/network-elements/nicira-nvp/test/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java
+++ b/plugins/network-elements/nicira-nvp/test/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java
@@ -21,14 +21,20 @@ package com.cloud.network.resource;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.when;
 
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 
 import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.CreateLogicalSwitchCommand;
 import com.cloud.agent.api.MaintainCommand;
 import com.cloud.agent.api.ReadyCommand;
+import com.cloud.network.nicira.LogicalSwitch;
+import com.cloud.network.nicira.NiciraNvpApi;
+import com.cloud.network.nicira.NiciraNvpApiException;
 
 public class NiciraNvpRequestWrapperTest {
 
@@ -58,4 +64,39 @@ public class NiciraNvpRequestWrapperTest {
 
         assertTrue(answer.getResult());
     }
+
+    @Test
+    public void testCreateLogicalSwitchCommandWrapper() {
+        final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class);
+        final NiciraNvpUtilities niciraNvpUtilities = Mockito.mock(NiciraNvpUtilities.class);
+        final LogicalSwitch logicalSwitch = Mockito.mock(LogicalSwitch.class);
+
+        final String transportUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345";
+        final String transportType = "stt";
+        final String name = "logicalswitch";
+        final String ownerName = "owner";
+
+        final CreateLogicalSwitchCommand command = new CreateLogicalSwitchCommand(transportUuid,
transportType, name, ownerName);
+
+        final String truncated = "lswitch-" + command.getName();
+
+        when(niciraNvpResource.getNiciraNvpUtilities()).thenReturn(niciraNvpUtilities);
+        when(niciraNvpUtilities.createLogicalSwitch()).thenReturn(logicalSwitch);
+        when(niciraNvpResource.truncate("lswitch-" + command.getName(), NiciraNvpResource.NAME_MAX_LEN)).thenReturn(truncated);
+        when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi);
+
+        try {
+            when(niciraNvpApi.createLogicalSwitch(logicalSwitch)).thenReturn(logicalSwitch);
+            when(logicalSwitch.getUuid()).thenReturn(transportUuid);
+        } catch (final NiciraNvpApiException e) {
+            fail(e.getMessage());
+        }
+
+        final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance();
+        assertNotNull(wrapper);
+
+        final Answer answer = wrapper.execute(command, niciraNvpResource);
+
+        assertTrue(answer.getResult());
+    }
 }
\ No newline at end of file


Mime
View raw message