Wire the PortForwarding and StaticNat calls

This commit is contained in:
Hugo Trippaers 2012-09-30 10:20:54 -07:00
parent d4d82fa3f7
commit c5634b9687
12 changed files with 409 additions and 281 deletions

View File

@ -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;
}
}

View File

@ -16,20 +16,19 @@
// under the License. // under the License.
package com.cloud.agent.api; package com.cloud.agent.api;
import com.cloud.agent.api.Command;
/** /**
* *
*/ */
public class AssignIpToLogicalRouterAnswer extends Answer { public class ConfigurePortForwardingRulesOnLogicalRouterAnswer extends Answer {
public AssignIpToLogicalRouterAnswer(Command command, boolean success, public ConfigurePortForwardingRulesOnLogicalRouterAnswer(Command command,
String details) { boolean success, String details) {
super(command, success, details); super(command, success, details);
} }
public AssignIpToLogicalRouterAnswer(Command command, Exception e) { public ConfigurePortForwardingRulesOnLogicalRouterAnswer(Command command,
super(command, e); Exception e) {
} super(command, e);
}
} }

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -33,7 +33,7 @@ public class CreateLogicalRouterAnswer extends Answer {
super(command, e); super(command, e);
} }
public String getLogicalSwitchUuid() { public String getLogicalRouterUuid() {
return _logicalRouterUuid; return _logicalRouterUuid;
} }

View File

@ -47,6 +47,10 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager; 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.CreateLogicalRouterAnswer;
import com.cloud.agent.api.CreateLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalRouterCommand;
import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; 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.StartupNiciraNvpCommand;
import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer; import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer;
import com.cloud.agent.api.UpdateLogicalSwitchPortCommand; 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.AddNiciraNvpDeviceCmd;
import com.cloud.api.commands.DeleteNiciraNvpDeviceCmd; import com.cloud.api.commands.DeleteNiciraNvpDeviceCmd;
import com.cloud.api.commands.ListNiciraNvpDeviceNetworksCmd; 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.NetworkVO;
import com.cloud.network.Networks; import com.cloud.network.Networks;
import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.IpAddress;
import com.cloud.network.NetworkManager; import com.cloud.network.NetworkManager;
import com.cloud.network.NiciraNvpDeviceVO; import com.cloud.network.NiciraNvpDeviceVO;
import com.cloud.network.NiciraNvpNicMappingVO; 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.dao.PhysicalNetworkServiceProviderVO;
import com.cloud.network.guru.NiciraNvpGuestNetworkGuru; import com.cloud.network.guru.NiciraNvpGuestNetworkGuru;
import com.cloud.network.resource.NiciraNvpResource; import com.cloud.network.resource.NiciraNvpResource;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.PortForwardingRule; import com.cloud.network.rules.PortForwardingRule;
import com.cloud.network.rules.StaticNat; 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.offering.NetworkOffering;
import com.cloud.resource.ResourceManager; import com.cloud.resource.ResourceManager;
import com.cloud.resource.ResourceState; 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 // Store the uuid so we can easily find it during cleanup
NiciraNvpRouterMappingVO routermapping = NiciraNvpRouterMappingVO routermapping =
new NiciraNvpRouterMappingVO(cmd.getLogicalSwitchUuid(), network.getId()); new NiciraNvpRouterMappingVO(answer.getLogicalRouterUuid(), network.getId());
_niciraNvpRouterMappingDao.persist(routermapping); _niciraNvpRouterMappingDao.persist(routermapping);
} }
} finally { } 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"); s_logger.warn("Unable to provide services without Connectivity service enabled for this element");
return false; return false;
} }
if ((services.contains(Service.PortForwarding) || services.contains(Service.StaticNat)) && !services.contains(Service.PortForwarding)) { if ((services.contains(Service.PortForwarding) || services.contains(Service.StaticNat)) && !services.contains(Service.SourceNat)) {
s_logger.warn("Unable to provider StaticNat and/or PortForwarding without the SourceNat service"); s_logger.warn("Unable to provide StaticNat and/or PortForwarding without the SourceNat service");
return false; return false;
} }
return true; return true;
@ -833,15 +843,49 @@ public class NiciraNvpElement extends AdapterBase implements
* From interface StaticNatServiceProvider * From interface StaticNatServiceProvider
*/ */
@Override @Override
public boolean applyStaticNats(Network config, public boolean applyStaticNats(Network network,
List<? extends StaticNat> rules) List<? extends StaticNat> rules)
throws ResourceUnavailableException { throws ResourceUnavailableException {
// FIXME Implement this if (!canHandle(network, Service.StaticNat)) {
s_logger.debug("Entering applyStaticNats"); // TODO Remove this line return false;
for (StaticNat rule : rules) { }
s_logger.debug ("StaticNat rule : from " + rule.getSourceIpAddressId() + " to " + rule.getDestIpAddress() + (rule.isForRevoke() ? " for revoke" : ""));
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 @Override
public boolean applyPFRules(Network network, List<PortForwardingRule> rules) public boolean applyPFRules(Network network, List<PortForwardingRule> rules)
throws ResourceUnavailableException { throws ResourceUnavailableException {
// FIXME Implement this if (!canHandle(network, Service.PortForwarding)) {
s_logger.debug("Entering applyPFRules"); // TODO Remove this line return false;
for (PortForwardingRule rule : rules) { }
s_logger.debug ("PortForwardingRule rule : from " + rule.getSourceIpAddressId() +
" to " + rule.getDestinationIpAddress().addr() + " port " + rule.getDestinationPortStart() + "-" + rule.getDestinationPortEnd()); 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();
} }
} }

