Updated Branches:
  refs/heads/master 0d7601d2d -> e2b814d04

If there already is an entry in the nic mapping table for a starting VM,
try to reconcile with the Nicira Controller.


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

Branch: refs/heads/master
Commit: e2b814d045d10c1efe1ac405603b94cd05c68383
Parents: 0d7601d
Author: Hugo Trippaers <[email protected]>
Authored: Mon Aug 27 11:30:20 2012 +0200
Committer: Hugo Trippaers <[email protected]>
Committed: Mon Aug 27 13:41:27 2012 +0200

----------------------------------------------------------------------
 .../agent/api/FindLogicalSwitchPortAnswer.java     |   36 ++++++++
 .../agent/api/FindLogicalSwitchPortCommand.java    |   44 ++++++++++
 .../agent/api/UpdateLogicalSwitchPortAnswer.java   |   36 ++++++++
 .../agent/api/UpdateLogicalSwitchPortCommand.java  |   65 +++++++++++++++
 .../cloud/network/element/NiciraNvpElement.java    |   26 ++++++-
 .../src/com/cloud/network/nicira/NiciraNvpApi.java |   16 ++++
 .../cloud/network/resource/NiciraNvpResource.java  |   56 +++++++++++++
 7 files changed, 278 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortAnswer.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortAnswer.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortAnswer.java
