You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by hu...@apache.org on 2012/10/12 14:15:51 UTC

[9/16] git commit: Wire the PortForwarding and StaticNat calls

Wire the PortForwarding and StaticNat calls

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

Branch: refs/heads/master
Commit: c5634b96873ec69c43c210ed172855c9d9b2ff7d
Parents: d4d82fa
Author: Hugo Trippaers <ht...@schubergphilis.com>
Authored: Sun Sep 30 10:20:54 2012 -0700
Committer: Hugo Trippaers <ht...@schubergphilis.com>
Committed: Sun Sep 30 10:20:54 2012 -0700

----------------------------------------------------------------------
 .../agent/api/AssignIpToLogicalRouterAnswer.java   |   35 -----
 .../agent/api/AssignIpToLogicalRouterCommand.java  |   98 --------------
 ...rePortForwardingRulesOnLogicalRouterAnswer.java |   34 +++++
 ...ePortForwardingRulesOnLogicalRouterCommand.java |   61 +++++++++
 ...nfigureStaticNatRulesOnLogicalRouterAnswer.java |   43 ++++++
 ...figureStaticNatRulesOnLogicalRouterCommand.java |   64 +++++++++
 .../cloud/agent/api/CreateLogicalRouterAnswer.java |    2 +-
 .../cloud/network/element/NiciraNvpElement.java    |  102 ++++++++++++--
 .../cloud/network/nicira/DestinationNatRule.java   |   49 +-------
 .../src/com/cloud/network/nicira/NatRule.java      |   89 +++++++++++++-
 .../src/com/cloud/network/nicira/NiciraNvpApi.java |    6 +
 .../com/cloud/network/nicira/SourceNatRule.java    |   58 +--------
 .../cloud/network/resource/NiciraNvpResource.java  |   95 ++++++--------
 13 files changed, 432 insertions(+), 304 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java