View File

@ -1,53 +1,8 @@
package com.cloud.network.nicira; package com.cloud.network.nicira;
public class DestinationNatRule extends NatRule { public class DestinationNatRule extends NatRule {
private Match match; {
private String to_destination_ip_address_min; type = "DestinationNatRule";
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;
} }
} }

View File

@ -20,5 +20,92 @@ package com.cloud.network.nicira;
* *
*/ */
public abstract class NatRule { 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;
}
} }

View File

@ -284,6 +284,12 @@ public class NiciraNvpApi {
executeUpdateObject(logicalRouterPort, uri, Collections.<String,String>emptyMap()); 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 { private <T> void executeUpdateObject(T newObject, String uri, Map<String,String> parameters) throws NiciraNvpApiException {
String url; String url;
try { try {

View File

@ -20,60 +20,8 @@ package com.cloud.network.nicira;
* *
*/ */
public class SourceNatRule extends NatRule { public class SourceNatRule extends NatRule {
private Match match; {
private String to_source_ip_address_min; type = "SourceNatRule";
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;
}
} }

View File

@ -26,9 +26,11 @@ import org.apache.log4j.Logger;
import com.cloud.agent.IAgentControl; import com.cloud.agent.IAgentControl;
import com.cloud.agent.api.Answer; 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.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.CreateLogicalRouterAnswer;
import com.cloud.agent.api.CreateLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalRouterCommand;
import com.cloud.agent.api.CreateLogicalSwitchAnswer; 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.StartupNiciraNvpCommand;
import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer; import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer;
import com.cloud.agent.api.UpdateLogicalSwitchPortCommand; import com.cloud.agent.api.UpdateLogicalSwitchPortCommand;
import com.cloud.agent.api.to.StaticNatRuleTO;
import com.cloud.host.Host; import com.cloud.host.Host;
import com.cloud.host.Host.Type; import com.cloud.host.Host.Type;
import com.cloud.network.nicira.Attachment; 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.LogicalSwitch;
import com.cloud.network.nicira.LogicalSwitchPort; import com.cloud.network.nicira.LogicalSwitchPort;
import com.cloud.network.nicira.Match; import com.cloud.network.nicira.Match;
import com.cloud.network.nicira.NatRule;
import com.cloud.network.nicira.NiciraNvpApi; import com.cloud.network.nicira.NiciraNvpApi;
import com.cloud.network.nicira.NiciraNvpApiException; import com.cloud.network.nicira.NiciraNvpApiException;
import com.cloud.network.nicira.NiciraNvpList; import com.cloud.network.nicira.NiciraNvpList;
@ -220,10 +224,12 @@ public class NiciraNvpResource implements ServerResource {
else if (cmd instanceof DeleteLogicalRouterCommand) { else if (cmd instanceof DeleteLogicalRouterCommand) {
return executeRequest((DeleteLogicalRouterCommand) cmd, numRetries); return executeRequest((DeleteLogicalRouterCommand) cmd, numRetries);
} }
else if (cmd instanceof AssignIpToLogicalRouterCommand) { else if (cmd instanceof ConfigureStaticNatRulesOnLogicalRouterCommand) {
return executeRequest((AssignIpToLogicalRouterCommand) cmd, numRetries); 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); 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 { try {
LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid()); 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 = for (StaticNatRuleTO rule : cmd.getRules()) {
_niciraNvpApi.findLogicalRouterPortByGatewayServiceAndVlanId(cmd.getLogicalRouterUuid(), // Find if a DestinationNat rule exists for this rule
cmd.getGatewayServiceUuid(), cmd.getPublicIpVlan()); for (NatRule storedRule : existingRules.getResults()) {
if ("SourceNatRule".equals(storedRule.getType())) {
String publicNetworkIpAddress = cmd.getPublicIpCidr(); continue;
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;
}
} }
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) { } catch (NiciraNvpApiException e) {
if (numRetries > 0) { if (numRetries > 0) {
return retry(cmd, --numRetries); return retry(cmd, --numRetries);
} }
else { 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);
} }
} }