new file mode 100644
index 0000000..f54bd85
--- /dev/null
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortAnswer.java
@@ -0,0 +1,36 @@
+// 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.agent.api;
+
+public class FindLogicalSwitchPortAnswer extends Answer {
+    private String _logicalSwitchPortUuid;
+
+    public FindLogicalSwitchPortAnswer(Command command, boolean success,
+            String details, String localSwitchPortUuid) {
+        super(command, success, details);
+        this._logicalSwitchPortUuid = localSwitchPortUuid;
+    }
+    
+    public String getLogicalSwitchPortUuid() {
+        return _logicalSwitchPortUuid;
+    }
+
+    public FindLogicalSwitchPortAnswer(Command command, Exception e) {
+        super(command, e);
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortCommand.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortCommand.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortCommand.java
new file mode 100644
index 0000000..cccce67
--- /dev/null
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/FindLogicalSwitchPortCommand.java
@@ -0,0 +1,44 @@
+// 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.agent.api;
+
+public class FindLogicalSwitchPortCommand extends Command {
+       private String _logicalSwitchUuid;
+    private String _logicalSwitchPortUuid;
+    
+    public FindLogicalSwitchPortCommand(String logicalSwitchUuid, String 
logicalSwitchPortUuid) {
+       this._logicalSwitchUuid = logicalSwitchUuid;
+        this._logicalSwitchPortUuid = logicalSwitchPortUuid;
+    }
+    
+    
+    public String getLogicalSwitchUuid() {
+        return _logicalSwitchUuid;
+    }
+
+    
+    public String getLogicalSwitchPortUuid() {
+        return _logicalSwitchPortUuid;
+    }
+
+    
+    @Override
+    public boolean executeInSequence() {
+        return false;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortAnswer.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortAnswer.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortAnswer.java
new file mode 100644
index 0000000..3b7fbf7
--- /dev/null
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortAnswer.java
@@ -0,0 +1,36 @@
+// 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.agent.api;
+
+public class UpdateLogicalSwitchPortAnswer extends Answer {
+    private String _logicalSwitchPortUuid;
+
+    public UpdateLogicalSwitchPortAnswer(Command command, boolean success,
+            String details, String localSwitchPortUuid) {
+        super(command, success, details);
+        this._logicalSwitchPortUuid = localSwitchPortUuid;
+    }
+    
+    public String getLogicalSwitchPortUuid() {
+        return _logicalSwitchPortUuid;
+    }
+
+    public UpdateLogicalSwitchPortAnswer(Command command, Exception e) {
+        super(command, e);
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortCommand.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortCommand.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortCommand.java
new file mode 100644
index 0000000..83ae231
--- /dev/null
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/UpdateLogicalSwitchPortCommand.java
@@ -0,0 +1,65 @@
+// 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.agent.api;
+
+public class UpdateLogicalSwitchPortCommand extends Command {
+    private String _logicalSwitchUuid;
+    private String _logicalSwitchPortUuid;
+    private String _attachmentUuid;
+    private String _ownerName;
+    private String _nicName;
+    
+    public UpdateLogicalSwitchPortCommand(String logicalSwitchPortUuid, String 
logicalSwitchUuid, String attachmentUuid, String ownerName, String nicName) {
+        this._logicalSwitchUuid = logicalSwitchUuid;
+        this._logicalSwitchPortUuid = logicalSwitchPortUuid;
+        this._attachmentUuid = attachmentUuid;
+        this._ownerName = ownerName;
+        this._nicName = nicName;
+    }
+    
+    
+    public String getLogicalSwitchUuid() {
+        return _logicalSwitchUuid;
+    }
+
+
+    public String getLogicalSwitchPortUuid() {
+        return _logicalSwitchPortUuid;
+    }
+
+
+    public String getAttachmentUuid() {
+        return _attachmentUuid;
+    }
+
+
+    public String getOwnerName() {
+        return _ownerName;
+    }
+
+
+    public String getNicName() {
+        return _nicName;
+    }
+
+
+    @Override
+    public boolean executeInSequence() {
+        return false;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java
index bff69aa..1fcccdb 100644
--- 
a/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java
@@ -51,8 +51,12 @@ import com.cloud.agent.api.CreateLogicalSwitchPortAnswer;
 import com.cloud.agent.api.CreateLogicalSwitchPortCommand;
 import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer;
 import com.cloud.agent.api.DeleteLogicalSwitchPortCommand;
+import com.cloud.agent.api.FindLogicalSwitchPortAnswer;
+import com.cloud.agent.api.FindLogicalSwitchPortCommand;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.agent.api.StartupNiciraNvpCommand;
+import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer;
+import com.cloud.agent.api.UpdateLogicalSwitchPortCommand;
 import com.cloud.api.commands.AddNiciraNvpDeviceCmd;
 import com.cloud.api.commands.DeleteNiciraNvpDeviceCmd;
 import com.cloud.api.commands.ListNiciraNvpDeviceNetworksCmd;
@@ -198,6 +202,26 @@ public class NiciraNvpElement extends AdapterBase 
implements ConnectivityProvide
         }
         NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
         HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId());
+
+        NiciraNvpNicMappingVO existingNicMap = 
_niciraNvpNicMappingDao.findByNicUuid(nicVO.getUuid());
+        if (existingNicMap != null) {
+            FindLogicalSwitchPortCommand findCmd = new 
FindLogicalSwitchPortCommand(existingNicMap.getLogicalSwitchUuid(), 
+                       existingNicMap.getLogicalSwitchPortUuid());
+            FindLogicalSwitchPortAnswer answer = (FindLogicalSwitchPortAnswer) 
_agentMgr.easySend(niciraNvpHost.getId(), findCmd);
+            
+            if (answer.getResult()) {
+                   s_logger.warn("Existing Logical Switchport found for nic " 
+ nic.getName() + " with uuid " + existingNicMap.getLogicalSwitchPortUuid());
+                   UpdateLogicalSwitchPortCommand cmd = new 
UpdateLogicalSwitchPortCommand(existingNicMap.getLogicalSwitchPortUuid(), 
+                               
network.getBroadcastUri().getSchemeSpecificPart(), nicVO.getUuid(), 
+                           context.getDomain().getName() + "-" + 
context.getAccount().getAccountName(), nic.getName());
+                   _agentMgr.easySend(niciraNvpHost.getId(), cmd);
+                   return true;
+            }
+            else {
+                   s_logger.error("Stale entry found for nic " + nic.getName() 
+ " with logical switchport uuid " + existingNicMap.getLogicalSwitchPortUuid());
+                   _niciraNvpNicMappingDao.remove(existingNicMap.getId());
+            }
+        }
         
         CreateLogicalSwitchPortCommand cmd = new 
CreateLogicalSwitchPortCommand(network.getBroadcastUri().getSchemeSpecificPart(),
 nicVO.getUuid(), 
                 context.getDomain().getName() + "-" + 
context.getAccount().getAccountName(), nic.getName());
@@ -244,7 +268,7 @@ public class NiciraNvpElement extends AdapterBase 
implements ConnectivityProvide
             s_logger.error("No mapping for nic " + nic.getName());
             return false;
         }
-        
+                
         DeleteLogicalSwitchPortCommand cmd = new 
DeleteLogicalSwitchPortCommand(nicMap.getLogicalSwitchUuid(), 
nicMap.getLogicalSwitchPortUuid());
         DeleteLogicalSwitchPortAnswer answer = (DeleteLogicalSwitchPortAnswer) 
_agentMgr.easySend(niciraNvpHost.getId(), cmd);
         

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
----------------------------------------------------------------------
diff --git 
a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
 
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
index 264b24a..2c002ab 100644
--- 
a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
+++ 
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
@@ -195,6 +195,22 @@ public class NiciraNvpApi {
         return ccs;
     }
 
+    public NiciraNvpList<LogicalSwitchPort> 
findLogicalSwitchPortsByUuid(String logicalSwitchUuid, String 
logicalSwitchPortUuid) throws NiciraNvpApiException {
+        String uri = "/ws.v1/lswitch/" + logicalSwitchUuid + "/lport";
+        Map<String,String> params = new HashMap<String,String>();
+        params.put("uuid", logicalSwitchPortUuid);
+        params.put("fields", "uuid");
+            
+        NiciraNvpList<LogicalSwitchPort> lspl = executeRetrieveObject(new 
TypeToken<NiciraNvpList<LogicalSwitchPort>>(){}.getType(), uri, params);
+                
+        if (lspl == null ) {
+            throw new NiciraNvpApiException("Unexpected response from API");
+        }
+        
+        return lspl;
+    }
+    
+    
     private <T> void executeUpdateObject(T newObject, String uri, 
Map<String,String> parameters) throws NiciraNvpApiException {
         String url;
         try {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e2b814d0/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 f0f1a0c..31cac6f 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
@@ -35,6 +35,8 @@ import com.cloud.agent.api.DeleteLogicalSwitchAnswer;
 import com.cloud.agent.api.DeleteLogicalSwitchCommand;
 import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer;
 import com.cloud.agent.api.DeleteLogicalSwitchPortCommand;
+import com.cloud.agent.api.FindLogicalSwitchPortAnswer;
+import com.cloud.agent.api.FindLogicalSwitchPortCommand;
 import com.cloud.agent.api.MaintainAnswer;
 import com.cloud.agent.api.MaintainCommand;
 import com.cloud.agent.api.PingCommand;
@@ -42,6 +44,8 @@ import com.cloud.agent.api.ReadyAnswer;
 import com.cloud.agent.api.ReadyCommand;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.agent.api.StartupNiciraNvpCommand;
+import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer;
+import com.cloud.agent.api.UpdateLogicalSwitchPortCommand;
 import com.cloud.host.Host;
 import com.cloud.host.Host.Type;
 import com.cloud.network.nicira.ControlClusterStatus;
@@ -49,6 +53,7 @@ import com.cloud.network.nicira.LogicalSwitch;
 import com.cloud.network.nicira.LogicalSwitchPort;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
+import com.cloud.network.nicira.NiciraNvpList;
 import com.cloud.network.nicira.NiciraNvpTag;
 import com.cloud.network.nicira.TransportZoneBinding;
 import com.cloud.network.nicira.VifAttachment;
@@ -186,6 +191,12 @@ public class NiciraNvpResource implements ServerResource {
         else if (cmd instanceof DeleteLogicalSwitchPortCommand) {
             return executeRequest((DeleteLogicalSwitchPortCommand) cmd, 
numRetries);
         }
+        else if (cmd instanceof UpdateLogicalSwitchPortCommand) {
+               return executeRequest((UpdateLogicalSwitchPortCommand) cmd, 
numRetries);
+        }
+        else if (cmd instanceof FindLogicalSwitchPortCommand) {
+               return executeRequest((FindLogicalSwitchPortCommand) cmd, 
numRetries);
+        }
         s_logger.debug("Received unsupported command " + cmd.toString());
         return Answer.createUnsupportedCommandAnswer(cmd);
     }
@@ -284,6 +295,51 @@ public class NiciraNvpResource implements ServerResource {
         }
     }
 
+    private Answer executeRequest(UpdateLogicalSwitchPortCommand cmd, int 
numRetries) {
+        String logicalSwitchUuid = cmd.getLogicalSwitchUuid();
+        String logicalSwitchPortUuid = cmd.getLogicalSwitchPortUuid();
+        String attachmentUuid = cmd.getAttachmentUuid();
+        
+        try {
+            // Tags set to scope cs_account and account name
+            List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
+            tags.add(new NiciraNvpTag("cs_account",cmd.getOwnerName()));
+
+            _niciraNvpApi.modifyLogicalSwitchPortAttachment(logicalSwitchUuid, 
logicalSwitchPortUuid, new VifAttachment(attachmentUuid));
+            return new UpdateLogicalSwitchPortAnswer(cmd, true, "Attachment 
for  " + logicalSwitchPortUuid + " updated", logicalSwitchPortUuid);
+        } catch (NiciraNvpApiException e) {
+               if (numRetries > 0) {
+                       return retry(cmd, --numRetries);
+               } 
+               else {
+                       return new UpdateLogicalSwitchPortAnswer(cmd, e);
+               }
+        }
+       
+    }
+    
+    private Answer executeRequest(FindLogicalSwitchPortCommand cmd, int 
numRetries) {
+       String logicalSwitchUuid = cmd.getLogicalSwitchUuid();
+        String logicalSwitchPortUuid = cmd.getLogicalSwitchPortUuid();
+        
+        try {
+               NiciraNvpList<LogicalSwitchPort> ports = 
_niciraNvpApi.findLogicalSwitchPortsByUuid(logicalSwitchUuid, 
logicalSwitchPortUuid);
+               if (ports.getResult_count() == 0) {
+                       return new FindLogicalSwitchPortAnswer(cmd, false, 
"Logical switchport " + logicalSwitchPortUuid + " not found", null);
+               }
+               else {
+                       return new FindLogicalSwitchPortAnswer(cmd, true, 
"Logical switchport " + logicalSwitchPortUuid + " found", 
logicalSwitchPortUuid);
+               }
+        } catch (NiciraNvpApiException e) {
+               if (numRetries > 0) {
+                       return retry(cmd, --numRetries);
+               } 
+               else {
+                       return new FindLogicalSwitchPortAnswer(cmd, e);
+               }
+        }      
+    }
+    
     private Answer executeRequest(ReadyCommand cmd) {
         return new ReadyAnswer(cmd);
     }

Reply via email to