deleted file mode 100644
index 1eda213..0000000
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java
+++ /dev/null
@@ -1,35 +0,0 @@
-// 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;
-
-import com.cloud.agent.api.Command;
-
-/**
- * 
- */
-public class AssignIpToLogicalRouterAnswer extends Answer {
-	
-    public AssignIpToLogicalRouterAnswer(Command command, boolean success,
-            String details) {
-        super(command, success, details);
-    }
-    
-    public AssignIpToLogicalRouterAnswer(Command command, Exception e) {
-        super(command, e);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java
deleted file mode 100644
index 98c36a8..0000000
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java
+++ /dev/null
@@ -1,98 +0,0 @@
-// 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;
-
-import com.cloud.network.IpAddress;
-
-/**
- * 
- */
-public class AssignIpToLogicalRouterCommand extends Command {
-	private String logicalRouterUuid;
-	private String gatewayServiceUuid;
-	private String publicIpCidr;
-	private long publicIpVlan;
-	private boolean sourceNat;
-	private String internalNetworkCidr;
-
-	public AssignIpToLogicalRouterCommand(String logicalRouterUuid, String gatewayServiceUuid, String publicIpCidr, long publicIpVlan, boolean sourceNat, String internetNetworkCidr) {
-		this.logicalRouterUuid = logicalRouterUuid;
-		this.gatewayServiceUuid = gatewayServiceUuid;
-		this.publicIpCidr = publicIpCidr;
-		this.sourceNat = sourceNat;
-		this.internalNetworkCidr = internetNetworkCidr;
-		this.publicIpVlan = publicIpVlan;
-	}
-	
-	public String getLogicalRouterUuid() {
-		return logicalRouterUuid;
-	}
-
-	public void setLogicalRouterUuid(String logicalRouterUuid) {
-		this.logicalRouterUuid = logicalRouterUuid;
-	}
-
-	public String getGatewayServiceUuid() {
-		return gatewayServiceUuid;
-	}
-
-	public void setGatewayServiceUuid(String gatewayServiceUuid) {
-		this.gatewayServiceUuid = gatewayServiceUuid;
-	}
-
-	public String getPublicIpCidr() {
-		return publicIpCidr;
-	}
-
-	public void setPublicIpCidr(String publicIpCidr) {
-		this.publicIpCidr = publicIpCidr;
-	}
-
-	public long getPublicIpVlan() {
-		return publicIpVlan;
-	}
-
-	public void setPublicIpVlan(long publicIpVlan) {
-		this.publicIpVlan = publicIpVlan;
-	}
-
-	public boolean isSourceNat() {
-		return sourceNat;
-	}
-
-	public void setSourceNat(boolean sourceNat) {
-		this.sourceNat = sourceNat;
-	}
-
-	public String getInternalNetworkCidr() {
-		return internalNetworkCidr;
-	}
-
-	public void setInternalNetworkCidr(String internalNetworkCidr) {
-		this.internalNetworkCidr = internalNetworkCidr;
-	}
-
-	/* (non-Javadoc)
-	 * @see com.cloud.agent.api.Command#executeInSequence()
-	 */
-	@Override
-	public boolean executeInSequence() {
-		// TODO Auto-generated method stub
-		return false;
-	}
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java
new file mode 100644
index 0000000..40bde6c
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java
@@ -0,0 +1,34 @@
+// 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 ConfigurePortForwardingRulesOnLogicalRouterAnswer extends Answer {
+
+	public ConfigurePortForwardingRulesOnLogicalRouterAnswer(Command command,
+			boolean success, String details) {
+		super(command, success, details);
+	}
+
+	public ConfigurePortForwardingRulesOnLogicalRouterAnswer(Command command,
+			Exception e) {
+		super(command, e);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterCommand.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterCommand.java
new file mode 100644
index 0000000..1d57e35
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterCommand.java
@@ -0,0 +1,61 @@
+// 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;
+
+import java.util.List;
+
+import com.cloud.agent.api.to.PortForwardingRuleTO;
+
+/**
+ * 
+ */
+public class ConfigurePortForwardingRulesOnLogicalRouterCommand extends Command {
+	
+	private String logicalRouterUuid;
+	private List<PortForwardingRuleTO> rules;
+
+	public ConfigurePortForwardingRulesOnLogicalRouterCommand(String logicalRouterUuid, List<PortForwardingRuleTO> rules) {
+		this.logicalRouterUuid = logicalRouterUuid;
+		this.rules = rules;
+	}
+	
+	public String getLogicalRouterUuid() {
+		return logicalRouterUuid;
+	}
+
+	public void setLogicalRouterUuid(String logicalRouterUuid) {
+		this.logicalRouterUuid = logicalRouterUuid;
+	}
+
+	public List<PortForwardingRuleTO> getRules() {
+		return rules;
+	}
+
+	public void setRules(List<PortForwardingRuleTO> rules) {
+		this.rules = rules;
+	}
+
+	/* (non-Javadoc)
+	 * @see com.cloud.agent.api.Command#executeInSequence()
+	 */
+	@Override
+	public boolean executeInSequence() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterAnswer.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterAnswer.java
new file mode 100644
index 0000000..463dd46
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterAnswer.java
@@ -0,0 +1,43 @@
+// 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 ConfigureStaticNatRulesOnLogicalRouterAnswer extends Answer {
+
+	/**
+	 * @param command
+	 * @param success
+	 * @param details
+	 */
+	public ConfigureStaticNatRulesOnLogicalRouterAnswer(Command command,
+			boolean success, String details) {
+		super(command, success, details);
+	}
+
+	/**
+	 * @param command
+	 * @param e
+	 */
+	public ConfigureStaticNatRulesOnLogicalRouterAnswer(Command command,
+			Exception e) {
+		super(command, e);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterCommand.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterCommand.java
new file mode 100644
index 0000000..4e5d37c
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterCommand.java
@@ -0,0 +1,64 @@
+// 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;
+
+import java.util.List;
+
+import com.cloud.agent.api.to.StaticNatRuleTO;
+
+/**
+ * 
+ */
+public class ConfigureStaticNatRulesOnLogicalRouterCommand extends Command {
+	
+	private String logicalRouterUuid;
+	private List<StaticNatRuleTO> rules;
+
+	public ConfigureStaticNatRulesOnLogicalRouterCommand(
+			String logicalRouterUuid, List<StaticNatRuleTO> rules) {
+		super();
+		this.logicalRouterUuid = logicalRouterUuid;
+		this.rules = rules;
+
+	}
+
+	public String getLogicalRouterUuid() {
+		return logicalRouterUuid;
+	}
+
+	public void setLogicalRouterUuid(String logicalRouterUuid) {
+		this.logicalRouterUuid = logicalRouterUuid;
+	}
+
+	public List<StaticNatRuleTO> getRules() {
+		return rules;
+	}
+
+	public void setRules(List<StaticNatRuleTO> rules) {
+		this.rules = rules;
+	}
+
+	/* (non-Javadoc)
+	 * @see com.cloud.agent.api.Command#executeInSequence()
+	 */
+	@Override
+	public boolean executeInSequence() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java
index 3337e92..4a09e44 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java
@@ -33,7 +33,7 @@ public class CreateLogicalRouterAnswer extends Answer {
         super(command, e);
     }
 
-    public String getLogicalSwitchUuid() {
+    public String getLogicalRouterUuid() {
         return _logicalRouterUuid;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/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 7a0b268..716cd78 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
@@ -47,6 +47,10 @@ import javax.naming.ConfigurationException;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer;
+import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand;
+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.CreateLogicalSwitchPortAnswer;
@@ -61,6 +65,8 @@ 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.agent.api.to.PortForwardingRuleTO;
+import com.cloud.agent.api.to.StaticNatRuleTO;
 import com.cloud.api.commands.AddNiciraNvpDeviceCmd;
 import com.cloud.api.commands.DeleteNiciraNvpDeviceCmd;
 import com.cloud.api.commands.ListNiciraNvpDeviceNetworksCmd;
@@ -87,6 +93,7 @@ import com.cloud.network.Network.Service;
 import com.cloud.network.NetworkVO;
 import com.cloud.network.Networks;
 import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.IpAddress;
 import com.cloud.network.NetworkManager;
 import com.cloud.network.NiciraNvpDeviceVO;
 import com.cloud.network.NiciraNvpNicMappingVO;
@@ -105,8 +112,11 @@ import com.cloud.network.dao.PhysicalNetworkServiceProviderDao;
 import com.cloud.network.dao.PhysicalNetworkServiceProviderVO;
 import com.cloud.network.guru.NiciraNvpGuestNetworkGuru;
 import com.cloud.network.resource.NiciraNvpResource;
+import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.PortForwardingRule;
 import com.cloud.network.rules.StaticNat;
+import com.cloud.network.rules.StaticNatRule;
+import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.resource.ResourceManager;
 import com.cloud.resource.ResourceState;
@@ -291,7 +301,7 @@ public class NiciraNvpElement extends AdapterBase implements
 				
 				// Store the uuid so we can easily find it during cleanup
 				NiciraNvpRouterMappingVO routermapping = 
-						new NiciraNvpRouterMappingVO(cmd.getLogicalSwitchUuid(), network.getId());
+						new NiciraNvpRouterMappingVO(answer.getLogicalRouterUuid(), network.getId());
 				_niciraNvpRouterMappingDao.persist(routermapping);
 			}
 		} finally {
@@ -518,8 +528,8 @@ public class NiciraNvpElement extends AdapterBase implements
 			s_logger.warn("Unable to provide services without Connectivity service enabled for this element");
 			return false;
 		}
-		if ((services.contains(Service.PortForwarding) || services.contains(Service.StaticNat)) && !services.contains(Service.PortForwarding)) {
-			s_logger.warn("Unable to provider StaticNat and/or PortForwarding without the SourceNat service");
+		if ((services.contains(Service.PortForwarding) || services.contains(Service.StaticNat)) && !services.contains(Service.SourceNat)) {
+			s_logger.warn("Unable to provide StaticNat and/or PortForwarding without the SourceNat service");
 			return false;
 		}
 		return true;
@@ -833,15 +843,49 @@ public class NiciraNvpElement extends AdapterBase implements
 	 * From interface StaticNatServiceProvider
 	 */
 	@Override
-	public boolean applyStaticNats(Network config,
+	public boolean applyStaticNats(Network network,
 			List<? extends StaticNat> rules)
 			throws ResourceUnavailableException {
-		// FIXME Implement this
-		s_logger.debug("Entering applyStaticNats"); // TODO Remove this line
-		for (StaticNat rule : rules) {
-			s_logger.debug ("StaticNat rule : from " + rule.getSourceIpAddressId() + " to " + rule.getDestIpAddress() + (rule.isForRevoke() ? " for revoke" : ""));
+        if (!canHandle(network, Service.StaticNat)) {
+            return false;
+        }
+
+		List<NiciraNvpDeviceVO> devices = _niciraNvpDao
+				.listByPhysicalNetwork(network.getPhysicalNetworkId());
+		if (devices.isEmpty()) {
+			s_logger.error("No NiciraNvp Controller on physical network "
+					+ network.getPhysicalNetworkId());
+			return false;
 		}
-		return true;
+		NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
+		HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId());
+        	
+		NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao
+				.findByNetworkIdI(network.getId());
+		if (routermapping == null) {
+			s_logger.error("No logical router uuid found for network "
+					+ network.getDisplayText());
+			return false;
+		}
+
+		List<StaticNatRuleTO> staticNatRules = new ArrayList<StaticNatRuleTO>(); 
+        for (StaticNat rule : rules) {
+            IpAddress sourceIp = _networkManager.getIp(rule.getSourceIpAddressId());
+            // Force the nat rule into the StaticNatRuleTO, no use making a new TO object
+            // we only need the source and destination ip. Unfortunately no mention if a rule
+            // is new.
+            StaticNatRuleTO ruleTO = new StaticNatRuleTO(1, 
+            		sourceIp.getAddress().addr(), 0, 65535, 
+            		rule.getDestIpAddress(), 0, 65535,
+            		"any", rule.isForRevoke(), false);
+            staticNatRules.add(ruleTO);
+        }
+        
+        ConfigureStaticNatRulesOnLogicalRouterCommand cmd = 
+        		new ConfigureStaticNatRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), staticNatRules);
+        ConfigureStaticNatRulesOnLogicalRouterAnswer answer = (ConfigureStaticNatRulesOnLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd);
+        
+        return answer.getResult();
 	}
 
 	/**
@@ -850,13 +894,41 @@ public class NiciraNvpElement extends AdapterBase implements
 	@Override
 	public boolean applyPFRules(Network network, List<PortForwardingRule> rules)
 			throws ResourceUnavailableException {
-		// FIXME Implement this
-		s_logger.debug("Entering applyPFRules"); // TODO Remove this line
-		for (PortForwardingRule rule : rules) {
-			s_logger.debug ("PortForwardingRule rule : from " + rule.getSourceIpAddressId() + 
-					" to " + rule.getDestinationIpAddress().addr() + " port " + rule.getDestinationPortStart() + "-" + rule.getDestinationPortEnd());
+        if (!canHandle(network, Service.PortForwarding)) {
+            return false;
+        }
+        
+		List<NiciraNvpDeviceVO> devices = _niciraNvpDao
+				.listByPhysicalNetwork(network.getPhysicalNetworkId());
+		if (devices.isEmpty()) {
+			s_logger.error("No NiciraNvp Controller on physical network "
+					+ network.getPhysicalNetworkId());
+			return false;
 		}
-		return false;
+		NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
+		HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId());
+        	
+		NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao
+				.findByNetworkIdI(network.getId());
+		if (routermapping == null) {
+			s_logger.error("No logical router uuid found for network "
+					+ network.getDisplayText());
+			return false;
+		}
+		
+		List<PortForwardingRuleTO> portForwardingRules = new ArrayList<PortForwardingRuleTO>(); 
+        for (PortForwardingRule rule : rules) {
+            IpAddress sourceIp = _networkManager.getIp(rule.getSourceIpAddressId());
+            Vlan vlan = _vlanDao.findById(sourceIp.getVlanId());
+            PortForwardingRuleTO ruleTO = new PortForwardingRuleTO((PortForwardingRule) rule, vlan.getVlanTag(), sourceIp.getAddress().addr());
+            portForwardingRules.add(ruleTO);
+        }
+        
+        ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = 
+        		new ConfigurePortForwardingRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), portForwardingRules);
+        ConfigurePortForwardingRulesOnLogicalRouterAnswer answer = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd);
+        
+        return answer.getResult();
 	}
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java
index 2cbdace..b1ef12f 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java
@@ -1,53 +1,8 @@
 package com.cloud.network.nicira;
 
 public class DestinationNatRule extends NatRule {
-	private Match match;
-	private String to_destination_ip_address_min;
-	private String to_destination_ip_address_max;
-	private Integer to_destination_port;
-	private String uuid;
-	private String type = "DestinationNatRule";
-	
-	public Match getMatch() {
-		return match;
-	}
-	
-	public void setMatch(Match match) {
-		this.match = match;
-	}
-	
-	public String getToDestinationIpAddressMin() {
-		return to_destination_ip_address_min;
-	}
-	
-	public void setToDestinationIpAddressMin(
-			String to_destination_ip_address_min) {
-		this.to_destination_ip_address_min = to_destination_ip_address_min;
-	}
-	
-	public String getToDestinationIpAddressMax() {
-		return to_destination_ip_address_max;
-	}
-	
-	public void setToDestinationIpAddressMax(
-			String to_destination_ip_address_max) {
-		this.to_destination_ip_address_max = to_destination_ip_address_max;
-	}
-	
-	public Integer getToDestinationPort() {
-		return to_destination_port;
-	}
-	
-	public void setToDestinationPort(Integer to_destination_port) {
-		this.to_destination_port = to_destination_port;
-	}
-	
-	public String getUuid() {
-		return uuid;
-	}
-	
-	public void setUuid(String uuid) {
-		this.uuid = uuid;
+	{
+		type = "DestinationNatRule";
 	}
 	
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java
index 30196f0..82a3378 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java
@@ -20,5 +20,92 @@ package com.cloud.network.nicira;
  * 
  */
 public abstract class NatRule {
-
+	protected Match match;
+	protected String to_source_ip_address_min;
+	protected String to_source_ip_address_max;
+	protected Integer to_source_port_min;
+	protected Integer to_source_port_max;
+	protected String uuid;
+	protected String type;
+	protected String to_destination_ip_address_min;
+	protected String to_destination_ip_address_max;
+	protected Integer to_destination_port;
+	
+	public Match getMatch() {
+		return match;
+	}
+	
+	public void setMatch(Match match) {
+		this.match = match;
+	}
+	
+	public String getToSourceIpAddressMin() {
+		return to_source_ip_address_min;
+	}
+	
+	public void setToSourceIpAddressMin(String to_source_ip_address_min) {
+		this.to_source_ip_address_min = to_source_ip_address_min;
+	}
+	
+	public String getToSourceIpAddressMax() {
+		return to_source_ip_address_max;
+	}
+	
+	public void setToSourceIpAddressMax(String to_source_ip_address_max) {
+		this.to_source_ip_address_max = to_source_ip_address_max;
+	}
+	
+	public Integer getToSourcePortMin() {
+		return to_source_port_min;
+	}
+	
+	public void setToSourcePortMin(Integer to_source_port_min) {
+		this.to_source_port_min = to_source_port_min;
+	}
+	
+	public Integer getToSourcePortMax() {
+		return to_source_port_max;
+	}
+	
+	public void setToSourcePortMax(Integer to_source_port_max) {
+		this.to_source_port_max = to_source_port_max;
+	}
+	
+	public String getUuid() {
+		return uuid;
+	}
+	
+	public void setUuid(String uuid) {
+		this.uuid = uuid;
+	}
+	
+	public String getToDestinationIpAddressMin() {
+		return to_destination_ip_address_min;
+	}
+	
+	public void setToDestinationIpAddressMin(
+			String to_destination_ip_address_min) {
+		this.to_destination_ip_address_min = to_destination_ip_address_min;
+	}
+	
+	public String getToDestinationIpAddressMax() {
+		return to_destination_ip_address_max;
+	}
+	
+	public void setToDestinationIpAddressMax(
+			String to_destination_ip_address_max) {
+		this.to_destination_ip_address_max = to_destination_ip_address_max;
+	}
+	
+	public Integer getToDestinationPort() {
+		return to_destination_port;
+	}
+	
+	public void setToDestinationPort(Integer to_destination_port) {
+		this.to_destination_port = to_destination_port;
+	}
+	
+	public String getType() {
+		return type;
+	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/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 a04dd1e..fe4deec 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
@@ -284,6 +284,12 @@ public class NiciraNvpApi {
     	executeUpdateObject(logicalRouterPort, uri, Collections.<String,String>emptyMap());
     }
     
+    public NiciraNvpList<NatRule> findNatRulesByLogicalRouterUuid(String logicalRouterUuid) throws NiciraNvpApiException {
+    	String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat";
+    	
+    	return executeRetrieveObject(new TypeToken<NiciraNvpList<NatRule>>(){}.getType(), uri, Collections.<String,String>emptyMap());
+    }
+    
     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/c5634b96/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java
index 7d1c13d..acbf21e 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java
@@ -20,60 +20,8 @@ package com.cloud.network.nicira;
  * 
  */
 public class SourceNatRule extends NatRule {
-	private Match match;
-	private String to_source_ip_address_min;
-	private String to_source_ip_address_max;
-	private Integer to_source_port_min;
-	private Integer to_source_port_max;
-	private String uuid;
-	private String type = "SourceNatRule";
-	
-	public Match getMatch() {
-		return match;
-	}
-	
-	public void setMatch(Match match) {
-		this.match = match;
-	}
-	
-	public String getToSourceIpAddressMin() {
-		return to_source_ip_address_min;
-	}
-	
-	public void setToSourceIpAddressMin(String to_source_ip_address_min) {
-		this.to_source_ip_address_min = to_source_ip_address_min;
-	}
-	
-	public String getToSourceIpAddressMax() {
-		return to_source_ip_address_max;
-	}
-	
-	public void setToSourceIpAddressMax(String to_source_ip_address_max) {
-		this.to_source_ip_address_max = to_source_ip_address_max;
-	}
-	
-	public Integer getToSourcePortMin() {
-		return to_source_port_min;
-	}
-	
-	public void setToSourcePortMin(Integer to_source_port_min) {
-		this.to_source_port_min = to_source_port_min;
-	}
-	
-	public Integer getToSourcePortMax() {
-		return to_source_port_max;
-	}
-	
-	public void setToSourcePortMax(Integer to_source_port_max) {
-		this.to_source_port_max = to_source_port_max;
-	}
-	
-	public String getUuid() {
-		return uuid;
-	}
-	
-	public void setUuid(String uuid) {
-		this.uuid = uuid;
-	}
+	{
+		type = "SourceNatRule";
+	}	
 	
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c5634b96/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 96ed856..0908101 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
@@ -26,9 +26,11 @@ import org.apache.log4j.Logger;
 
 import com.cloud.agent.IAgentControl;
 import com.cloud.agent.api.Answer;
-import com.cloud.agent.api.AssignIpToLogicalRouterAnswer;
-import com.cloud.agent.api.AssignIpToLogicalRouterCommand;
 import com.cloud.agent.api.Command;
+import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer;
+import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand;
+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;
@@ -52,6 +54,7 @@ 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.agent.api.to.StaticNatRuleTO;
 import com.cloud.host.Host;
 import com.cloud.host.Host.Type;
 import com.cloud.network.nicira.Attachment;
@@ -62,6 +65,7 @@ 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;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
 import com.cloud.network.nicira.NiciraNvpList;
@@ -220,10 +224,12 @@ public class NiciraNvpResource implements ServerResource {
         else if (cmd instanceof DeleteLogicalRouterCommand) {
         	return executeRequest((DeleteLogicalRouterCommand) cmd, numRetries);
         }
-        else if (cmd instanceof AssignIpToLogicalRouterCommand) {
-        	return executeRequest((AssignIpToLogicalRouterCommand) cmd, numRetries);
+        else if (cmd instanceof ConfigureStaticNatRulesOnLogicalRouterCommand) {
+        	return executeRequest((ConfigureStaticNatRulesOnLogicalRouterCommand) cmd, numRetries);
         }
-        s_logger.debug("Received unsupported command " + cmd.toString());
+        else if (cmd instanceof ConfigurePortForwardingRulesOnLogicalRouterCommand) {
+        	return executeRequest((ConfigurePortForwardingRulesOnLogicalRouterCommand) cmd, numRetries);
+        }        s_logger.debug("Received unsupported command " + cmd.toString());
         return Answer.createUnsupportedCommandAnswer(cmd);
     }
 
@@ -475,64 +481,49 @@ public class NiciraNvpResource implements ServerResource {
         }
     }
     
-    private Answer executeRequest(AssignIpToLogicalRouterCommand cmd, int numRetries) {
+    private Answer executeRequest(ConfigureStaticNatRulesOnLogicalRouterCommand cmd, int numRetries) {
     	try {
     		LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid());
+    		NiciraNvpList<NatRule> existingRules = _niciraNvpApi.findNatRulesByLogicalRouterUuid(cmd.getLogicalRouterUuid());
+    		// Rules of the game (also known as assumptions-that-will-make-stuff-break-later-on)
+    		// A SourceNat rule with a match other than a /32 cidr is assumed to be the "main" SourceNat rule
+    		// Any other SourceNat rule should have a corresponding DestinationNat rule
     		
-    		NiciraNvpList<LogicalRouterPort> ports = 
-    				_niciraNvpApi.findLogicalRouterPortByGatewayServiceAndVlanId(cmd.getLogicalRouterUuid(), 
-    						cmd.getGatewayServiceUuid(), cmd.getPublicIpVlan());
-    		
-    		String publicNetworkIpAddress = cmd.getPublicIpCidr();    		
-    		
-    		if (ports.isEmpty()) {
-    			// No attachment on this network, we need to create one
-	        	// Create the outside port for the router
-	        	LogicalRouterPort lrpo = new LogicalRouterPort();
-	        	lrpo.setAdminStatusEnabled(true);
-	        	lrpo.setDisplayName(lrc.getDisplayName() + "-outside-port");
-	        	lrpo.setTags(lrc.getTags());
-	        	List<String> outsideIpAddresses = new ArrayList<String>();
-	        	outsideIpAddresses.add(publicNetworkIpAddress);
-	        	lrpo.setIpAddresses(outsideIpAddresses);
-	        	lrpo = _niciraNvpApi.createLogicalRouterPort(lrc.getUuid(),lrpo);
-	        	
-	        	// Attach the outside port to the gateway service on the correct VLAN
-	        	L3GatewayAttachment attachment = new L3GatewayAttachment(cmd.getGatewayServiceUuid());
-	        	if (cmd.getPublicIpVlan() != 0) {
-	        		attachment.setVlanId(cmd.getPublicIpVlan());
-	        	}
-	        	_niciraNvpApi.modifyLogicalRouterPortAttachment(lrc.getUuid(), lrpo.getUuid(), attachment);
-	        	return new AssignIpToLogicalRouterAnswer(cmd, true, "Ip address configured on new logical router port");
-    		}
-    		else {
-    			// There is already and attachment to this public network, see if we need to add this IP
-    			boolean found = false;
-    			LogicalRouterPort publicPort = null;
-    			for (LogicalRouterPort port : ports.getResults()) {
-    				for (String cidr : port.getIpAddresses()) {
-    					if (publicNetworkIpAddress.equals(cidr)) {
-    						found = true;
-    						publicPort = port;
-    						break;
-    					}
+    		for (StaticNatRuleTO rule : cmd.getRules()) {
+    			// Find if a DestinationNat rule exists for this rule
+    			for (NatRule storedRule : existingRules.getResults()) {
+    				if ("SourceNatRule".equals(storedRule.getType())) {
+    					continue;
     				}
+    				String insideCidr = rule.getDstIp() + "/32";
+    				String outsideCidr = rule.getSrcIp() + "/32";
+    				//if (insideCidr.equals(storedRule.getMatch().getDestinationIpAddresses()))
     			}
-    			if (found) {
-    				s_logger.warn("Ip " + publicNetworkIpAddress + " is already configured on logical router " + cmd.getLogicalRouterUuid());
-    				return new AssignIpToLogicalRouterAnswer(cmd, true, "Ip address already alocated on logical Router");
-    			}
-    			
-    			publicPort.getIpAddresses().add(publicNetworkIpAddress);
-    			_niciraNvpApi.updateLogicalRouterPortConfig(cmd.getLogicalRouterUuid(), publicPort);
-    			return new AssignIpToLogicalRouterAnswer(cmd, true, "Ip address configured on existing logical router port");
     		}
+    		//FIXME implement!
+    		return new ConfigureStaticNatRulesOnLogicalRouterAnswer(cmd, true, cmd.getRules().size() +" StaticNat rules applied");
         } catch (NiciraNvpApiException e) {
         	if (numRetries > 0) {
         		return retry(cmd, --numRetries);
         	} 
         	else {
-        		return new DeleteLogicalRouterAnswer(cmd, e);
+        		return new ConfigureStaticNatRulesOnLogicalRouterAnswer(cmd, e);
+        	}
+        }
+    	
+    }
+
+    private Answer executeRequest(ConfigurePortForwardingRulesOnLogicalRouterCommand cmd, int numRetries) {
+    	try {
+    		LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid());
+    		//FIXME implement!
+    		return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(cmd, true, cmd.getRules().size() +" PortForwarding rules applied");
+        } catch (NiciraNvpApiException e) {
+        	if (numRetries > 0) {
+        		return retry(cmd, --numRetries);
+        	} 
+        	else {
+        		return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(cmd, e);
         	}
         }