From bfc8da1082ea3d27eec6d743d28b67ececdfd161 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Tue, 25 Sep 2012 15:51:48 -0700 Subject: [PATCH 01/15] Update Nicira Api to support L3 functionality --- .../network/nicira/DestinationNatRule.java | 53 +++++++ .../network/nicira/L3GatewayAttachment.java | 54 +++++++ .../network/nicira/LogicalRouterConfig.java | 64 +++++++++ .../network/nicira/LogicalRouterPort.java | 90 ++++++++++++ .../src/com/cloud/network/nicira/Match.java | 133 ++++++++++++++++++ .../src/com/cloud/network/nicira/NatRule.java | 24 ++++ .../cloud/network/nicira/NiciraNvpApi.java | 61 +++++++- .../cloud/network/nicira/RouterNextHop.java | 34 +++++ .../cloud/network/nicira/RoutingConfig.java | 5 + ...ingleDefaultRouteImplictRoutingConfig.java | 38 +++++ .../cloud/network/nicira/SourceNatRule.java | 79 +++++++++++ 11 files changed, 630 insertions(+), 5 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterConfig.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterPort.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RoutingConfig.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SingleDefaultRouteImplictRoutingConfig.java create mode 100644 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/DestinationNatRule.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java new file mode 100644 index 00000000000..2cbdace3115 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/DestinationNatRule.java @@ -0,0 +1,53 @@ +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; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java new file mode 100644 index 00000000000..65a0a8ed5c4 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +/** + * + */ +public class L3GatewayAttachment extends Attachment { + private String l3_gateway_service_uuid; + private String type = "L3GatewayAttachment"; + private int vlan_id; + + public L3GatewayAttachment(String l3_gateway_service_uuid) { + this.l3_gateway_service_uuid = l3_gateway_service_uuid; + // VLAN 0 is untagged, set explicitly of no vlan is given in the constructor + this.vlan_id = 0; + } + + public L3GatewayAttachment(String l3_gateway_service_uuid, int vlan_id) { + this.l3_gateway_service_uuid = l3_gateway_service_uuid; + this.vlan_id = vlan_id; + } + + public String getL3GatewayServiceUuid() { + return l3_gateway_service_uuid; + } + + public void setL3GatewayServiceUuid(String l3_gateway_service_uuid) { + this.l3_gateway_service_uuid = l3_gateway_service_uuid; + } + + public int getVlanId() { + return vlan_id; + } + + public void setVlanId(int vlan_id) { + this.vlan_id = vlan_id; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterConfig.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterConfig.java new file mode 100644 index 00000000000..897ee06b844 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterConfig.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.network.nicira; + +import java.util.List; + +/** + * + */ +public class LogicalRouterConfig { + private String display_name; + private RoutingConfig routing_config; + private String type = "LogicalRouterConfig"; + private String uuid; + private List tags; + + public RoutingConfig getRoutingConfig() { + return routing_config; + } + + public void setRoutingConfig(RoutingConfig routing_config) { + this.routing_config = routing_config; + } + + public String getDisplayName() { + return display_name; + } + + public void setDisplayName(String display_name) { + this.display_name = display_name; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterPort.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterPort.java new file mode 100644 index 00000000000..196106dbe40 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/LogicalRouterPort.java @@ -0,0 +1,90 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +import java.util.List; + +/** + * + */ +public class LogicalRouterPort { + private String display_name; + private List tags; + private Integer portno; + private boolean admin_status_enabled; + private List ip_addresses; + private String mac_address; + private String type = "LogicalRouterPortConfig"; + private String uuid; + + public int getPortno() { + return portno; + } + + public void setPortno(int portno) { + this.portno = portno; + } + + public boolean isAdminStatusEnabled() { + return admin_status_enabled; + } + + public void setAdminStatusEnabled(boolean admin_status_enabled) { + this.admin_status_enabled = admin_status_enabled; + } + + public List getIpAddresses() { + return ip_addresses; + } + + public void setIpAddresses(List ip_addresses) { + this.ip_addresses = ip_addresses; + } + + public String getMacAddress() { + return mac_address; + } + + public void setMacAddress(String mac_address) { + this.mac_address = mac_address; + } + + public String getDisplayName() { + return display_name; + } + + public void setDisplayName(String display_name) { + this.display_name = display_name; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + +} \ No newline at end of file diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java new file mode 100644 index 00000000000..cd898476b39 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java @@ -0,0 +1,133 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +/** + * + */ +public class Match { + private Integer protocol; + private String source_ip_addresses; + private boolean source_ip_addresses_not; + private String destination_ip_addresses; + private boolean destination_ip_addresses_not; + private Integer source_port_min; + private Integer source_port_max; + private boolean source_port_not; + private Integer destination_port_min; + private Integer destination_port_max; + private boolean destination_port_not; + private String ethertype = "IPv4"; + + public Integer getProtocol() { + return protocol; + } + + public void setProtocol(Integer protocol) { + this.protocol = protocol; + } + + public Integer getSource_port_min() { + return source_port_min; + } + + public void setSourcePortMin(Integer source_port_min) { + this.source_port_min = source_port_min; + } + + public Integer getSourcePortMax() { + return source_port_max; + } + + public void setSourcePortMax(Integer source_port_max) { + this.source_port_max = source_port_max; + } + + public boolean isSourcePortNot() { + return source_port_not; + } + + public void setSourcePortNot(boolean source_port_not) { + this.source_port_not = source_port_not; + } + + public Integer getDestinationPortMin() { + return destination_port_min; + } + + public void setDestinationPortMin(Integer destination_port_min) { + this.destination_port_min = destination_port_min; + } + + public Integer getDestinationPortMax() { + return destination_port_max; + } + + public void setDestinationPortMax(Integer destination_port_max) { + this.destination_port_max = destination_port_max; + } + + public boolean isDestinationPortNot() { + return destination_port_not; + } + + public void setDestinationPortNot(boolean destination_port_not) { + this.destination_port_not = destination_port_not; + } + + public String getEthertype() { + return ethertype; + } + + public void setEthertype(String ethertype) { + this.ethertype = ethertype; + } + + public String getSourceIpAddresses() { + return source_ip_addresses; + } + + public void setSourceIpAddresses(String source_ip_addresses) { + this.source_ip_addresses = source_ip_addresses; + } + + public boolean isSourceIpAddressesNot() { + return source_ip_addresses_not; + } + + public void setSourceIpAddresses_not(boolean source_ip_addresses_not) { + this.source_ip_addresses_not = source_ip_addresses_not; + } + + public String getDestinationIpAddresses() { + return destination_ip_addresses; + } + + public void setDestinationIpAddresses(String destination_ip_addresses) { + this.destination_ip_addresses = destination_ip_addresses; + } + + public boolean isDestinationIpAddressesNot() { + return destination_ip_addresses_not; + } + + public void setDestinationIpAddressesNot(boolean destination_ip_addresses_not) { + this.destination_ip_addresses_not = destination_ip_addresses_not; + } + + +} 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 new file mode 100644 index 00000000000..30196f0e73a --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NatRule.java @@ -0,0 +1,24 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +/** + * + */ +public abstract class NatRule { + +} 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 2c002abdbff..1c7b74155d3 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 @@ -78,6 +78,7 @@ import com.google.gson.reflect.TypeToken; public class NiciraNvpApi { private static final Logger s_logger = Logger.getLogger(NiciraNvpApi.class); + private final static String _protocol = "https"; private String _name; private String _host; @@ -117,7 +118,7 @@ public class NiciraNvpApi { String url; try { - url = new URL("https", _host, "/ws.v1/login").toString(); + url = new URL(_protocol, _host, "/ws.v1/login").toString(); } catch (MalformedURLException e) { s_logger.error("Unable to build Nicira API URL", e); throw new NiciraNvpApiException("Unable to build Nicira API URL", e); @@ -210,11 +211,61 @@ public class NiciraNvpApi { return lspl; } + public LogicalRouterConfig createLogicalRouter(LogicalRouterConfig logicalRouterConfig) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter"; + + LogicalRouterConfig lrc = executeCreateObject(logicalRouterConfig, new TypeToken(){}.getType(), uri, Collections.emptyMap()); + + return lrc; + } + + public void deleteLogicalRouter(String logicalRouterUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid; + + executeDeleteObject(uri); + } + + public LogicalRouterPort createLogicalRouterPort(String logicalRouterUuid, LogicalRouterPort logicalRouterPort) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport"; + + LogicalRouterPort lrp = executeCreateObject(logicalRouterPort, new TypeToken(){}.getType(), uri, Collections.emptyMap()); + return lrp; + } + + public void deleteLogicalRouterPort(String logicalRouterUuid, String logicalRouterPortUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport/" + logicalRouterPortUuid; + + executeDeleteObject(uri); + } + + public void modifyLogicalRouterPortAttachment(String logicalRouterUuid, String logicalRouterPortUuid, Attachment attachment) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport/" + logicalRouterPortUuid + "/attachment"; + executeUpdateObject(attachment, uri, Collections.emptyMap()); + } + + public NatRule createLogicalRouterNatRule(String logicalRouterUuid, NatRule natRule) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat"; + + if (natRule instanceof SourceNatRule) { + return executeCreateObject(natRule, new TypeToken(){}.getType(), uri, Collections.emptyMap()); + } + else if (natRule instanceof DestinationNatRule) { + return executeCreateObject(natRule, new TypeToken(){}.getType(), uri, Collections.emptyMap()); + } + + throw new NiciraNvpApiException("Unknown NatRule type"); + } + + public void deleteLogicalRouterNatRule(String logicalRouterUuid, String natRuleUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat/" + natRuleUuid; + + executeDeleteObject(uri); + } private void executeUpdateObject(T newObject, String uri, Map parameters) throws NiciraNvpApiException { String url; try { - url = new URL("https", _host, uri).toString(); + url = new URL(_protocol, _host, uri).toString(); } catch (MalformedURLException e) { s_logger.error("Unable to build Nicira API URL", e); throw new NiciraNvpApiException("Connection to NVP Failed"); @@ -244,7 +295,7 @@ public class NiciraNvpApi { private T executeCreateObject(T newObject, Type returnObjectType, String uri, Map parameters) throws NiciraNvpApiException { String url; try { - url = new URL("https", _host, uri).toString(); + url = new URL(_protocol, _host, uri).toString(); } catch (MalformedURLException e) { s_logger.error("Unable to build Nicira API URL", e); throw new NiciraNvpApiException("Unable to build Nicira API URL", e); @@ -282,7 +333,7 @@ public class NiciraNvpApi { private void executeDeleteObject(String uri) throws NiciraNvpApiException { String url; try { - url = new URL("https", _host, uri).toString(); + url = new URL(_protocol, _host, uri).toString(); } catch (MalformedURLException e) { s_logger.error("Unable to build Nicira API URL", e); throw new NiciraNvpApiException("Unable to build Nicira API URL", e); @@ -303,7 +354,7 @@ public class NiciraNvpApi { private T executeRetrieveObject(Type returnObjectType, String uri, Map parameters) throws NiciraNvpApiException { String url; try { - url = new URL("https", _host, uri).toString(); + url = new URL(_protocol, _host, uri).toString(); } catch (MalformedURLException e) { s_logger.error("Unable to build Nicira API URL", e); throw new NiciraNvpApiException("Unable to build Nicira API URL", e); diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java new file mode 100644 index 00000000000..be302fab79a --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.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.network.nicira; + +/** + * + */ +public class RouterNextHop { + private String gateway_ip_address; + private String type = "RouterNextHop"; + + public String getGatewayIpAddress() { + return gateway_ip_address; + } + + public void setGatewayIpAddress(String gateway_ip_address) { + this.gateway_ip_address = gateway_ip_address; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RoutingConfig.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RoutingConfig.java new file mode 100644 index 00000000000..12b3227e2e2 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RoutingConfig.java @@ -0,0 +1,5 @@ +package com.cloud.network.nicira; + +public class RoutingConfig { + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SingleDefaultRouteImplictRoutingConfig.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SingleDefaultRouteImplictRoutingConfig.java new file mode 100644 index 00000000000..b4eda447b13 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SingleDefaultRouteImplictRoutingConfig.java @@ -0,0 +1,38 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +/** + * + */ +public class SingleDefaultRouteImplictRoutingConfig extends RoutingConfig { + public RouterNextHop default_route_next_hop; + public String type = "SingleDefaultRouteImplicitRoutingConfig"; + + public SingleDefaultRouteImplictRoutingConfig(RouterNextHop routerNextHop) { + default_route_next_hop = routerNextHop; + } + + public RouterNextHop getDefaultRouteNextHop() { + return default_route_next_hop; + } + + public void setDefaultRouteNextHop(RouterNextHop default_route_next_hop) { + this.default_route_next_hop = default_route_next_hop; + } + +} 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 new file mode 100644 index 00000000000..7d1c13d9fc5 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/SourceNatRule.java @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.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; + } + +} From fe997651a6da0d3883cf54b0c32bf5f975b3fce0 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 27 Sep 2012 15:34:53 -0700 Subject: [PATCH 02/15] Wired up the creation of the SourceNat router in the element --- .../agent/api/CreateLogicalRouterAnswer.java | 40 +++++ .../agent/api/CreateLogicalRouterCommand.java | 115 +++++++++++++ .../api/commands/AddNiciraNvpDeviceCmd.java | 2 + .../network/element/NiciraNvpElement.java | 159 ++++++++++++++++-- .../network/nicira/L3GatewayAttachment.java | 10 +- .../cloud/network/nicira/PatchAttachment.java | 39 +++++ .../cloud/network/nicira/RouterNextHop.java | 4 + .../network/resource/NiciraNvpResource.java | 109 ++++++++++++ 8 files changed, 461 insertions(+), 17 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterCommand.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/PatchAttachment.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 new file mode 100644 index 00000000000..3337e92cf79 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterAnswer.java @@ -0,0 +1,40 @@ +// 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 CreateLogicalRouterAnswer extends Answer { + + private String _logicalRouterUuid; + + public CreateLogicalRouterAnswer(Command command, boolean success, + String details, String logicalRouterUuid) { + super(command, success, details); + this._logicalRouterUuid = logicalRouterUuid; + } + + public CreateLogicalRouterAnswer(Command command, Exception e) { + super(command, e); + } + + public String getLogicalSwitchUuid() { + return _logicalRouterUuid; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterCommand.java new file mode 100644 index 00000000000..57440dfc4b0 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/CreateLogicalRouterCommand.java @@ -0,0 +1,115 @@ +// 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 CreateLogicalRouterCommand extends Command { + private String _gatewayServiceUuid; + private String _logicalSwitchUuid; + private long _vlanId; + private String _name; + private String _ownerName; + private String _publicIpCidr; + private String _publicNextHop; + private String _internalIpCidr; + + public CreateLogicalRouterCommand(String gatewayServiceUuid, long vlanId, + String logicalSwitchUuid, String name, + String publicIpCidr, String publicNextHop, + String internalIpCidr, String ownerName) { + super(); + this._gatewayServiceUuid = gatewayServiceUuid; + this._logicalSwitchUuid = logicalSwitchUuid; + this._vlanId = vlanId; + this._name = name; + this._ownerName = ownerName; + this._publicIpCidr = publicIpCidr; + this._publicNextHop = publicNextHop; + this._internalIpCidr = internalIpCidr; + } + + @Override + public boolean executeInSequence() { + return false; + } + + public String getGatewayServiceUuid() { + return _gatewayServiceUuid; + } + + public void setGatewayServiceUuid(String gatewayServiceUuid) { + this._gatewayServiceUuid = gatewayServiceUuid; + } + + public String getLogicalSwitchUuid() { + return _logicalSwitchUuid; + } + + public void setLogicalSwitchUuid(String logicalSwitchUuid) { + _logicalSwitchUuid = logicalSwitchUuid; + } + + public long getVlanId() { + return _vlanId; + } + + public void setVlanId(long vlanId) { + this._vlanId = vlanId; + } + + public String getName() { + return _name; + } + + public void setName(String name) { + this._name = name; + } + + public String getOwnerName() { + return _ownerName; + } + + public void setOwnerName(String ownerName) { + this._ownerName = ownerName; + } + + public String getPublicIpCidr() { + return _publicIpCidr; + } + + public void setPublicIpCidr(String publicIpCidr) { + this._publicIpCidr = publicIpCidr; + } + + public String getInternalIpCidr() { + return _internalIpCidr; + } + + public void setInternalIpCidr(String internalIpCidr) { + this._internalIpCidr = internalIpCidr; + } + + public String getPublicNextHop() { + return _publicNextHop; + } + + public void setPublicNextHop(String publicNextHop) { + this._publicNextHop = publicNextHop; + } +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java index 6eac21f7255..28bc1fc9ad4 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java @@ -67,6 +67,8 @@ public class AddNiciraNvpDeviceCmd extends BaseCmd { @Parameter(name=ApiConstants.NICIRA_NVP_TRANSPORT_ZONE_UUID, type=CommandType.STRING, required = true, description="The Transportzone UUID configured on the Nicira Controller") private String transportzoneuuid; + //FIXME add optional gateway service uuid + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// 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 1fcccdb1ee3..638ed8ab117 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,8 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; import com.cloud.agent.AgentManager; +import com.cloud.agent.api.CreateLogicalRouterAnswer; +import com.cloud.agent.api.CreateLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; import com.cloud.agent.api.CreateLogicalSwitchPortCommand; import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer; @@ -62,6 +64,9 @@ import com.cloud.api.commands.DeleteNiciraNvpDeviceCmd; import com.cloud.api.commands.ListNiciraNvpDeviceNetworksCmd; import com.cloud.api.commands.ListNiciraNvpDevicesCmd; import com.cloud.api.response.NiciraNvpDeviceResponse; +import com.cloud.configuration.ConfigurationManager; +import com.cloud.dc.Vlan; +import com.cloud.dc.dao.VlanDao; import com.cloud.deploy.DeployDestination; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; @@ -80,11 +85,15 @@ 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.NetworkManager; import com.cloud.network.NiciraNvpDeviceVO; import com.cloud.network.NiciraNvpNicMappingVO; import com.cloud.network.PhysicalNetworkServiceProvider; import com.cloud.network.PhysicalNetworkVO; +import com.cloud.network.PublicIpAddress; +import com.cloud.network.addr.PublicIp; import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkServiceMapDao; import com.cloud.network.dao.NiciraNvpDao; import com.cloud.network.dao.NiciraNvpNicMappingDao; import com.cloud.network.dao.PhysicalNetworkDao; @@ -98,11 +107,13 @@ import com.cloud.resource.ResourceState; import com.cloud.resource.ResourceStateAdapter; import com.cloud.resource.ServerResource; import com.cloud.resource.UnableDeleteHostException; +import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.Inject; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicProfile; import com.cloud.vm.NicVO; import com.cloud.vm.ReservationContext; @@ -111,7 +122,7 @@ import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.dao.NicDao; @Local(value = NetworkElement.class) -public class NiciraNvpElement extends AdapterBase implements ConnectivityProvider, NiciraNvpElementService, ResourceStateAdapter { +public class NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, NiciraNvpElementService, ResourceStateAdapter, IpDeployer { private static final Logger s_logger = Logger.getLogger(NiciraNvpElement.class); private static final Map> capabilities = setCapabilities(); @@ -137,6 +148,14 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide NiciraNvpNicMappingDao _niciraNvpNicMappingDao; @Inject NetworkDao _networkDao; + @Inject + NetworkManager _networkManager; + @Inject + ConfigurationManager _configMgr; + @Inject + NetworkServiceMapDao _ntwkSrvcDao; + @Inject + VlanDao _vlanDao; @Override public Map> getCapabilities() { @@ -148,12 +167,23 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide return Provider.NiciraNvp; } - private boolean canHandle(Network network) { + private boolean canHandle(Network network, Service service) { + s_logger.debug("Checking if NiciraNvpElement can handle service " + service.getName() + " on network " + network.getDisplayText()); if (network.getBroadcastDomainType() != BroadcastDomainType.Lswitch) { return false; } - return true; + if (!_networkManager.isProviderForNetwork(getProvider(), network.getId())) { + s_logger.debug("NiciraNvpElement is not a provider for network " + network.getDisplayText()); + return false; + } + + if (!_ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), service, Network.Provider.NiciraNvp)) { + s_logger.debug("NiciraNvpElement can't provide the " + service.getName() + " service on network " + network.getDisplayText()); + return false; + } + + return true; } @Override @@ -169,12 +199,79 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + s_logger.debug("entering NiciraNvpElement implement function for network " + + network.getDisplayText() + " (state " + network.getState() + ")"); - if (!canHandle(network)) { + if (!canHandle(network, Service.Connectivity)) { return false; } - return true; + if (network.getBroadcastUri() == null) { + s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); + return false; + } + + List devices = _niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); + _hostDao.loadDetails(niciraNvpHost); + + Account owner = context.getAccount(); + + /** + * Lock the network as we might need to do multiple operations that + * should be done only once. + */ + Network lock = _networkDao.acquireInLockTable(network.getId(), + _networkManager.getNetworkLockTimeout()); + if (lock == null) { + throw new ConcurrentOperationException("Unable to lock network " + + network.getId()); + } + try { + if (_networkManager.isProviderSupportServiceInNetwork( + network.getId(), Service.SourceNat, Provider.NiciraNvp)) { + s_logger.debug("Apparently we are supposed to provide SourceNat on this network"); + + PublicIp sourceNatIp = _networkManager.assignSourceNatIpAddressToGuestNetwork(owner, network); + String publicCidr = sourceNatIp.getAddress().addr() + "/" + NetUtils.getCidrSize(sourceNatIp.getVlanNetmask()); + String internalCidr = network.getGateway() + "/" + network.getCidr().split("/")[1]; + long vlanid = (Vlan.UNTAGGED.equals(sourceNatIp.getVlanTag())) ? 0 : Long.parseLong(sourceNatIp.getVlanTag()); + + CreateLogicalRouterCommand cmd = new CreateLogicalRouterCommand( + niciraNvpHost.getDetail("gatewayserviceuuid"), + vlanid, + network.getBroadcastUri().getSchemeSpecificPart(), + "router-" + network.getDisplayText(), + publicCidr, + sourceNatIp.getGateway(), + internalCidr, + context.getDomain().getName() + "-" + context.getAccount().getAccountName()); + CreateLogicalRouterAnswer answer = + (CreateLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd); + if (answer.getResult() == false) { + s_logger.error("Failed to create Logical Router for network " + network.getDisplayText()); + return false; + } + + } + + + } finally { + if (lock != null) { + _networkDao.releaseFromLockTable(lock.getId()); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Lock is released for network id " + + lock.getId() + " as a part of router startup in " + + dest); + } + } + } + return true; } @Override @@ -184,7 +281,7 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { - if (!canHandle(network)) { + if (!canHandle(network, Service.Connectivity)) { return false; } @@ -244,7 +341,7 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException { - if (!canHandle(network)) { + if (!canHandle(network, Service.Connectivity)) { return false; } @@ -286,7 +383,7 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide public boolean shutdown(Network network, ReservationContext context, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException { - if (!canHandle(network)) { + if (!canHandle(network, Service.Connectivity)) { return false; } @@ -296,7 +393,7 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide @Override public boolean destroy(Network network) throws ConcurrentOperationException, ResourceUnavailableException { - if (!canHandle(network)) { + if (!canHandle(network, Service.Connectivity)) { return false; } @@ -318,18 +415,34 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide @Override public boolean canEnableIndividualServices() { - return false; + return true; } @Override public boolean verifyServicesCombination(Set services) { - return true; + // This element can only function in a Nicra Nvp based + // SDN network, so Connectivity needs to be present here + if (services.contains(Service.Connectivity)) { + return true; + } + s_logger.debug("Unable to provide services without Connectivity service enabled for this element"); + return false; } private static Map> setCapabilities() { Map> capabilities = new HashMap>(); + // Basic L2 SDN provisioning capabilities.put(Service.Connectivity, null); + + // L3 Support : SourceNat + capabilities.put(Service.Gateway, null); + Map sourceNatCapabilities = new HashMap(); + sourceNatCapabilities.put(Capability.SupportedSourceNatTypes, "peraccount"); + sourceNatCapabilities.put(Capability.RedundantRouter, "true"); + capabilities.put(Service.SourceNat, sourceNatCapabilities); + + return capabilities; } @@ -540,4 +653,28 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide return new DeleteHostAnswer(true); } + /** + * From interface SourceNatServiceProvider + */ + @Override + public IpDeployer getIpDeployer(Network network) { + return this; + } + + /** + * From interface IpDeployer + * @param network + * @param ipAddress + * @param services + * @return + * @throws ResourceUnavailableException + */ + @Override + public boolean applyIps(Network network, + List ipAddress, Set services) + throws ResourceUnavailableException { + // TODO Auto-generated method stub + return false; + } + } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java index 65a0a8ed5c4..8b807fd9e72 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/L3GatewayAttachment.java @@ -22,15 +22,13 @@ package com.cloud.network.nicira; public class L3GatewayAttachment extends Attachment { private String l3_gateway_service_uuid; private String type = "L3GatewayAttachment"; - private int vlan_id; + private Long vlan_id; public L3GatewayAttachment(String l3_gateway_service_uuid) { this.l3_gateway_service_uuid = l3_gateway_service_uuid; - // VLAN 0 is untagged, set explicitly of no vlan is given in the constructor - this.vlan_id = 0; } - public L3GatewayAttachment(String l3_gateway_service_uuid, int vlan_id) { + public L3GatewayAttachment(String l3_gateway_service_uuid, long vlan_id) { this.l3_gateway_service_uuid = l3_gateway_service_uuid; this.vlan_id = vlan_id; } @@ -43,11 +41,11 @@ public class L3GatewayAttachment extends Attachment { this.l3_gateway_service_uuid = l3_gateway_service_uuid; } - public int getVlanId() { + public long getVlanId() { return vlan_id; } - public void setVlanId(int vlan_id) { + public void setVlanId(long vlan_id) { this.vlan_id = vlan_id; } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/PatchAttachment.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/PatchAttachment.java new file mode 100644 index 00000000000..e57c2493f1c --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/PatchAttachment.java @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.nicira; + +/** + * + */ +public class PatchAttachment extends Attachment { + private final String type = "PatchAttachment"; + private String peer_port_uuid; + + public PatchAttachment(String peerPortUuid) { + this.peer_port_uuid = peerPortUuid; + } + + public String getPeerPortUuid() { + return peer_port_uuid; + } + + public void setPeerPortUuid(String peerPortUuid) { + this.peer_port_uuid = peerPortUuid; + } + + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java index be302fab79a..c018af37467 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/RouterNextHop.java @@ -22,6 +22,10 @@ package com.cloud.network.nicira; public class RouterNextHop { private String gateway_ip_address; private String type = "RouterNextHop"; + + public RouterNextHop(String gatewayIpAddress) { + this.gateway_ip_address = gatewayIpAddress; + } public String getGatewayIpAddress() { return gateway_ip_address; 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 31cac6f000c..4aa6ff18798 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 @@ -27,6 +27,8 @@ import org.apache.log4j.Logger; import com.cloud.agent.IAgentControl; import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; +import com.cloud.agent.api.CreateLogicalRouterAnswer; +import com.cloud.agent.api.CreateLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalSwitchAnswer; import com.cloud.agent.api.CreateLogicalSwitchCommand; import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; @@ -48,17 +50,28 @@ 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.Attachment; import com.cloud.network.nicira.ControlClusterStatus; +import com.cloud.network.nicira.L3GatewayAttachment; +import com.cloud.network.nicira.LogicalRouterConfig; +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.NiciraNvpApi; import com.cloud.network.nicira.NiciraNvpApiException; import com.cloud.network.nicira.NiciraNvpList; import com.cloud.network.nicira.NiciraNvpTag; +import com.cloud.network.nicira.PatchAttachment; +import com.cloud.network.nicira.RouterNextHop; +import com.cloud.network.nicira.SingleDefaultRouteImplictRoutingConfig; +import com.cloud.network.nicira.SourceNatRule; import com.cloud.network.nicira.TransportZoneBinding; import com.cloud.network.nicira.VifAttachment; import com.cloud.resource.ServerResource; +import edu.emory.mathcs.backport.java.util.Arrays; + public class NiciraNvpResource implements ServerResource { private static final Logger s_logger = Logger.getLogger(NiciraNvpResource.class); @@ -197,6 +210,9 @@ public class NiciraNvpResource implements ServerResource { else if (cmd instanceof FindLogicalSwitchPortCommand) { return executeRequest((FindLogicalSwitchPortCommand) cmd, numRetries); } + else if (cmd instanceof CreateLogicalRouterCommand) { + return executeRequest((CreateLogicalRouterCommand) cmd, 0); //TODO set to numRetries when done + } s_logger.debug("Received unsupported command " + cmd.toString()); return Answer.createUnsupportedCommandAnswer(cmd); } @@ -340,6 +356,99 @@ public class NiciraNvpResource implements ServerResource { } } + private Answer executeRequest(CreateLogicalRouterCommand cmd, int numRetries) { + String routerName = cmd.getName(); + String gatewayServiceUuid = cmd.getGatewayServiceUuid(); + String logicalSwitchUuid = cmd.getLogicalSwitchUuid(); + + List tags = new ArrayList(); + tags.add(new NiciraNvpTag("cs_account",cmd.getOwnerName())); + + String publicNetworkNextHopIp = cmd.getPublicNextHop(); + String publicNetworkIpAddress = cmd.getPublicIpCidr(); + String internalNetworkAddress = cmd.getInternalIpCidr(); + + s_logger.debug("Creating a logical router with external ip " + + publicNetworkIpAddress + " and internal ip " + internalNetworkAddress + + "on gateway service " + gatewayServiceUuid); + + try { + // Create the Router + LogicalRouterConfig lrc = new LogicalRouterConfig(); + lrc.setDisplayName(routerName); + lrc.setTags(tags); + lrc.setRoutingConfig(new SingleDefaultRouteImplictRoutingConfig( + new RouterNextHop(publicNetworkNextHopIp))); + lrc = _niciraNvpApi.createLogicalRouter(lrc); + + try { + // Create the outside port for the router + LogicalRouterPort lrpo = new LogicalRouterPort(); + lrpo.setAdminStatusEnabled(true); + lrpo.setDisplayName(routerName + "-port"); + lrpo.setTags(tags); + List outsideIpAddresses = new ArrayList(); + 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(gatewayServiceUuid); + if (cmd.getVlanId() != 0) { + attachment.setVlanId(cmd.getVlanId()); + } + _niciraNvpApi.modifyLogicalRouterPortAttachment(lrc.getUuid(), lrpo.getUuid(), attachment); + + // Create the inside port for the router + LogicalRouterPort lrpi = new LogicalRouterPort(); + lrpi.setAdminStatusEnabled(true); + lrpi.setDisplayName(routerName + "-port"); + lrpi.setTags(tags); + List insideIpAddresses = new ArrayList(); + insideIpAddresses.add(internalNetworkAddress); + lrpi.setIpAddresses(insideIpAddresses); + lrpi = _niciraNvpApi.createLogicalRouterPort(lrc.getUuid(),lrpi); + + // Create the inside port on the lswitch + LogicalSwitchPort lsp = new LogicalSwitchPort(routerName + "-port", tags, true); + lsp = _niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp); + + // Attach the inside router port to the lswitch port with a PatchAttachment + _niciraNvpApi.modifyLogicalRouterPortAttachment(lrc.getUuid(), lrpi.getUuid(), + new PatchAttachment(lsp.getUuid())); + + // Attach the inside lswitch port to the router with a PatchAttachment + _niciraNvpApi.modifyLogicalSwitchPortAttachment(logicalSwitchUuid, lsp.getUuid(), + new PatchAttachment(lrpi.getUuid())); + + // Setup the source nat rule + SourceNatRule snr = new SourceNatRule(); + snr.setToSourceIpAddressMin(publicNetworkIpAddress.split("/")[0]); + snr.setToSourceIpAddressMax(publicNetworkIpAddress.split("/")[0]); + Match match = new Match(); + match.setSourceIpAddresses(internalNetworkAddress); + snr.setMatch(match); + _niciraNvpApi.createLogicalRouterNatRule(lrc.getUuid(), snr); + } finally { + // We need to destroy the router if we already created it + // this will also take care of any router ports + // TODO Clean up the switchport + try { + _niciraNvpApi.deleteLogicalRouter(lrc.getUuid()); + } catch (NiciraNvpApiException e) {} + } + + return new CreateLogicalRouterAnswer(cmd, true, "Logical Router created (uuid " + lrc.getUuid() + ")", lrc.getUuid()); + } catch (NiciraNvpApiException e) { + if (numRetries > 0) { + return retry(cmd, --numRetries); + } + else { + return new CreateLogicalRouterAnswer(cmd, e); + } + } + } + private Answer executeRequest(ReadyCommand cmd) { return new ReadyAnswer(cmd); } From 8ba7749869396b351920ccd986952f6b1a28dc51 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 27 Sep 2012 18:24:19 -0700 Subject: [PATCH 03/15] Prepare all capabilities we will be supporting Remove the logical router if we are done with it. --- api/src/com/cloud/api/ApiConstants.java | 1 + .../agent/api/DeleteLogicalRouterAnswer.java | 32 + .../agent/api/DeleteLogicalRouterCommand.java | 41 + .../api/commands/AddNiciraNvpDeviceCmd.java | 7 +- .../network/element/NiciraNvpElement.java | 1082 ++++++++++------- .../network/resource/NiciraNvpResource.java | 27 +- 6 files changed, 715 insertions(+), 475 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterAnswer.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterCommand.java diff --git a/api/src/com/cloud/api/ApiConstants.java b/api/src/com/cloud/api/ApiConstants.java index 067ddf73567..32b852fb4a3 100755 --- a/api/src/com/cloud/api/ApiConstants.java +++ b/api/src/com/cloud/api/ApiConstants.java @@ -382,6 +382,7 @@ public class ApiConstants { public static final String NICIRA_NVP_DEVICE_ID = "nvpdeviceid"; public static final String NICIRA_NVP_TRANSPORT_ZONE_UUID = "transportzoneuuid"; public static final String NICIRA_NVP_DEVICE_NAME = "niciradevicename"; + public static final String NICIRA_NVP_GATEWAYSERVICE_UUID = "l3gatewayserviceuuid"; public enum HostDetails { diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterAnswer.java new file mode 100644 index 00000000000..8a6bb9f193a --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterAnswer.java @@ -0,0 +1,32 @@ +// 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 DeleteLogicalRouterAnswer extends Answer { + + public DeleteLogicalRouterAnswer(Command command, boolean success, + String details) { + super(command, success, details); + } + + public DeleteLogicalRouterAnswer(Command command, Exception e) { + super(command, e); + } +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterCommand.java new file mode 100644 index 00000000000..4799f9e58fe --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/DeleteLogicalRouterCommand.java @@ -0,0 +1,41 @@ +// 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 DeleteLogicalRouterCommand extends Command { + + private String _logicalRouterUuid; + + public DeleteLogicalRouterCommand(String logicalRouterUuid) { + this._logicalRouterUuid = logicalRouterUuid; + } + + /* (non-Javadoc) + * @see com.cloud.agent.api.Command#executeInSequence() + */ + @Override + public boolean executeInSequence() { + return false; + } + + public String getLogicalRouterUuid() { + return _logicalRouterUuid; + } +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java index 28bc1fc9ad4..1f497c6a2c5 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java @@ -67,7 +67,8 @@ public class AddNiciraNvpDeviceCmd extends BaseCmd { @Parameter(name=ApiConstants.NICIRA_NVP_TRANSPORT_ZONE_UUID, type=CommandType.STRING, required = true, description="The Transportzone UUID configured on the Nicira Controller") private String transportzoneuuid; - //FIXME add optional gateway service uuid + @Parameter(name=ApiConstants.NICIRA_NVP_GATEWAYSERVICE_UUID, type=CommandType.STRING, required = false, description="The L3 Gateway Service UUID configured on the Nicira Controller") + private String l3gatewayserviceuuid; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -92,6 +93,10 @@ public class AddNiciraNvpDeviceCmd extends BaseCmd { public String getTransportzoneUuid() { return transportzoneuuid; } + + public String getL3GatewayServiceUuid() { + return l3gatewayserviceuuid; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// 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 638ed8ab117..161f3dc917e 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,6 +51,8 @@ import com.cloud.agent.api.CreateLogicalRouterAnswer; import com.cloud.agent.api.CreateLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; import com.cloud.agent.api.CreateLogicalSwitchPortCommand; +import com.cloud.agent.api.DeleteLogicalRouterAnswer; +import com.cloud.agent.api.DeleteLogicalRouterCommand; import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer; import com.cloud.agent.api.DeleteLogicalSwitchPortCommand; import com.cloud.agent.api.FindLogicalSwitchPortAnswer; @@ -101,6 +103,8 @@ 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.PortForwardingRule; +import com.cloud.network.rules.StaticNat; import com.cloud.offering.NetworkOffering; import com.cloud.resource.ResourceManager; import com.cloud.resource.ResourceState; @@ -122,106 +126,121 @@ import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.dao.NicDao; @Local(value = NetworkElement.class) -public class NiciraNvpElement extends AdapterBase implements ConnectivityProvider, SourceNatServiceProvider, NiciraNvpElementService, ResourceStateAdapter, IpDeployer { - private static final Logger s_logger = Logger.getLogger(NiciraNvpElement.class); - - private static final Map> capabilities = setCapabilities(); - +public class NiciraNvpElement extends AdapterBase implements + ConnectivityProvider, SourceNatServiceProvider, + PortForwardingServiceProvider, StaticNatServiceProvider, + NiciraNvpElementService, ResourceStateAdapter, IpDeployer { + private static final Logger s_logger = Logger + .getLogger(NiciraNvpElement.class); - @Inject - NicDao _nicDao; - @Inject - ResourceManager _resourceMgr; - @Inject - PhysicalNetworkDao _physicalNetworkDao; - @Inject - PhysicalNetworkServiceProviderDao _physicalNetworkServiceProviderDao; - @Inject - NiciraNvpDao _niciraNvpDao; - @Inject - HostDetailsDao _hostDetailsDao; - @Inject - HostDao _hostDao; - @Inject - AgentManager _agentMgr; - @Inject - NiciraNvpNicMappingDao _niciraNvpNicMappingDao; - @Inject - NetworkDao _networkDao; - @Inject - NetworkManager _networkManager; - @Inject - ConfigurationManager _configMgr; - @Inject - NetworkServiceMapDao _ntwkSrvcDao; - @Inject - VlanDao _vlanDao; - - @Override - public Map> getCapabilities() { - return capabilities; - } + private static final Map> capabilities = setCapabilities(); - @Override - public Provider getProvider() { - return Provider.NiciraNvp; - } - - private boolean canHandle(Network network, Service service) { - s_logger.debug("Checking if NiciraNvpElement can handle service " + service.getName() + " on network " + network.getDisplayText()); - if (network.getBroadcastDomainType() != BroadcastDomainType.Lswitch) { - return false; - } - - if (!_networkManager.isProviderForNetwork(getProvider(), network.getId())) { - s_logger.debug("NiciraNvpElement is not a provider for network " + network.getDisplayText()); - return false; - } - - if (!_ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), service, Network.Provider.NiciraNvp)) { - s_logger.debug("NiciraNvpElement can't provide the " + service.getName() + " service on network " + network.getDisplayText()); - return false; - } - - return true; - } - - @Override - public boolean configure(String name, Map params) - throws ConfigurationException { - super.configure(name, params); - _resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this); - return true; - } + @Inject + NicDao _nicDao; + @Inject + ResourceManager _resourceMgr; + @Inject + PhysicalNetworkDao _physicalNetworkDao; + @Inject + PhysicalNetworkServiceProviderDao _physicalNetworkServiceProviderDao; + @Inject + NiciraNvpDao _niciraNvpDao; + @Inject + HostDetailsDao _hostDetailsDao; + @Inject + HostDao _hostDao; + @Inject + AgentManager _agentMgr; + @Inject + NiciraNvpNicMappingDao _niciraNvpNicMappingDao; + @Inject + NetworkDao _networkDao; + @Inject + NetworkManager _networkManager; + @Inject + ConfigurationManager _configMgr; + @Inject + NetworkServiceMapDao _ntwkSrvcDao; + @Inject + VlanDao _vlanDao; + + @Override + public Map> getCapabilities() { + return capabilities; + } + + @Override + public Provider getProvider() { + return Provider.NiciraNvp; + } + + private boolean canHandle(Network network, Service service) { + s_logger.debug("Checking if NiciraNvpElement can handle service " + + service.getName() + " on network " + network.getDisplayText()); + if (network.getBroadcastDomainType() != BroadcastDomainType.Lswitch) { + return false; + } + + if (!_networkManager.isProviderForNetwork(getProvider(), + network.getId())) { + s_logger.debug("NiciraNvpElement is not a provider for network " + + network.getDisplayText()); + return false; + } + + if (!_ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), + service, Network.Provider.NiciraNvp)) { + s_logger.debug("NiciraNvpElement can't provide the " + + service.getName() + " service on network " + + network.getDisplayText()); + return false; + } + + return true; + } + + @Override + public boolean configure(String name, Map params) + throws ConfigurationException { + super.configure(name, params); + _resourceMgr.registerResourceStateAdapter(this.getClass() + .getSimpleName(), this); + return true; + } + + @Override + public boolean implement(Network network, NetworkOffering offering, + DeployDestination dest, ReservationContext context) + throws ConcurrentOperationException, ResourceUnavailableException, + InsufficientCapacityException { + s_logger.debug("entering NiciraNvpElement implement function for network " + + network.getDisplayText() + + " (state " + + network.getState() + + ")"); + + if (!canHandle(network, Service.Connectivity)) { + return false; + } + + if (network.getBroadcastUri() == null) { + s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); + return false; + } + + List devices = _niciraNvpDao + .listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); + _hostDao.loadDetails(niciraNvpHost); + + Account owner = context.getAccount(); - @Override - public boolean implement(Network network, NetworkOffering offering, - DeployDestination dest, ReservationContext context) - throws ConcurrentOperationException, ResourceUnavailableException, - InsufficientCapacityException { - s_logger.debug("entering NiciraNvpElement implement function for network " - + network.getDisplayText() + " (state " + network.getState() + ")"); - - if (!canHandle(network, Service.Connectivity)) { - return false; - } - - if (network.getBroadcastUri() == null) { - s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); - return false; - } - - List devices = _niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); - if (devices.isEmpty()) { - s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); - return false; - } - NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); - HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); - _hostDao.loadDetails(niciraNvpHost); - - Account owner = context.getAccount(); - /** * Lock the network as we might need to do multiple operations that * should be done only once. @@ -236,31 +255,33 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide if (_networkManager.isProviderSupportServiceInNetwork( network.getId(), Service.SourceNat, Provider.NiciraNvp)) { s_logger.debug("Apparently we are supposed to provide SourceNat on this network"); - - PublicIp sourceNatIp = _networkManager.assignSourceNatIpAddressToGuestNetwork(owner, network); - String publicCidr = sourceNatIp.getAddress().addr() + "/" + NetUtils.getCidrSize(sourceNatIp.getVlanNetmask()); - String internalCidr = network.getGateway() + "/" + network.getCidr().split("/")[1]; - long vlanid = (Vlan.UNTAGGED.equals(sourceNatIp.getVlanTag())) ? 0 : Long.parseLong(sourceNatIp.getVlanTag()); - + + PublicIp sourceNatIp = _networkManager + .assignSourceNatIpAddressToGuestNetwork(owner, network); + String publicCidr = sourceNatIp.getAddress().addr() + "/" + + NetUtils.getCidrSize(sourceNatIp.getVlanNetmask()); + String internalCidr = network.getGateway() + "/" + + network.getCidr().split("/")[1]; + long vlanid = (Vlan.UNTAGGED.equals(sourceNatIp.getVlanTag())) ? 0 + : Long.parseLong(sourceNatIp.getVlanTag()); + CreateLogicalRouterCommand cmd = new CreateLogicalRouterCommand( - niciraNvpHost.getDetail("gatewayserviceuuid"), - vlanid, + niciraNvpHost.getDetail("gatewayserviceuuid"), vlanid, network.getBroadcastUri().getSchemeSpecificPart(), - "router-" + network.getDisplayText(), - publicCidr, - sourceNatIp.getGateway(), - internalCidr, - context.getDomain().getName() + "-" + context.getAccount().getAccountName()); - CreateLogicalRouterAnswer answer = - (CreateLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd); + "router-" + network.getDisplayText(), publicCidr, + sourceNatIp.getGateway(), internalCidr, context + .getDomain().getName() + + "-" + + context.getAccount().getAccountName()); + CreateLogicalRouterAnswer answer = (CreateLogicalRouterAnswer) _agentMgr + .easySend(niciraNvpHost.getId(), cmd); if (answer.getResult() == false) { - s_logger.error("Failed to create Logical Router for network " + network.getDisplayText()); + s_logger.error("Failed to create Logical Router for network " + + network.getDisplayText()); return false; } - + } - - } finally { if (lock != null) { _networkDao.releaseFromLockTable(lock.getId()); @@ -272,390 +293,486 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide } } return true; - } + } - @Override - public boolean prepare(Network network, NicProfile nic, - VirtualMachineProfile vm, - DeployDestination dest, ReservationContext context) - throws ConcurrentOperationException, ResourceUnavailableException, - InsufficientCapacityException { - - if (!canHandle(network, Service.Connectivity)) { - return false; - } + @Override + public boolean prepare(Network network, NicProfile nic, + VirtualMachineProfile vm, + DeployDestination dest, ReservationContext context) + throws ConcurrentOperationException, ResourceUnavailableException, + InsufficientCapacityException { - if (network.getBroadcastUri() == null) { - s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); - return false; - } + if (!canHandle(network, Service.Connectivity)) { + return false; + } - NicVO nicVO = _nicDao.findById(nic.getId()); + if (network.getBroadcastUri() == null) { + s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); + return false; + } - List devices = _niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); - if (devices.isEmpty()) { - s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); - return false; - } - NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); - HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); + NicVO nicVO = _nicDao.findById(nic.getId()); - 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()); - CreateLogicalSwitchPortAnswer answer = (CreateLogicalSwitchPortAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd); - - if (answer == null || !answer.getResult()) { - s_logger.error ("CreateLogicalSwitchPortCommand failed"); - return false; - } - - NiciraNvpNicMappingVO nicMap = new NiciraNvpNicMappingVO(network.getBroadcastUri().getSchemeSpecificPart(), answer.getLogicalSwitchPortUuid(), nicVO.getUuid()); - _niciraNvpNicMappingDao.persist(nicMap); + List devices = _niciraNvpDao + .listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); - return true; - } + 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); - @Override - public boolean release(Network network, NicProfile nic, - VirtualMachineProfile vm, - ReservationContext context) throws ConcurrentOperationException, - ResourceUnavailableException { + 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()); + } + } - if (!canHandle(network, Service.Connectivity)) { - return false; - } + CreateLogicalSwitchPortCommand cmd = new CreateLogicalSwitchPortCommand( + network.getBroadcastUri().getSchemeSpecificPart(), + nicVO.getUuid(), context.getDomain().getName() + "-" + + context.getAccount().getAccountName(), nic.getName()); + CreateLogicalSwitchPortAnswer answer = (CreateLogicalSwitchPortAnswer) _agentMgr + .easySend(niciraNvpHost.getId(), cmd); - if (network.getBroadcastUri() == null) { - s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); - return false; - } - - NicVO nicVO = _nicDao.findById(nic.getId()); + if (answer == null || !answer.getResult()) { + s_logger.error("CreateLogicalSwitchPortCommand failed"); + return false; + } - List devices = _niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); - if (devices.isEmpty()) { - s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId()); - return false; - } - NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); - HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); - - NiciraNvpNicMappingVO nicMap = _niciraNvpNicMappingDao.findByNicUuid(nicVO.getUuid()); - if (nicMap == null) { - 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); - - if (answer == null || !answer.getResult()) { - s_logger.error ("DeleteLogicalSwitchPortCommand failed"); - return false; - } - - _niciraNvpNicMappingDao.remove(nicMap.getId()); - - return true; - } + NiciraNvpNicMappingVO nicMap = new NiciraNvpNicMappingVO(network + .getBroadcastUri().getSchemeSpecificPart(), + answer.getLogicalSwitchPortUuid(), nicVO.getUuid()); + _niciraNvpNicMappingDao.persist(nicMap); - @Override - public boolean shutdown(Network network, ReservationContext context, - boolean cleanup) throws ConcurrentOperationException, - ResourceUnavailableException { - if (!canHandle(network, Service.Connectivity)) { - return false; - } + return true; + } - return true; - } + @Override + public boolean release(Network network, NicProfile nic, + VirtualMachineProfile vm, + ReservationContext context) throws ConcurrentOperationException, + ResourceUnavailableException { - @Override - public boolean destroy(Network network) - throws ConcurrentOperationException, ResourceUnavailableException { - if (!canHandle(network, Service.Connectivity)) { - return false; - } + if (!canHandle(network, Service.Connectivity)) { + return false; + } - return true; - } + if (network.getBroadcastUri() == null) { + s_logger.error("Nic has no broadcast Uri with the LSwitch Uuid"); + return false; + } - @Override - public boolean isReady(PhysicalNetworkServiceProvider provider) { - return true; - } + NicVO nicVO = _nicDao.findById(nic.getId()); - @Override - public boolean shutdownProviderInstances( - PhysicalNetworkServiceProvider provider, ReservationContext context) - throws ConcurrentOperationException, ResourceUnavailableException { - // Nothing to do here. - return true; - } + List devices = _niciraNvpDao + .listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); - @Override - public boolean canEnableIndividualServices() { - return true; - } + NiciraNvpNicMappingVO nicMap = _niciraNvpNicMappingDao + .findByNicUuid(nicVO.getUuid()); + if (nicMap == null) { + s_logger.error("No mapping for nic " + nic.getName()); + return false; + } - @Override - public boolean verifyServicesCombination(Set services) { - // This element can only function in a Nicra Nvp based - // SDN network, so Connectivity needs to be present here - if (services.contains(Service.Connectivity)) { - return true; - } - s_logger.debug("Unable to provide services without Connectivity service enabled for this element"); - return false; - } + DeleteLogicalSwitchPortCommand cmd = new DeleteLogicalSwitchPortCommand( + nicMap.getLogicalSwitchUuid(), + nicMap.getLogicalSwitchPortUuid()); + DeleteLogicalSwitchPortAnswer answer = (DeleteLogicalSwitchPortAnswer) _agentMgr + .easySend(niciraNvpHost.getId(), cmd); - private static Map> setCapabilities() { - Map> capabilities = new HashMap>(); + if (answer == null || !answer.getResult()) { + s_logger.error("DeleteLogicalSwitchPortCommand failed"); + return false; + } - // Basic L2 SDN provisioning - capabilities.put(Service.Connectivity, null); - - // L3 Support : SourceNat - capabilities.put(Service.Gateway, null); - Map sourceNatCapabilities = new HashMap(); - sourceNatCapabilities.put(Capability.SupportedSourceNatTypes, "peraccount"); - sourceNatCapabilities.put(Capability.RedundantRouter, "true"); - capabilities.put(Service.SourceNat, sourceNatCapabilities); - + _niciraNvpNicMappingDao.remove(nicMap.getId()); - return capabilities; - } + return true; + } - @Override - public String getPropertiesFile() { - return "nicira-nvp_commands.properties"; - } + @Override + public boolean shutdown(Network network, ReservationContext context, + boolean cleanup) throws ConcurrentOperationException, + ResourceUnavailableException { + if (!canHandle(network, Service.Connectivity)) { + return false; + } - @Override - @DB - public NiciraNvpDeviceVO addNiciraNvpDevice(AddNiciraNvpDeviceCmd cmd) { - ServerResource resource = new NiciraNvpResource(); - String deviceName = Network.Provider.NiciraNvp.getName(); - NetworkDevice networkDevice = NetworkDevice.getNetworkDevice(deviceName); - Long physicalNetworkId = cmd.getPhysicalNetworkId(); - NiciraNvpDeviceVO niciraNvpDevice = null; - - PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); - if (physicalNetwork == null) { - throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId); - } - long zoneId = physicalNetwork.getDataCenterId(); + List devices = _niciraNvpDao + .listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); - PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder()); - if (ntwkSvcProvider == null) { - throw new CloudRuntimeException("Network Service Provider: " + networkDevice.getNetworkServiceProvder() + - " is not enabled in the physical network: " + physicalNetworkId + "to add this device"); - } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) { - throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + - " is in shutdown state in the physical network: " + physicalNetworkId + "to add this device"); - } - - if (_niciraNvpDao.listByPhysicalNetwork(physicalNetworkId).size() != 0) { - throw new CloudRuntimeException("A NiciraNvp device is already configured on this physical network"); - } - - Map params = new HashMap(); - params.put("guid", UUID.randomUUID().toString()); - params.put("zoneId", String.valueOf(physicalNetwork.getDataCenterId())); - params.put("physicalNetworkId", String.valueOf(physicalNetwork.getId())); - params.put("name", "Nicira Controller - " + cmd.getHost()); - params.put("ip", cmd.getHost()); - params.put("adminuser", cmd.getUsername()); - params.put("adminpass", cmd.getPassword()); - params.put("transportzoneuuid", cmd.getTransportzoneUuid()); - params.put("transportzoneisotype", physicalNetwork.getIsolationMethods().get(0).toLowerCase()); // FIXME What to do with multiple isolation types + if (_networkManager.isProviderSupportServiceInNetwork(network.getId(), + Service.SourceNat, Provider.NiciraNvp)) { + s_logger.debug("Apparently we were providing SourceNat on this network"); - Map hostdetails = new HashMap(); - hostdetails.putAll(params); - - - Transaction txn = Transaction.currentTxn(); - try { - resource.configure(cmd.getHost(), hostdetails); - - Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params); - if (host != null) { - txn.start(); - - niciraNvpDevice = new NiciraNvpDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName); - _niciraNvpDao.persist(niciraNvpDevice); - - DetailVO detail = new DetailVO(host.getId(), "niciranvpdeviceid", String.valueOf(niciraNvpDevice.getId())); - _hostDetailsDao.persist(detail); + // Deleting the LogicalRouter will also take care of all provisioned + // nat rules. + /* + * DeleteLogicalRouterCommand cmd = new + * DeleteLogicalRouterCommand(""); DeleteLogicalRouterAnswer answer + * = (DeleteLogicalRouterAnswer) + * _agentMgr.easySend(niciraNvpHost.getId(), cmd); if + * (answer.getResult() == false) { + * s_logger.error("Failed to delete LogicalRouter for network " + + * network.getDisplayText()); return false; } + */} - txn.commit(); - return niciraNvpDevice; - } else { - throw new CloudRuntimeException("Failed to add Nicira Nvp Device due to internal error."); - } - } catch (ConfigurationException e) { - txn.rollback(); - throw new CloudRuntimeException(e.getMessage()); - } - } + return true; + } - @Override - public NiciraNvpDeviceResponse createNiciraNvpDeviceResponse( - NiciraNvpDeviceVO niciraNvpDeviceVO) { - NiciraNvpDeviceResponse response = new NiciraNvpDeviceResponse(); - response.setDeviceName(niciraNvpDeviceVO.getDeviceName()); - response.setPhysicalNetworkId(niciraNvpDeviceVO.getPhysicalNetworkId()); - response.setId(niciraNvpDeviceVO.getId()); - response.setProviderName(niciraNvpDeviceVO.getProviderName()); - return response; - } - - @Override - public boolean deleteNiciraNvpDevice(DeleteNiciraNvpDeviceCmd cmd) { - Long niciraDeviceId = cmd.getNiciraNvpDeviceId(); - NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao.findById(niciraDeviceId); - if (niciraNvpDevice == null) { - throw new InvalidParameterValueException("Could not find a nicira device with id " + niciraDeviceId); - } - - // Find the physical network we work for - Long physicalNetworkId = niciraNvpDevice.getPhysicalNetworkId(); - PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); - if (physicalNetwork != null) { - // Lets see if there are networks that use us - // Find the nicira networks on this physical network - List networkList = _networkDao.listByPhysicalNetwork(physicalNetworkId); - - // Networks with broadcast type lswitch are ours - for (NetworkVO network : networkList) { - if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.Lswitch) { - if ((network.getState() != Network.State.Shutdown) && (network.getState() != Network.State.Destroy)) { - throw new CloudRuntimeException("This Nicira Nvp device can not be deleted as there are one or more logical networks provisioned by cloudstack."); - } - } - } - } - - HostVO niciraHost = _hostDao.findById(niciraNvpDevice.getHostId()); - Long hostId = niciraHost.getId(); - - niciraHost.setResourceState(ResourceState.Maintenance); - _hostDao.update(hostId, niciraHost); - _resourceMgr.deleteHost(hostId, false, false); - - _niciraNvpDao.remove(niciraDeviceId); - - return true; - } - - @Override - public List listNiciraNvpDevices(ListNiciraNvpDevicesCmd cmd) { - Long physicalNetworkId = cmd.getPhysicalNetworkId(); - Long niciraNvpDeviceId = cmd.getNiciraNvpDeviceId(); - List responseList = new ArrayList(); - - if (physicalNetworkId == null && niciraNvpDeviceId == null) { - throw new InvalidParameterValueException("Either physical network Id or nicira device Id must be specified"); - } - - if (niciraNvpDeviceId != null) { - NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao.findById(niciraNvpDeviceId); - if (niciraNvpDevice == null) { - throw new InvalidParameterValueException("Could not find Nicira Nvp device with id: " + niciraNvpDevice); - } - responseList.add(niciraNvpDevice); - } - else { - PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); - if (physicalNetwork == null) { - throw new InvalidParameterValueException("Could not find a physical network with id: " + physicalNetworkId); - } - responseList = _niciraNvpDao.listByPhysicalNetwork(physicalNetworkId); - } - - return responseList; - } - - @Override - public List listNiciraNvpDeviceNetworks(ListNiciraNvpDeviceNetworksCmd cmd) { - Long niciraDeviceId = cmd.getNiciraNvpDeviceId(); - NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao.findById(niciraDeviceId); - if (niciraNvpDevice == null) { - throw new InvalidParameterValueException("Could not find a nicira device with id " + niciraDeviceId); - } - - // Find the physical network we work for - Long physicalNetworkId = niciraNvpDevice.getPhysicalNetworkId(); - PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); - if (physicalNetwork == null) { - // No such physical network, so no provisioned networks - return Collections.emptyList(); - } - - // Find the nicira networks on this physical network - List networkList = _networkDao.listByPhysicalNetwork(physicalNetworkId); - - // Networks with broadcast type lswitch are ours - List responseList = new ArrayList(); - for (NetworkVO network : networkList) { - if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.Lswitch) { - responseList.add(network); - } - } - - return responseList; - } - - @Override - public HostVO createHostVOForConnectedAgent(HostVO host, - StartupCommand[] cmd) { - // TODO Auto-generated method stub - return null; - } + @Override + public boolean destroy(Network network) + throws ConcurrentOperationException, ResourceUnavailableException { + if (!canHandle(network, Service.Connectivity)) { + return false; + } - @Override - public HostVO createHostVOForDirectConnectAgent(HostVO host, - StartupCommand[] startup, ServerResource resource, - Map details, List hostTags) { - if (!(startup[0] instanceof StartupNiciraNvpCommand)) { - return null; - } - host.setType(Host.Type.L2Networking); - return host; - } + return true; + } - @Override - public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, - boolean isForceDeleteStorage) throws UnableDeleteHostException { - if (!(host.getType() == Host.Type.L2Networking)) { - return null; - } - return new DeleteHostAnswer(true); - } + @Override + public boolean isReady(PhysicalNetworkServiceProvider provider) { + return true; + } - /** - * From interface SourceNatServiceProvider - */ + @Override + public boolean shutdownProviderInstances( + PhysicalNetworkServiceProvider provider, ReservationContext context) + throws ConcurrentOperationException, ResourceUnavailableException { + // Nothing to do here. + return true; + } + + @Override + public boolean canEnableIndividualServices() { + return true; + } + + @Override + public boolean verifyServicesCombination(Set services) { + // This element can only function in a Nicra Nvp based + // SDN network, so Connectivity needs to be present here + if (services.contains(Service.Connectivity)) { + return true; + } + s_logger.debug("Unable to provide services without Connectivity service enabled for this element"); + return false; + } + + private static Map> setCapabilities() { + Map> capabilities = new HashMap>(); + + // L2 Support : SDN provisioning + capabilities.put(Service.Connectivity, null); + + // L3 Support : Generic? + capabilities.put(Service.Gateway, null); + + // L3 Support : SourceNat + Map sourceNatCapabilities = new HashMap(); + sourceNatCapabilities.put(Capability.SupportedSourceNatTypes, + "peraccount"); + sourceNatCapabilities.put(Capability.RedundantRouter, "false"); + capabilities.put(Service.SourceNat, sourceNatCapabilities); + + // L3 Support : Port Forwarding + capabilities.put(Service.PortForwarding, null); + + // L3 support : StaticNat + capabilities.put(Service.StaticNat, null); + + return capabilities; + } + + @Override + public String getPropertiesFile() { + return "nicira-nvp_commands.properties"; + } + + @Override + @DB + public NiciraNvpDeviceVO addNiciraNvpDevice(AddNiciraNvpDeviceCmd cmd) { + ServerResource resource = new NiciraNvpResource(); + String deviceName = Network.Provider.NiciraNvp.getName(); + NetworkDevice networkDevice = NetworkDevice + .getNetworkDevice(deviceName); + Long physicalNetworkId = cmd.getPhysicalNetworkId(); + NiciraNvpDeviceVO niciraNvpDevice = null; + + PhysicalNetworkVO physicalNetwork = _physicalNetworkDao + .findById(physicalNetworkId); + if (physicalNetwork == null) { + throw new InvalidParameterValueException( + "Could not find phyical network with ID: " + + physicalNetworkId); + } + long zoneId = physicalNetwork.getDataCenterId(); + + PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao + .findByServiceProvider(physicalNetwork.getId(), + networkDevice.getNetworkServiceProvder()); + if (ntwkSvcProvider == null) { + throw new CloudRuntimeException("Network Service Provider: " + + networkDevice.getNetworkServiceProvder() + + " is not enabled in the physical network: " + + physicalNetworkId + "to add this device"); + } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) { + throw new CloudRuntimeException("Network Service Provider: " + + ntwkSvcProvider.getProviderName() + + " is in shutdown state in the physical network: " + + physicalNetworkId + "to add this device"); + } + + if (_niciraNvpDao.listByPhysicalNetwork(physicalNetworkId).size() != 0) { + throw new CloudRuntimeException( + "A NiciraNvp device is already configured on this physical network"); + } + + Map params = new HashMap(); + params.put("guid", UUID.randomUUID().toString()); + params.put("zoneId", String.valueOf(physicalNetwork.getDataCenterId())); + params.put("physicalNetworkId", String.valueOf(physicalNetwork.getId())); + params.put("name", "Nicira Controller - " + cmd.getHost()); + params.put("ip", cmd.getHost()); + params.put("adminuser", cmd.getUsername()); + params.put("adminpass", cmd.getPassword()); + params.put("transportzoneuuid", cmd.getTransportzoneUuid()); + // FIXME What to do with multiple isolation types + params.put("transportzoneisotype", + physicalNetwork.getIsolationMethods().get(0).toLowerCase()); + if (cmd.getL3GatewayServiceUuid() != null) { + params.put("l3gatewayserviceuuid", cmd.getL3GatewayServiceUuid()); + } + + Map hostdetails = new HashMap(); + hostdetails.putAll(params); + + Transaction txn = Transaction.currentTxn(); + try { + resource.configure(cmd.getHost(), hostdetails); + + Host host = _resourceMgr.addHost(zoneId, resource, + Host.Type.L2Networking, params); + if (host != null) { + txn.start(); + + niciraNvpDevice = new NiciraNvpDeviceVO(host.getId(), + physicalNetworkId, ntwkSvcProvider.getProviderName(), + deviceName); + _niciraNvpDao.persist(niciraNvpDevice); + + DetailVO detail = new DetailVO(host.getId(), + "niciranvpdeviceid", String.valueOf(niciraNvpDevice + .getId())); + _hostDetailsDao.persist(detail); + + txn.commit(); + return niciraNvpDevice; + } else { + throw new CloudRuntimeException( + "Failed to add Nicira Nvp Device due to internal error."); + } + } catch (ConfigurationException e) { + txn.rollback(); + throw new CloudRuntimeException(e.getMessage()); + } + } + + @Override + public NiciraNvpDeviceResponse createNiciraNvpDeviceResponse( + NiciraNvpDeviceVO niciraNvpDeviceVO) { + NiciraNvpDeviceResponse response = new NiciraNvpDeviceResponse(); + response.setDeviceName(niciraNvpDeviceVO.getDeviceName()); + response.setPhysicalNetworkId(niciraNvpDeviceVO.getPhysicalNetworkId()); + response.setId(niciraNvpDeviceVO.getId()); + response.setProviderName(niciraNvpDeviceVO.getProviderName()); + return response; + } + + @Override + public boolean deleteNiciraNvpDevice(DeleteNiciraNvpDeviceCmd cmd) { + Long niciraDeviceId = cmd.getNiciraNvpDeviceId(); + NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao + .findById(niciraDeviceId); + if (niciraNvpDevice == null) { + throw new InvalidParameterValueException( + "Could not find a nicira device with id " + niciraDeviceId); + } + + // Find the physical network we work for + Long physicalNetworkId = niciraNvpDevice.getPhysicalNetworkId(); + PhysicalNetworkVO physicalNetwork = _physicalNetworkDao + .findById(physicalNetworkId); + if (physicalNetwork != null) { + // Lets see if there are networks that use us + // Find the nicira networks on this physical network + List networkList = _networkDao + .listByPhysicalNetwork(physicalNetworkId); + + // Networks with broadcast type lswitch are ours + for (NetworkVO network : networkList) { + if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.Lswitch) { + if ((network.getState() != Network.State.Shutdown) + && (network.getState() != Network.State.Destroy)) { + throw new CloudRuntimeException( + "This Nicira Nvp device can not be deleted as there are one or more logical networks provisioned by cloudstack."); + } + } + } + } + + HostVO niciraHost = _hostDao.findById(niciraNvpDevice.getHostId()); + Long hostId = niciraHost.getId(); + + niciraHost.setResourceState(ResourceState.Maintenance); + _hostDao.update(hostId, niciraHost); + _resourceMgr.deleteHost(hostId, false, false); + + _niciraNvpDao.remove(niciraDeviceId); + + return true; + } + + @Override + public List listNiciraNvpDevices( + ListNiciraNvpDevicesCmd cmd) { + Long physicalNetworkId = cmd.getPhysicalNetworkId(); + Long niciraNvpDeviceId = cmd.getNiciraNvpDeviceId(); + List responseList = new ArrayList(); + + if (physicalNetworkId == null && niciraNvpDeviceId == null) { + throw new InvalidParameterValueException( + "Either physical network Id or nicira device Id must be specified"); + } + + if (niciraNvpDeviceId != null) { + NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao + .findById(niciraNvpDeviceId); + if (niciraNvpDevice == null) { + throw new InvalidParameterValueException( + "Could not find Nicira Nvp device with id: " + + niciraNvpDevice); + } + responseList.add(niciraNvpDevice); + } else { + PhysicalNetworkVO physicalNetwork = _physicalNetworkDao + .findById(physicalNetworkId); + if (physicalNetwork == null) { + throw new InvalidParameterValueException( + "Could not find a physical network with id: " + + physicalNetworkId); + } + responseList = _niciraNvpDao + .listByPhysicalNetwork(physicalNetworkId); + } + + return responseList; + } + + @Override + public List listNiciraNvpDeviceNetworks( + ListNiciraNvpDeviceNetworksCmd cmd) { + Long niciraDeviceId = cmd.getNiciraNvpDeviceId(); + NiciraNvpDeviceVO niciraNvpDevice = _niciraNvpDao + .findById(niciraDeviceId); + if (niciraNvpDevice == null) { + throw new InvalidParameterValueException( + "Could not find a nicira device with id " + niciraDeviceId); + } + + // Find the physical network we work for + Long physicalNetworkId = niciraNvpDevice.getPhysicalNetworkId(); + PhysicalNetworkVO physicalNetwork = _physicalNetworkDao + .findById(physicalNetworkId); + if (physicalNetwork == null) { + // No such physical network, so no provisioned networks + return Collections.emptyList(); + } + + // Find the nicira networks on this physical network + List networkList = _networkDao + .listByPhysicalNetwork(physicalNetworkId); + + // Networks with broadcast type lswitch are ours + List responseList = new ArrayList(); + for (NetworkVO network : networkList) { + if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.Lswitch) { + responseList.add(network); + } + } + + return responseList; + } + + @Override + public HostVO createHostVOForConnectedAgent(HostVO host, + StartupCommand[] cmd) { + // TODO Auto-generated method stub + return null; + } + + @Override + public HostVO createHostVOForDirectConnectAgent(HostVO host, + StartupCommand[] startup, ServerResource resource, + Map details, List hostTags) { + if (!(startup[0] instanceof StartupNiciraNvpCommand)) { + return null; + } + host.setType(Host.Type.L2Networking); + return host; + } + + @Override + public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, + boolean isForceDeleteStorage) throws UnableDeleteHostException { + if (!(host.getType() == Host.Type.L2Networking)) { + return null; + } + return new DeleteHostAnswer(true); + } + + /** + * From interface SourceNatServiceProvider + */ @Override public IpDeployer getIpDeployer(Network network) { return this; @@ -663,6 +780,7 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide /** * From interface IpDeployer + * * @param network * @param ipAddress * @param services @@ -673,6 +791,30 @@ public class NiciraNvpElement extends AdapterBase implements ConnectivityProvide public boolean applyIps(Network network, List ipAddress, Set services) throws ResourceUnavailableException { + s_logger.debug("Entering applyIps"); // TODO Remove this line + // TODO Auto-generated method stub + return false; + } + + /** + * From interface StaticNatServiceProvider + */ + @Override + public boolean applyStaticNats(Network config, + List rules) + throws ResourceUnavailableException { + s_logger.debug("Entering applyStaticNats"); // TODO Remove this line + // TODO Auto-generated method stub + return false; + } + + /** + * From interface PortForwardingServiceProvider + */ + @Override + public boolean applyPFRules(Network network, List rules) + throws ResourceUnavailableException { + s_logger.debug("Entering applyPFRules"); // TODO Remove this line // TODO Auto-generated method stub return false; } 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 4aa6ff18798..817c6306f0d 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 @@ -33,6 +33,8 @@ import com.cloud.agent.api.CreateLogicalSwitchAnswer; import com.cloud.agent.api.CreateLogicalSwitchCommand; import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; import com.cloud.agent.api.CreateLogicalSwitchPortCommand; +import com.cloud.agent.api.DeleteLogicalRouterAnswer; +import com.cloud.agent.api.DeleteLogicalRouterCommand; import com.cloud.agent.api.DeleteLogicalSwitchAnswer; import com.cloud.agent.api.DeleteLogicalSwitchCommand; import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer; @@ -211,7 +213,10 @@ public class NiciraNvpResource implements ServerResource { return executeRequest((FindLogicalSwitchPortCommand) cmd, numRetries); } else if (cmd instanceof CreateLogicalRouterCommand) { - return executeRequest((CreateLogicalRouterCommand) cmd, 0); //TODO set to numRetries when done + return executeRequest((CreateLogicalRouterCommand) cmd, numRetries); + } + else if (cmd instanceof DeleteLogicalRouterCommand) { + return executeRequest((DeleteLogicalRouterCommand) cmd, numRetries); } s_logger.debug("Received unsupported command " + cmd.toString()); return Answer.createUnsupportedCommandAnswer(cmd); @@ -385,7 +390,7 @@ public class NiciraNvpResource implements ServerResource { // Create the outside port for the router LogicalRouterPort lrpo = new LogicalRouterPort(); lrpo.setAdminStatusEnabled(true); - lrpo.setDisplayName(routerName + "-port"); + lrpo.setDisplayName(routerName + "-outside-port"); lrpo.setTags(tags); List outsideIpAddresses = new ArrayList(); outsideIpAddresses.add(publicNetworkIpAddress); @@ -402,7 +407,7 @@ public class NiciraNvpResource implements ServerResource { // Create the inside port for the router LogicalRouterPort lrpi = new LogicalRouterPort(); lrpi.setAdminStatusEnabled(true); - lrpi.setDisplayName(routerName + "-port"); + lrpi.setDisplayName(routerName + "-inside-port"); lrpi.setTags(tags); List insideIpAddresses = new ArrayList(); insideIpAddresses.add(internalNetworkAddress); @@ -410,7 +415,7 @@ public class NiciraNvpResource implements ServerResource { lrpi = _niciraNvpApi.createLogicalRouterPort(lrc.getUuid(),lrpi); // Create the inside port on the lswitch - LogicalSwitchPort lsp = new LogicalSwitchPort(routerName + "-port", tags, true); + LogicalSwitchPort lsp = new LogicalSwitchPort(routerName + "-inside-port", tags, true); lsp = _niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp); // Attach the inside router port to the lswitch port with a PatchAttachment @@ -449,6 +454,20 @@ public class NiciraNvpResource implements ServerResource { } } + private Answer executeRequest(DeleteLogicalRouterCommand cmd, int numRetries) { + try { + _niciraNvpApi.deleteLogicalRouter(cmd.getLogicalRouterUuid()); + return new DeleteLogicalRouterAnswer(cmd, true, "Logical Router deleted (uuid " + cmd.getLogicalRouterUuid() + ")"); + } catch (NiciraNvpApiException e) { + if (numRetries > 0) { + return retry(cmd, --numRetries); + } + else { + return new DeleteLogicalRouterAnswer(cmd, e); + } + } + } + private Answer executeRequest(ReadyCommand cmd) { return new ReadyAnswer(cmd); } From af77912034389c8e57246b57c6d11f6d6217b60d Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 27 Sep 2012 18:26:56 -0700 Subject: [PATCH 04/15] Keep naming consistent --- .../src/com/cloud/network/element/NiciraNvpElement.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 161f3dc917e..d781d033769 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 @@ -266,7 +266,7 @@ public class NiciraNvpElement extends AdapterBase implements : Long.parseLong(sourceNatIp.getVlanTag()); CreateLogicalRouterCommand cmd = new CreateLogicalRouterCommand( - niciraNvpHost.getDetail("gatewayserviceuuid"), vlanid, + niciraNvpHost.getDetail("l3gatewayserviceuuid"), vlanid, network.getBroadcastUri().getSchemeSpecificPart(), "router-" + network.getDisplayText(), publicCidr, sourceNatIp.getGateway(), internalCidr, context From dd68a0033858a03138d3e9d035843afb4d4d65a9 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 27 Sep 2012 22:16:11 -0700 Subject: [PATCH 05/15] Persist the uuid of the logical router once created --- client/tomcatconf/components.xml.in | 1 + .../network/NiciraNvpRouterMappingVO.java | 79 +++++++++++++++++++ .../dao/NiciraNvpRouterMappingDao.java | 9 +++ .../dao/NiciraNvpRouterMappingDaoImpl.java | 30 +++++++ .../network/element/NiciraNvpElement.java | 36 ++++++--- setup/db/create-schema.sql | 9 +++ 6 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/NiciraNvpRouterMappingVO.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java diff --git a/client/tomcatconf/components.xml.in b/client/tomcatconf/components.xml.in index 5730b832075..2953eb781a6 100755 --- a/client/tomcatconf/components.xml.in +++ b/client/tomcatconf/components.xml.in @@ -179,6 +179,7 @@ under the License. + diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/NiciraNvpRouterMappingVO.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/NiciraNvpRouterMappingVO.java new file mode 100644 index 00000000000..037ba699fc0 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/NiciraNvpRouterMappingVO.java @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="nicira_nvp_router_map") +public class NiciraNvpRouterMappingVO { + //FIXME the ddl for this table should be in one of the upgrade scripts + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="id") + private long id; + + @Column(name="logicalrouter_uuid") + private String logicalRouterUuid; + + @Column(name="network_id") + private long networkId; + + public NiciraNvpRouterMappingVO() { + } + + public NiciraNvpRouterMappingVO(String logicalRouterUuid, long networkId) { + this.logicalRouterUuid = logicalRouterUuid; + this.networkId = networkId; + } + + public NiciraNvpRouterMappingVO(long id, String logicalRouterUuid, long networkId) { + this.id = id; + this.logicalRouterUuid = logicalRouterUuid; + this.networkId = networkId; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getLogicalRouterUuid() { + return logicalRouterUuid; + } + + public void setLogicalRouterUuid(String logicalRouterUuid) { + this.logicalRouterUuid = logicalRouterUuid; + } + + public long getNetworkId() { + return networkId; + } + + public void setNetworkId(long networkId) { + this.networkId = networkId; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java new file mode 100644 index 00000000000..6fae52eb773 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java @@ -0,0 +1,9 @@ +package com.cloud.network.dao; + +import com.cloud.network.NiciraNvpRouterMappingVO; +import com.cloud.utils.db.GenericDao; + +public interface NiciraNvpRouterMappingDao extends GenericDao { + + public NiciraNvpRouterMappingVO findByNetworkIdI(long id); +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java new file mode 100644 index 00000000000..303b7608f31 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java @@ -0,0 +1,30 @@ +package com.cloud.network.dao; + +import javax.ejb.Local; + +import com.cloud.network.NiciraNvpRouterMappingVO; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.SearchCriteria.Op; + +@Local(value=NiciraNvpRouterMappingDao.class) +public class NiciraNvpRouterMappingDaoImpl extends GenericDaoBase implements NiciraNvpRouterMappingDao { + + protected final SearchBuilder networkSearch; + + public NiciraNvpRouterMappingDaoImpl() { + networkSearch = createSearchBuilder(); + networkSearch.and("network_id", networkSearch.entity().getNetworkId(), Op.EQ); + networkSearch.done(); + } + + @Override + public NiciraNvpRouterMappingVO findByNetworkIdI(long id) { + SearchCriteria sc = networkSearch.create(); + sc.setParameters("network_id", id); + return findOneBy(sc); + } + + +} 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 d781d033769..9a220dc3f8e 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 @@ -90,6 +90,7 @@ import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.NetworkManager; import com.cloud.network.NiciraNvpDeviceVO; import com.cloud.network.NiciraNvpNicMappingVO; +import com.cloud.network.NiciraNvpRouterMappingVO; import com.cloud.network.PhysicalNetworkServiceProvider; import com.cloud.network.PhysicalNetworkVO; import com.cloud.network.PublicIpAddress; @@ -98,6 +99,7 @@ import com.cloud.network.dao.NetworkDao; import com.cloud.network.dao.NetworkServiceMapDao; import com.cloud.network.dao.NiciraNvpDao; import com.cloud.network.dao.NiciraNvpNicMappingDao; +import com.cloud.network.dao.NiciraNvpRouterMappingDao; import com.cloud.network.dao.PhysicalNetworkDao; import com.cloud.network.dao.PhysicalNetworkServiceProviderDao; import com.cloud.network.dao.PhysicalNetworkServiceProviderVO; @@ -154,6 +156,8 @@ public class NiciraNvpElement extends AdapterBase implements @Inject NiciraNvpNicMappingDao _niciraNvpNicMappingDao; @Inject + NiciraNvpRouterMappingDao _niciraNvpRouterMappingDao; + @Inject NetworkDao _networkDao; @Inject NetworkManager _networkManager; @@ -280,7 +284,11 @@ public class NiciraNvpElement extends AdapterBase implements + network.getDisplayText()); return false; } - + + // Store the uuid so we can easily find it during cleanup + NiciraNvpRouterMappingVO routermapping = + new NiciraNvpRouterMappingVO(cmd.getLogicalSwitchUuid(), network.getId()); + _niciraNvpRouterMappingDao.persist(routermapping); } } finally { if (lock != null) { @@ -446,15 +454,23 @@ public class NiciraNvpElement extends AdapterBase implements // Deleting the LogicalRouter will also take care of all provisioned // nat rules. - /* - * DeleteLogicalRouterCommand cmd = new - * DeleteLogicalRouterCommand(""); DeleteLogicalRouterAnswer answer - * = (DeleteLogicalRouterAnswer) - * _agentMgr.easySend(niciraNvpHost.getId(), cmd); if - * (answer.getResult() == false) { - * s_logger.error("Failed to delete LogicalRouter for network " + - * network.getDisplayText()); return false; } - */} + NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao + .findByNetworkIdI(network.getId()); + if (routermapping == null) { + s_logger.error("No logical router uuid found for network " + + network.getDisplayText()); + return false; + } + + DeleteLogicalRouterCommand cmd = new DeleteLogicalRouterCommand(routermapping.getLogicalRouterUuid()); + DeleteLogicalRouterAnswer answer = + (DeleteLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd); + if (answer.getResult() == false) { + s_logger.error("Failed to delete LogicalRouter for network " + + network.getDisplayText()); + return false; + } + } return true; } diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 5b6dc0447c7..53e5f08aee8 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -146,6 +146,7 @@ DROP TABLE IF EXISTS `cloud`.`s2s_vpn_gateway`; DROP TABLE IF EXISTS `cloud`.`s2s_vpn_connection`; DROP TABLE IF EXISTS `cloud`,`external_nicira_nvp_devices`; DROP TABLE IF EXISTS `cloud`,`nicira_nvp_nic_map`; +DROP TABLE IF EXISTS `cloud`,`nicira_nvp_router_map`; CREATE TABLE `cloud`.`version` ( `id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id', @@ -2370,5 +2371,13 @@ CREATE TABLE `cloud`.`nicira_nvp_nic_map` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `cloud`.`nicira_nvp_router_map` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + `logicalrouter_uuid` varchar(255) NOT NULL UNIQUE COMMENT 'nicira uuid of logical router', + `network_id` bigint unsigned NOT NULL UNIQUE COMMENT 'cloudstack id of the network', + PRIMARY KEY (`id`), + CONSTRAINT `fk_nicira_nvp_router_map__network_id` FOREIGN KEY (`network_id`) REFERENCES `networks`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + SET foreign_key_checks = 1; From de41cb7cb5ee0fb128c81fb5b00f3eb537e4eec2 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 28 Sep 2012 11:32:53 -0700 Subject: [PATCH 06/15] Start implementing IpDeployer --- .../api/AssignIpToLogicalRouterAnswer.java | 35 +++++++ .../api/AssignIpToLogicalRouterCommand.java | 98 +++++++++++++++++++ .../network/element/NiciraNvpElement.java | 46 ++++++--- .../cloud/network/nicira/NiciraNvpApi.java | 24 ++++- .../cloud/network/nicira/NiciraNvpList.java | 10 +- .../network/resource/NiciraNvpResource.java | 76 +++++++++++++- 6 files changed, 271 insertions(+), 18 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java create mode 100644 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/AssignIpToLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java new file mode 100644 index 00000000000..1eda213eb11 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java @@ -0,0 +1,35 @@ +// 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); + } + +} 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 new file mode 100644 index 00000000000..98c36a83d2e --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java @@ -0,0 +1,98 @@ +// 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; + } + +} 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 9a220dc3f8e..7a0b2686b8f 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 @@ -256,6 +256,10 @@ public class NiciraNvpElement extends AdapterBase implements + network.getId()); } try { + // FIXME Check if any services other than connectiviy are required + // If that is the case start the logical router with only the + // internal interface, leave the external interfaces to the + // IpDeployer if (_networkManager.isProviderSupportServiceInNetwork( network.getId(), Service.SourceNat, Provider.NiciraNvp)) { s_logger.debug("Apparently we are supposed to provide SourceNat on this network"); @@ -457,9 +461,10 @@ public class NiciraNvpElement extends AdapterBase implements NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao .findByNetworkIdI(network.getId()); if (routermapping == null) { - s_logger.error("No logical router uuid found for network " + s_logger.warn("No logical router uuid found for network " + network.getDisplayText()); - return false; + // This might be cause by a failed deployment, so don't make shutdown fail as well. + return true; } DeleteLogicalRouterCommand cmd = new DeleteLogicalRouterCommand(routermapping.getLogicalRouterUuid()); @@ -470,6 +475,8 @@ public class NiciraNvpElement extends AdapterBase implements + network.getDisplayText()); return false; } + + _niciraNvpRouterMappingDao.remove(routermapping.getId()); } return true; @@ -507,11 +514,15 @@ public class NiciraNvpElement extends AdapterBase implements public boolean verifyServicesCombination(Set services) { // This element can only function in a Nicra Nvp based // SDN network, so Connectivity needs to be present here - if (services.contains(Service.Connectivity)) { - return true; + if (!services.contains(Service.Connectivity)) { + s_logger.warn("Unable to provide services without Connectivity service enabled for this element"); + return false; } - s_logger.debug("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"); + return false; + } + return true; } private static Map> setCapabilities() { @@ -808,8 +819,14 @@ public class NiciraNvpElement extends AdapterBase implements List ipAddress, Set services) throws ResourceUnavailableException { s_logger.debug("Entering applyIps"); // TODO Remove this line - // TODO Auto-generated method stub - return false; + + // The Nicira Nvp L3 Gateway doesn't need the addresses configured on + // the router interface. Only configure the CIDR and individual ip + // addresses become available when DNAT rules are created for them. + // The cidr setup is done during implementation of the logical router + // in the function implement + + return true; } /** @@ -819,9 +836,12 @@ public class NiciraNvpElement extends AdapterBase implements public boolean applyStaticNats(Network config, List rules) throws ResourceUnavailableException { + // FIXME Implement this s_logger.debug("Entering applyStaticNats"); // TODO Remove this line - // TODO Auto-generated method stub - return false; + for (StaticNat rule : rules) { + s_logger.debug ("StaticNat rule : from " + rule.getSourceIpAddressId() + " to " + rule.getDestIpAddress() + (rule.isForRevoke() ? " for revoke" : "")); + } + return true; } /** @@ -830,8 +850,12 @@ public class NiciraNvpElement extends AdapterBase implements @Override public boolean applyPFRules(Network network, List rules) throws ResourceUnavailableException { + // FIXME Implement this s_logger.debug("Entering applyPFRules"); // TODO Remove this line - // TODO Auto-generated method stub + for (PortForwardingRule rule : rules) { + s_logger.debug ("PortForwardingRule rule : from " + rule.getSourceIpAddressId() + + " to " + rule.getDestinationIpAddress().addr() + " port " + rule.getDestinationPortStart() + "-" + rule.getDestinationPortEnd()); + } return false; } 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 1c7b74155d3..a04dd1e42c1 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 @@ -181,7 +181,7 @@ public class NiciraNvpApi { NiciraNvpList lspl = executeRetrieveObject(new TypeToken>(){}.getType(), uri, params); - if (lspl == null || lspl.getResult_count() != 1) { + if (lspl == null || lspl.getResultCount() != 1) { throw new NiciraNvpApiException("Unexpected response from API"); } @@ -262,6 +262,28 @@ public class NiciraNvpApi { executeDeleteObject(uri); } + public NiciraNvpList findLogicalRouterPortByGatewayServiceAndVlanId(String logicalRouterUuid, String gatewayServiceUuid, long vlanId) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport"; + Map params = new HashMap(); + params.put("attachment_gwsvc_uuid", gatewayServiceUuid); + params.put("attachment_vlan", "0"); + params.put("fields","*"); + + return executeRetrieveObject(new TypeToken>(){}.getType(), uri, params); + } + + public LogicalRouterConfig findOneLogicalRouterByUuid(String logicalRouterUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid; + + return executeRetrieveObject(new TypeToken(){}.getType(), uri, Collections.emptyMap()); + } + + public void updateLogicalRouterPortConfig(String logicalRouterUuid, LogicalRouterPort logicalRouterPort) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport" + logicalRouterPort.getUuid(); + + executeUpdateObject(logicalRouterPort, uri, Collections.emptyMap()); + } + private void executeUpdateObject(T newObject, String uri, Map parameters) throws NiciraNvpApiException { String url; try { diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpList.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpList.java index 13d352daba3..c97e40ecd81 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpList.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpList.java @@ -30,12 +30,16 @@ public class NiciraNvpList { this.results = results; } - public int getResult_count() { + public int getResultCount() { return result_count; } - public void setResult_count(int result_count) { + public void setResultCount(int result_count) { this.result_count = result_count; - } + } + + public boolean isEmpty() { + return result_count == 0; + } } 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 817c6306f0d..96ed8564711 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,6 +26,8 @@ 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.CreateLogicalRouterAnswer; import com.cloud.agent.api.CreateLogicalRouterCommand; @@ -218,6 +220,9 @@ 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); + } s_logger.debug("Received unsupported command " + cmd.toString()); return Answer.createUnsupportedCommandAnswer(cmd); } @@ -345,7 +350,7 @@ public class NiciraNvpResource implements ServerResource { try { NiciraNvpList ports = _niciraNvpApi.findLogicalSwitchPortsByUuid(logicalSwitchUuid, logicalSwitchPortUuid); - if (ports.getResult_count() == 0) { + if (ports.getResultCount() == 0) { return new FindLogicalSwitchPortAnswer(cmd, false, "Logical switchport " + logicalSwitchPortUuid + " not found", null); } else { @@ -434,13 +439,15 @@ public class NiciraNvpResource implements ServerResource { match.setSourceIpAddresses(internalNetworkAddress); snr.setMatch(match); _niciraNvpApi.createLogicalRouterNatRule(lrc.getUuid(), snr); - } finally { + } catch (NiciraNvpApiException e) { // We need to destroy the router if we already created it // this will also take care of any router ports // TODO Clean up the switchport try { _niciraNvpApi.deleteLogicalRouter(lrc.getUuid()); - } catch (NiciraNvpApiException e) {} + } catch (NiciraNvpApiException ex) {} + + throw e; } return new CreateLogicalRouterAnswer(cmd, true, "Logical Router created (uuid " + lrc.getUuid() + ")", lrc.getUuid()); @@ -468,6 +475,69 @@ public class NiciraNvpResource implements ServerResource { } } + private Answer executeRequest(AssignIpToLogicalRouterCommand cmd, int numRetries) { + try { + LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid()); + + NiciraNvpList 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 outsideIpAddresses = new ArrayList(); + 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; + } + } + } + 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"); + } + } catch (NiciraNvpApiException e) { + if (numRetries > 0) { + return retry(cmd, --numRetries); + } + else { + return new DeleteLogicalRouterAnswer(cmd, e); + } + } + + } + private Answer executeRequest(ReadyCommand cmd) { return new ReadyAnswer(cmd); } From d4d82fa3f7f8e949cf70054f167bbc63a693c895 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 28 Sep 2012 11:33:14 -0700 Subject: [PATCH 07/15] This name makes more sense --- ui/scripts/configuration.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/scripts/configuration.js b/ui/scripts/configuration.js index 96b6fb9ab83..c6bc649c2df 100644 --- a/ui/scripts/configuration.js +++ b/ui/scripts/configuration.js @@ -1331,6 +1331,7 @@ case 'PortForwarding': serviceDisplayName = 'Port Forwarding'; break; case 'SecurityGroup': serviceDisplayName = 'Security Groups'; break; case 'UserData': serviceDisplayName = 'User Data'; break; + case 'Connectivity': serviceDisplayName = 'Virtual Networking'; break; default: serviceDisplayName = serviceName; break; } From c5634b96873ec69c43c210ed172855c9d9b2ff7d Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Sun, 30 Sep 2012 10:20:54 -0700 Subject: [PATCH 08/15] Wire the PortForwarding and StaticNat calls --- .../api/AssignIpToLogicalRouterCommand.java | 98 ----------------- ...ForwardingRulesOnLogicalRouterAnswer.java} | 23 ++-- ...ForwardingRulesOnLogicalRouterCommand.java | 61 +++++++++++ ...reStaticNatRulesOnLogicalRouterAnswer.java | 43 ++++++++ ...eStaticNatRulesOnLogicalRouterCommand.java | 64 +++++++++++ .../agent/api/CreateLogicalRouterAnswer.java | 2 +- .../network/element/NiciraNvpElement.java | 102 +++++++++++++++--- .../network/nicira/DestinationNatRule.java | 49 +-------- .../src/com/cloud/network/nicira/NatRule.java | 89 ++++++++++++++- .../cloud/network/nicira/NiciraNvpApi.java | 6 ++ .../cloud/network/nicira/SourceNatRule.java | 58 +--------- .../network/resource/NiciraNvpResource.java | 95 ++++++++-------- 12 files changed, 409 insertions(+), 281 deletions(-) delete mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java rename plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/{AssignIpToLogicalRouterAnswer.java => ConfigurePortForwardingRulesOnLogicalRouterAnswer.java} (70%) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterCommand.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigureStaticNatRulesOnLogicalRouterAnswer.java create mode 100644 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/AssignIpToLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterCommand.java deleted file mode 100644 index 98c36a83d2e..00000000000 --- 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; - } - -} 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/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java similarity index 70% rename from plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java rename to plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java index 1eda213eb11..40bde6c6e74 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/AssignIpToLogicalRouterAnswer.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePortForwardingRulesOnLogicalRouterAnswer.java @@ -16,20 +16,19 @@ // 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); - } +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); + } } 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 00000000000..1d57e35a95c --- /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 rules; + + public ConfigurePortForwardingRulesOnLogicalRouterCommand(String logicalRouterUuid, List rules) { + this.logicalRouterUuid = logicalRouterUuid; + this.rules = rules; + } + + public String getLogicalRouterUuid() { + return logicalRouterUuid; + } + + public void setLogicalRouterUuid(String logicalRouterUuid) { + this.logicalRouterUuid = logicalRouterUuid; + } + + public List getRules() { + return rules; + } + + public void setRules(List rules) { + this.rules = rules; + } + + /* (non-Javadoc) + * @see com.cloud.agent.api.Command#executeInSequence() + */ + @Override + public boolean executeInSequence() { + // TODO Auto-generated method stub + return false; + } + +} 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 00000000000..463dd4628cf --- /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); + } + +} 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 00000000000..4e5d37cb417 --- /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 rules; + + public ConfigureStaticNatRulesOnLogicalRouterCommand( + String logicalRouterUuid, List rules) { + super(); + this.logicalRouterUuid = logicalRouterUuid; + this.rules = rules; + + } + + public String getLogicalRouterUuid() { + return logicalRouterUuid; + } + + public void setLogicalRouterUuid(String logicalRouterUuid) { + this.logicalRouterUuid = logicalRouterUuid; + } + + public List getRules() { + return rules; + } + + public void setRules(List rules) { + this.rules = rules; + } + + /* (non-Javadoc) + * @see com.cloud.agent.api.Command#executeInSequence() + */ + @Override + public boolean executeInSequence() { + // TODO Auto-generated method stub + return false; + } + +} 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 3337e92cf79..4a09e449af8 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; } 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 7a0b2686b8f..716cd7808f0 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 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 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 staticNatRules = new ArrayList(); + 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 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 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 portForwardingRules = new ArrayList(); + 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(); } } 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 2cbdace3115..b1ef12f03b2 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"; } } 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 30196f0e73a..82a33787faf 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; + } } 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 a04dd1e42c1..fe4deec0626 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.emptyMap()); } + public NiciraNvpList findNatRulesByLogicalRouterUuid(String logicalRouterUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat"; + + return executeRetrieveObject(new TypeToken>(){}.getType(), uri, Collections.emptyMap()); + } + private void executeUpdateObject(T newObject, String uri, Map parameters) throws NiciraNvpApiException { String url; try { 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 7d1c13d9fc5..acbf21e18af 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"; + } } 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 96ed8564711..0908101a28a 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 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 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 outsideIpAddresses = new ArrayList(); - 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); } } From c9b0b0b1a9510f7740ca2c1de8961ad25b2d023f Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Sun, 30 Sep 2012 23:33:37 -0700 Subject: [PATCH 09/15] Add Nicira configuration to the UI --- api/src/com/cloud/event/EventTypes.java | 5 + .../api/commands/AddNiciraNvpDeviceCmd.java | 12 +- .../commands/DeleteNiciraNvpDeviceCmd.java | 16 +- .../api/commands/ListNiciraNvpDevicesCmd.java | 2 +- .../api/response/NiciraNvpDeviceResponse.java | 33 +- .../network/element/NiciraNvpElement.java | 8 +- .../network/resource/NiciraNvpResource.java | 3 +- ui/scripts/system.js | 418 +++++++++++++++++- 8 files changed, 486 insertions(+), 11 deletions(-) diff --git a/api/src/com/cloud/event/EventTypes.java b/api/src/com/cloud/event/EventTypes.java index e84a40379ad..9e6b36b780b 100755 --- a/api/src/com/cloud/event/EventTypes.java +++ b/api/src/com/cloud/event/EventTypes.java @@ -291,4 +291,9 @@ public class EventTypes { public static final String EVENT_TAGS_CREATE = "CREATE_TAGS"; public static final String EVENT_TAGS_DELETE = "DELETE_TAGS"; + // external network device events + public static final String EVENT_EXTERNAL_NVP_CONTROLLER_ADD = "PHYSICAL.NVPCONTROLLER.ADD"; + public static final String EVENT_EXTERNAL_NVP_CONTROLLER_DELETE = "PHYSICAL.NVPCONTROLLER.DELETE"; + public static final String EVENT_EXTERNAL_NVP_CONTROLLER_CONFIGURE = "PHYSICAL.NVPCONTROLLER.CONFIGURE"; + } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java index 1f497c6a2c5..7744b4f7148 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java @@ -42,7 +42,7 @@ import com.cloud.user.UserContext; import com.cloud.utils.exception.CloudRuntimeException; @Implementation(responseObject=NiciraNvpDeviceResponse.class, description="Adds a Nicira NVP device") -public class AddNiciraNvpDeviceCmd extends BaseCmd { +public class AddNiciraNvpDeviceCmd extends BaseAsyncCmd { private static final Logger s_logger = Logger.getLogger(AddNiciraNvpDeviceCmd.class.getName()); private static final String s_name = "addniciranvpdevice"; @PlugService NiciraNvpElementService _niciraNvpElementService; @@ -130,4 +130,14 @@ public class AddNiciraNvpDeviceCmd extends BaseCmd { public long getEntityOwnerId() { return UserContext.current().getCaller().getId(); } + + @Override + public String getEventType() { + return EventTypes.EVENT_EXTERNAL_NVP_CONTROLLER_ADD; + } + + @Override + public String getEventDescription() { + return "Adding a Nicira Nvp Controller"; + } } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/DeleteNiciraNvpDeviceCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/DeleteNiciraNvpDeviceCmd.java index 01a39466fd3..9cc8d626bb5 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/DeleteNiciraNvpDeviceCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/DeleteNiciraNvpDeviceCmd.java @@ -19,6 +19,7 @@ package com.cloud.api.commands; import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; +import com.cloud.api.BaseAsyncCmd; import com.cloud.api.BaseCmd; import com.cloud.api.IdentityMapper; import com.cloud.api.Implementation; @@ -26,6 +27,7 @@ import com.cloud.api.Parameter; import com.cloud.api.PlugService; import com.cloud.api.ServerApiException; import com.cloud.api.response.SuccessResponse; +import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InvalidParameterValueException; @@ -36,9 +38,9 @@ import com.cloud.user.UserContext; import com.cloud.utils.exception.CloudRuntimeException; @Implementation(responseObject=SuccessResponse.class, description=" delete a nicira nvp device") -public class DeleteNiciraNvpDeviceCmd extends BaseCmd { +public class DeleteNiciraNvpDeviceCmd extends BaseAsyncCmd { private static final Logger s_logger = Logger.getLogger(DeleteNiciraNvpDeviceCmd.class.getName()); - private static final String s_name = "addniciranvpdevice"; + private static final String s_name = "deleteniciranvpdeviceresponse"; @PlugService NiciraNvpElementService _niciraNvpElementService; ///////////////////////////////////////////////////// @@ -89,4 +91,14 @@ public class DeleteNiciraNvpDeviceCmd extends BaseCmd { return UserContext.current().getCaller().getId(); } + @Override + public String getEventType() { + return EventTypes.EVENT_EXTERNAL_LB_DEVICE_DELETE; + } + + @Override + public String getEventDescription() { + return "Deleting Nicira Nvp Controller"; + } + } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/ListNiciraNvpDevicesCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/ListNiciraNvpDevicesCmd.java index f9c157d9feb..f38cb16c8e9 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/ListNiciraNvpDevicesCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/ListNiciraNvpDevicesCmd.java @@ -43,7 +43,7 @@ import com.cloud.utils.exception.CloudRuntimeException; @Implementation(responseObject=NiciraNvpDeviceResponse.class, description="Lists Nicira NVP devices") public class ListNiciraNvpDevicesCmd extends BaseListCmd { private static final Logger s_logger = Logger.getLogger(ListNiciraNvpDevicesCmd.class.getName()); - private static final String s_name = "listniciranvpdevices"; + private static final String s_name = "listniciranvpdeviceresponse"; @PlugService NiciraNvpElementService _niciraNvpElementService; ///////////////////////////////////////////////////// diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/response/NiciraNvpDeviceResponse.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/response/NiciraNvpDeviceResponse.java index d5c82b34a87..e50be813879 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/response/NiciraNvpDeviceResponse.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/response/NiciraNvpDeviceResponse.java @@ -33,7 +33,16 @@ public class NiciraNvpDeviceResponse extends BaseResponse { @SerializedName(ApiConstants.NICIRA_NVP_DEVICE_NAME) @Param(description="device name") private String deviceName; - + + @SerializedName(ApiConstants.HOST_NAME) @Param(description="the controller Ip address") + private String hostName; + + @SerializedName(ApiConstants.NICIRA_NVP_TRANSPORT_ZONE_UUID) @Param(description="the transport zone Uuid") + private String transportZoneUuid; + + @SerializedName(ApiConstants.NICIRA_NVP_GATEWAYSERVICE_UUID) @Param(description="this L3 gateway service Uuid") + private String l3GatewayServiceUuid; + public void setId(long nvpDeviceId) { this.id.setValue(nvpDeviceId); } @@ -48,6 +57,26 @@ public class NiciraNvpDeviceResponse extends BaseResponse { public void setDeviceName(String deviceName) { this.deviceName = deviceName; - } + } + + public void setId(IdentityProxy id) { + this.id = id; + } + + public void setPhysicalNetworkId(IdentityProxy physicalNetworkId) { + this.physicalNetworkId = physicalNetworkId; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + public void setTransportZoneUuid(String transportZoneUuid) { + this.transportZoneUuid = transportZoneUuid; + } + + public void setL3GatewayServiceUuid(String l3GatewayServiceUuid) { + this.l3GatewayServiceUuid = l3GatewayServiceUuid; + } } 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 716cd7808f0..487a64d8f06 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 @@ -657,11 +657,18 @@ public class NiciraNvpElement extends AdapterBase implements @Override public NiciraNvpDeviceResponse createNiciraNvpDeviceResponse( NiciraNvpDeviceVO niciraNvpDeviceVO) { + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDeviceVO.getHostId()); + _hostDao.loadDetails(niciraNvpHost); + NiciraNvpDeviceResponse response = new NiciraNvpDeviceResponse(); response.setDeviceName(niciraNvpDeviceVO.getDeviceName()); response.setPhysicalNetworkId(niciraNvpDeviceVO.getPhysicalNetworkId()); response.setId(niciraNvpDeviceVO.getId()); response.setProviderName(niciraNvpDeviceVO.getProviderName()); + response.setHostName(niciraNvpHost.getDetail("ip")); + response.setTransportZoneUuid(niciraNvpHost.getDetail("transportzoneuuid")); + response.setL3GatewayServiceUuid(niciraNvpHost.getDetail("l3gatewayserviceuuid")); + response.setObjectName("niciranvpdevice"); return response; } @@ -705,7 +712,6 @@ public class NiciraNvpElement extends AdapterBase implements _resourceMgr.deleteHost(hostId, false, false); _niciraNvpDao.remove(niciraDeviceId); - return true; } 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 0908101a28a..4d01bc6e8c8 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 @@ -229,7 +229,8 @@ public class NiciraNvpResource implements ServerResource { } else if (cmd instanceof ConfigurePortForwardingRulesOnLogicalRouterCommand) { return executeRequest((ConfigurePortForwardingRulesOnLogicalRouterCommand) cmd, numRetries); - } s_logger.debug("Received unsupported command " + cmd.toString()); + } + s_logger.debug("Received unsupported command " + cmd.toString()); return Answer.createUnsupportedCommandAnswer(cmd); } diff --git a/ui/scripts/system.js b/ui/scripts/system.js index e5d53e2a4ac..826ba086497 100644 --- a/ui/scripts/system.js +++ b/ui/scripts/system.js @@ -3761,7 +3761,213 @@ name: { label: 'label.name' }//, //state: { label: 'label.status' } //comment it for now, since dataProvider below doesn't get called by widget code after action is done } - } + }, + // Nicira Nvp provider detail view + niciraNvp: { + type: 'detailView', + id: 'niciraNvpProvider', + label: 'label.niciraNvp', + viewAll: { label: 'label.devices', path: '_zone.niciraNvpDevices' }, + tabs: { + details: { + title: 'label.details', + fields: [ + { + name: { label: 'label.name' } + }, + { + state: { label: 'label.state' } + } + ], + dataProvider: function(args) { + refreshNspData("NiciraNvp"); + var providerObj; + $(nspHardcodingArray).each(function(){ + if(this.id == "niciraNvp") { + providerObj = this; + return false; //break each loop + } + }); + args.response.success({ + data: providerObj, + actionFilter: networkProviderActionFilter('niciraNvp') + }); + } + } + }, + actions: { + add: { + label: 'label.add.NiciraNvp.device', + createForm: { + title: 'label.add.NiciraNvp.device', + preFilter: function(args) { }, // TODO What is this? + fields: { + host: { + label: 'label.ip.address' + }, + username: { + label: 'label.username' + }, + password: { + label: 'label.password', + isPassword: true + }, + numretries: { + label: 'label.numretries', + defaultValue: '2' + }, + transportzoneuuid: { + label: 'label.nicira.transportzoneuuid' + }, + l3gatewayserviceuuid: { + label: 'label.nicira.l3gatewayserviceuuid' + } + } + }, + action: function(args) { + if(nspMap["niciraNvp"] == null) { + $.ajax({ + url: createURL("addNetworkServiceProvider&name=NiciraNvp&physicalnetworkid=" + selectedPhysicalNetworkObj.id), + dataType: "json", + async: true, + success: function(json) { + var jobId = json.addnetworkserviceproviderresponse.jobid; + var addNiciraNvpProviderIntervalID = setInterval(function() { + $.ajax({ + url: createURL("queryAsyncJobResult&jobId="+jobId), + dataType: "json", + success: function(json) { + var result = json.queryasyncjobresultresponse; + if (result.jobstatus == 0) { + return; //Job has not completed + } + else { + clearInterval(addNiciraNvpProviderIntervalID); + if (result.jobstatus == 1) { + nspMap["niciraNvp"] = json.queryasyncjobresultresponse.jobresult.networkserviceprovider; + addNiciraNvpDevice(args, selectedPhysicalNetworkObj, "addNiciraNvpDevice", "addniciranvpdeviceresponse", "niciranvpdevice") + } + else if (result.jobstatus == 2) { + alert("addNetworkServiceProvider&name=NiciraNvp failed. Error: " + _s(result.jobresult.errortext)); + } + } + }, + error: function(XMLHttpResponse) { + var errorMsg = parseXMLHttpResponse(XMLHttpResponse); + alert("addNetworkServiceProvider&name=NiciraNvp failed. Error: " + errorMsg); + } + }); + }, 3000); + } + }); + } + else { + addNiciraNvpDevice(args, selectedPhysicalNetworkObj, "addNiciraNvpDevice", "addniciranvpdeviceresponse", "niciranvpdevice") + } + }, + messages: { + notification: function(args) { + return 'label.add.NiciraNvp.device'; + } + }, + notification: { + poll: pollAsyncJobResult + } + }, + enable: { + label: 'label.enable.provider', + action: function(args) { + $.ajax({ + url: createURL("updateNetworkServiceProvider&id=" + nspMap["niciraNvp"].id + "&state=Enabled"), + dataType: "json", + success: function(json) { + var jid = json.updatenetworkserviceproviderresponse.jobid; + args.response.success( + {_custom: + { + jobId: jid, + getUpdatedItem: function(json) { + $(window).trigger('cloudStack.fullRefresh'); + } + } + } + ); + } + }); + }, + messages: { + confirm: function(args) { + return 'message.confirm.enable.provider'; + }, + notification: function() { + return 'label.enable.provider'; + } + }, + notification: { poll: pollAsyncJobResult } + }, + disable: { + label: 'label.disable.provider', + action: function(args) { + $.ajax({ + url: createURL("updateNetworkServiceProvider&id=" + nspMap["niciraNvp"].id + "&state=Disabled"), + dataType: "json", + success: function(json) { + var jid = json.updatenetworkserviceproviderresponse.jobid; + args.response.success( + {_custom: + { + jobId: jid, + getUpdatedItem: function(json) { + $(window).trigger('cloudStack.fullRefresh'); + } + } + } + ); + } + }); + }, + messages: { + confirm: function(args) { + return 'message.confirm.disable.provider'; + }, + notification: function() { + return 'label.disable.provider'; + } + }, + notification: { poll: pollAsyncJobResult } + }, + destroy: { + label: 'label.shutdown.provider', + action: function(args) { + $.ajax({ + url: createURL("deleteNetworkServiceProvider&id=" + nspMap["niciraNvp"].id), + dataType: "json", + success: function(json) { + var jid = json.deletenetworkserviceproviderresponse.jobid; + args.response.success( + {_custom: + { + jobId: jid + } + } + ); + + $(window).trigger('cloudStack.fullRefresh'); + } + }); + }, + messages: { + confirm: function(args) { + return 'message.confirm.shutdown.provider'; + }, + notification: function(args) { + return 'label.shutdown.provider'; + } + }, + notification: { poll: pollAsyncJobResult } + } + } + } } } }, @@ -6312,7 +6518,171 @@ } } }, - + // FIXME convert to nicira detailview + // NiciraNvp devices listView + niciraNvpDevices: { + id: 'niciraNvpDevices', + title: 'label.devices', + listView: { + id: 'niciraNvpDevices', + fields: { + hostname: { label: 'label.nicira.controller.address' }, + transportzoneuuid: { label: 'label.nicira.transportzoneuuid'}, + l3gatewayserviceuuid: { label: 'label.nicira.l3gatewayserviceuuid' } + }, + actions: { + add: { + label: 'label.add.NiciraNvp.device', + createForm: { + title: 'label.add.NiciraNvp.device', + preFilter: function(args) { }, // TODO What is this? + fields: { + host: { + label: 'label.ip.address' + }, + username: { + label: 'label.username' + }, + password: { + label: 'label.password', + isPassword: true + }, + numretries: { + label: 'label.numretries', + defaultValue: '2' + }, + transportzoneuuid: { + label: 'label.nicira.transportzoneuuid' + }, + l3gatewayserviceuuid: { + label: 'label.nicira.l3gatewayserviceuuid' + } + } + }, + action: function(args) { + if(nspMap["niciraNvp"] == null) { + $.ajax({ + url: createURL("addNetworkServiceProvider&name=NiciraNvp&physicalnetworkid=" + selectedPhysicalNetworkObj.id), + dataType: "json", + async: true, + success: function(json) { + var jobId = json.addnetworkserviceproviderresponse.jobid; + var addNiciraNvpProviderIntervalID = setInterval(function() { + $.ajax({ + url: createURL("queryAsyncJobResult&jobId="+jobId), + dataType: "json", + success: function(json) { + var result = json.queryasyncjobresultresponse; + if (result.jobstatus == 0) { + return; // Job has not completed + } + else { + clearInterval(addNiciraNvpProviderIntervalID); + if (result.jobstatus == 1) { + nspMap["niciraNvp"] = json.queryasyncjobresultresponse.jobresult.networkserviceprovider; + addNiciraNvpDevice(args, selectedPhysicalNetworkObj, "addNiciraNvpDevice", "addniciranvpdeviceresponse", "niciranvpdevice") + } + else if (result.jobstatus == 2) { + alert("addNetworkServiceProvider&name=NiciraNvp failed. Error: " + _s(result.jobresult.errortext)); + } + } + }, + error: function(XMLHttpResponse) { + var errorMsg = parseXMLHttpResponse(XMLHttpResponse); + alert("addNetworkServiceProvider&name=NiciraNvp failed. Error: " + errorMsg); + } + }); + }, 3000); + } + }); + } + else { + addNiciraNvpDevice(args, selectedPhysicalNetworkObj, "addNiciraNvpDevice", "addniciranvpdeviceresponse", "niciranvpdevice") + } + }, + + messages: { + notification: function(args) { + return 'Added new Nicira Nvp Controller'; + } + }, + notification: { + poll: pollAsyncJobResult + } + } + }, + dataProvider: function(args) { + $.ajax({ + url: createURL("listNiciraNvpDevices&physicalnetworkid=" + selectedPhysicalNetworkObj.id), + data: { page: args.page, pageSize: pageSize }, + dataType: "json", + async: false, + success: function(json) { + var items = json.listniciranvpdeviceresponse.niciranvpdevice; + args.response.success({data: items}); + } + }); + }, + detailView: { + name: 'Nicira Nvp details', + actions: { + 'remove': { + label: 'label.delete.NiciaNvp', + messages: { + confirm: function(args) { + return 'message.confirm.delete.NiciraNvp'; + }, + notification: function(args) { + return 'label.delete.NiciraNvp'; + } + }, + action: function(args) { + $.ajax({ + url: createURL("deleteNiciraNvpDevice&nvpdeviceid=" + args.context.niciraNvpDevices[0].nvpdeviceid), + dataType: "json", + async: true, + success: function(json) { + var jid = json.deleteniciranvpdeviceresponse.jobid; + args.response.success( + {_custom: + {jobId: jid} + } + ); + } + }); + }, + notification: { + poll: pollAsyncJobResult + } + } + }, + tabs: { + details: { + title: 'label.details', + fields: [ + { + nvpdeviceid: { label: 'label.id' }, + hostname: { label: 'label.ip.address' }, + transportzoneuuid: { label: 'label.nicira.transportzoneuuid' }, + l3gatewayserviceuuid: { label: 'label.nicira.l3gatewayserviceuuid' } + } + ], + dataProvider: function(args) { + $.ajax({ + url: createURL("listNiciraNvpDevices&nvpdeviceid=" + args.context.niciraNvpDevices[0].nvpdeviceid), + dataType: "json", + async: true, + success: function(json) { + var item = json.listniciranvpdeviceresponse.niciranvpdevice[0]; + args.response.success({data: item}); + } + }); + } + } + } + } + } + }, pods: { title: 'label.pods', listView: { @@ -9327,6 +9697,40 @@ }); } + function addNiciraNvpDevice(args, physicalNetworkObj, apiCmd, apiCmdRes, apiCmdObj) { + var array1 = []; + array1.push("&physicalnetworkid=" + physicalNetworkObj.id); + array1.push("&username=" + todb(args.data.username)); + array1.push("&password=" + todb(args.data.password)); + array1.push("&hostname=" + todb(args.data.host)); + array1.push("&transportzoneuuid=" + todb(args.data.transportzoneuuid)); + + var l3GatewayServiceUuid = args.data.l3gatewayserviceuuid; + if(l3GatewayServiceUuid != null && l3GatewayServiceUuid.length > 0) { + array1.push("&l3gatewayserviceuuid=" + todb(args.data.l3gatewayserviceuuid)); + } + + $.ajax({ + url: createURL(apiCmd + array1.join("")), + dataType: "json", + success: function(json) { + var jid = json[apiCmdRes].jobid; + args.response.success( + {_custom: + {jobId: jid, + getUpdatedItem: function(json) { + var item = json.queryasyncjobresultresponse.jobresult[apiCmdObj]; + + return item; + } + } + } + ); + } + }); + } + + var afterCreateZonePhysicalNetworkTrafficTypes = function(args, newZoneObj, newPhysicalnetwork) { $.ajax({ url: createURL("updatePhysicalNetwork&state=Enabled&id=" + newPhysicalnetwork.id), @@ -9924,6 +10328,9 @@ case "SecurityGroupProvider": nspMap["securityGroups"] = items[i]; break; + case "NiciraNvp": + nspMap["niciraNvp"] = items[i]; + break; } } } @@ -9940,7 +10347,12 @@ id: 'virtualRouter', name: 'Virtual Router', state: nspMap.virtualRouter ? nspMap.virtualRouter.state : 'Disabled' - } + }, + { + id: 'niciraNvp', + name: 'Nicira Nvp', + state: nspMap.niciraNvp ? nspMap.niciraNvp.state : 'Disabled' + } ]; if(selectedZoneObj.networktype == "Basic") { From bb4dadaed8223cbbf34cf74580af06becf32927a Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Mon, 1 Oct 2012 10:12:59 -0700 Subject: [PATCH 10/15] small fixes --- client/WEB-INF/classes/resources/messages.properties | 7 +++++++ .../src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/client/WEB-INF/classes/resources/messages.properties b/client/WEB-INF/classes/resources/messages.properties index 454974d4654..e23658259f2 100644 --- a/client/WEB-INF/classes/resources/messages.properties +++ b/client/WEB-INF/classes/resources/messages.properties @@ -1512,3 +1512,10 @@ error.menu.select=Unable to perform action due to no items being selected. error.mgmt.server.inaccessible=The Management Server is unaccessible. Please try again later. error.session.expired=Your session has expired. error.unresolved.internet.name=Your internet name cannot be resolved. + +label.add.NiciraNvp.device=Add Nvp Controller +label.delete.NiciraNvp=Remove Nvp Controller +label.nicira.controller.address=Controller Address +label.nicira.transportzoneuuid=Transport Zone Uuid +label.nicia.l3gatewayserviceuuid=L3 Gateway Service Uuid + diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java index 7744b4f7148..ef12bf62605 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/api/commands/AddNiciraNvpDeviceCmd.java @@ -44,7 +44,7 @@ import com.cloud.utils.exception.CloudRuntimeException; @Implementation(responseObject=NiciraNvpDeviceResponse.class, description="Adds a Nicira NVP device") public class AddNiciraNvpDeviceCmd extends BaseAsyncCmd { private static final Logger s_logger = Logger.getLogger(AddNiciraNvpDeviceCmd.class.getName()); - private static final String s_name = "addniciranvpdevice"; + private static final String s_name = "addniciranvpdeviceresponse"; @PlugService NiciraNvpElementService _niciraNvpElementService; ///////////////////////////////////////////////////// From 27473bd6eea73f6101f8b2f0c3ae11d8c65385a4 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 5 Oct 2012 07:53:24 -0700 Subject: [PATCH 11/15] Add the labels to the resources and the dictionary --- client/WEB-INF/classes/resources/messages.properties | 2 +- ui/dictionary.jsp | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/client/WEB-INF/classes/resources/messages.properties b/client/WEB-INF/classes/resources/messages.properties index e23658259f2..0210e427dfc 100644 --- a/client/WEB-INF/classes/resources/messages.properties +++ b/client/WEB-INF/classes/resources/messages.properties @@ -1517,5 +1517,5 @@ label.add.NiciraNvp.device=Add Nvp Controller label.delete.NiciraNvp=Remove Nvp Controller label.nicira.controller.address=Controller Address label.nicira.transportzoneuuid=Transport Zone Uuid -label.nicia.l3gatewayserviceuuid=L3 Gateway Service Uuid +label.nicira.l3gatewayserviceuuid=L3 Gateway Service Uuid diff --git a/ui/dictionary.jsp b/ui/dictionary.jsp index 9457e30af79..25cc005abcd 100644 --- a/ui/dictionary.jsp +++ b/ui/dictionary.jsp @@ -1408,6 +1408,11 @@ dictionary = { 'label.add.network.ACL': '', 'label.private.Gateway': '', 'label.VPC.router.details': '', -'label.VMs.in.tier': '' +'label.VMs.in.tier': '', +'label.add.NiciraNvp.device': '', +'label.delete.NiciraNvp': '', +'label.nicira.controller.address': '', +'label.nicira.transportzoneuuid': '', +'label.nicira.l3gatewayserviceuuid': '' }; From 18e11ac0c8d5cf10bea5e6a4cdca18725873a02b Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 5 Oct 2012 09:27:21 -0700 Subject: [PATCH 12/15] These need to be objects instead of primitives otherwise they get included by default during conversion to json. --- .../src/com/cloud/network/nicira/Match.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java index cd898476b39..ce5205ba47f 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/Match.java @@ -22,15 +22,15 @@ package com.cloud.network.nicira; public class Match { private Integer protocol; private String source_ip_addresses; - private boolean source_ip_addresses_not; + private Boolean source_ip_addresses_not; private String destination_ip_addresses; - private boolean destination_ip_addresses_not; + private Boolean destination_ip_addresses_not; private Integer source_port_min; private Integer source_port_max; - private boolean source_port_not; + private Boolean source_port_not; private Integer destination_port_min; private Integer destination_port_max; - private boolean destination_port_not; + private Boolean destination_port_not; private String ethertype = "IPv4"; public Integer getProtocol() { @@ -57,11 +57,11 @@ public class Match { this.source_port_max = source_port_max; } - public boolean isSourcePortNot() { + public Boolean isSourcePortNot() { return source_port_not; } - public void setSourcePortNot(boolean source_port_not) { + public void setSourcePortNot(Boolean source_port_not) { this.source_port_not = source_port_not; } @@ -81,11 +81,11 @@ public class Match { this.destination_port_max = destination_port_max; } - public boolean isDestinationPortNot() { + public Boolean isDestinationPortNot() { return destination_port_not; } - public void setDestinationPortNot(boolean destination_port_not) { + public void setDestinationPortNot(Boolean destination_port_not) { this.destination_port_not = destination_port_not; } @@ -121,11 +121,11 @@ public class Match { this.destination_ip_addresses = destination_ip_addresses; } - public boolean isDestinationIpAddressesNot() { + public Boolean isDestinationIpAddressesNot() { return destination_ip_addresses_not; } - public void setDestinationIpAddressesNot(boolean destination_ip_addresses_not) { + public void setDestinationIpAddressesNot(Boolean destination_ip_addresses_not) { this.destination_ip_addresses_not = destination_ip_addresses_not; } From 553a72047fcf35f628dcedcec40d90ff4859164a Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 11 Oct 2012 18:13:47 +0200 Subject: [PATCH 13/15] Implement static nat handling --- .../src/com/cloud/network/nicira/NatRule.java | 4 +- .../cloud/network/nicira/NiciraNvpApi.java | 12 +- .../network/resource/NiciraNvpResource.java | 133 +++++++++++++++++- 3 files changed, 141 insertions(+), 8 deletions(-) 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 82a33787faf..7255ab67d39 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 @@ -19,7 +19,7 @@ package com.cloud.network.nicira; /** * */ -public abstract class NatRule { +public class NatRule { protected Match match; protected String to_source_ip_address_min; protected String to_source_ip_address_max; @@ -31,6 +31,8 @@ public abstract class NatRule { protected String to_destination_ip_address_max; protected Integer to_destination_port; + public NatRule() {} + public Match getMatch() { return match; } 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 fe4deec0626..77feb0fbd9c 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 @@ -256,6 +256,12 @@ public class NiciraNvpApi { throw new NiciraNvpApiException("Unknown NatRule type"); } + public void modifyLogicalRouterNatRule(String logicalRouterUuid, NatRule natRule) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat/" + natRule.getUuid(); + + executeUpdateObject(natRule, uri, Collections.emptyMap()); + } + public void deleteLogicalRouterNatRule(String logicalRouterUuid, String natRuleUuid) throws NiciraNvpApiException { String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat/" + natRuleUuid; @@ -286,8 +292,10 @@ public class NiciraNvpApi { public NiciraNvpList findNatRulesByLogicalRouterUuid(String logicalRouterUuid) throws NiciraNvpApiException { String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/nat"; - - return executeRetrieveObject(new TypeToken>(){}.getType(), uri, Collections.emptyMap()); + Map params = new HashMap(); + params.put("fields","*"); + + return executeRetrieveObject(new TypeToken>(){}.getType(), uri, params); } private void executeUpdateObject(T newObject, String uri, Map parameters) throws NiciraNvpApiException { 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 4d01bc6e8c8..83a889f55a0 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 @@ -59,6 +59,7 @@ import com.cloud.host.Host; import com.cloud.host.Host.Type; import com.cloud.network.nicira.Attachment; import com.cloud.network.nicira.ControlClusterStatus; +import com.cloud.network.nicira.DestinationNatRule; import com.cloud.network.nicira.L3GatewayAttachment; import com.cloud.network.nicira.LogicalRouterConfig; import com.cloud.network.nicira.LogicalRouterPort; @@ -492,16 +493,138 @@ public class NiciraNvpResource implements ServerResource { for (StaticNatRuleTO rule : cmd.getRules()) { // Find if a DestinationNat rule exists for this rule - for (NatRule storedRule : existingRules.getResults()) { + boolean found = false; + String insideIp = rule.getDstIp(); + String insideCidr = rule.getDstIp() + "/32"; + String outsideIp = rule.getSrcIp(); + String outsideCidr = rule.getSrcIp() + "/32"; + + NatRule incoming = null; + NatRule outgoing = null; + + for (NatRule storedRule : existingRules.getResults()) { + if (s_logger.isDebugEnabled()) { + StringBuilder natRuleStr = new StringBuilder(); + natRuleStr.append("Rule "); + natRuleStr.append(storedRule.getUuid()); + natRuleStr.append(" ("); + natRuleStr.append(storedRule.getType()); + natRuleStr.append(") :"); + Match m = storedRule.getMatch(); + natRuleStr.append("match ("); + natRuleStr.append(m.getProtocol()); + natRuleStr.append(" "); + natRuleStr.append(m.getSourceIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getSource_port_min()); + natRuleStr.append("-"); + natRuleStr.append(m.getSourcePortMax()); + natRuleStr.append(" ] -> "); + natRuleStr.append(m.getDestinationIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getDestinationPortMin()); + natRuleStr.append("-"); + natRuleStr.append(m.getDestinationPortMax()); + natRuleStr.append(" ]) -->"); + if ("SourceNatRule".equals(storedRule.getType())) { + natRuleStr.append(storedRule.getToSourceIpAddressMin()); + natRuleStr.append("-"); + natRuleStr.append(storedRule.getToSourceIpAddressMax()); + natRuleStr.append(" ["); + natRuleStr.append(storedRule.getToSourcePortMin()); + natRuleStr.append("-"); + natRuleStr.append(storedRule.getToSourcePortMax()); + natRuleStr.append(" ])"); + } + else { + natRuleStr.append(storedRule.getToDestinationIpAddressMin()); + natRuleStr.append("-"); + natRuleStr.append(storedRule.getToDestinationIpAddressMax()); + natRuleStr.append(" ["); + natRuleStr.append(storedRule.getToDestinationPort()); + natRuleStr.append(" ])"); + } + s_logger.debug(natRuleStr.toString()); + } + if ("SourceNatRule".equals(storedRule.getType())) { + if (outsideIp.equals(storedRule.getToSourceIpAddressMin()) && + outsideIp.equals(storedRule.getToSourceIpAddressMax()) && + storedRule.getToSourcePortMin() == null) { + // The outgoing rule exists + outgoing = storedRule; + } + } + if ("DestinationNatRule".equals(storedRule.getType()) && + storedRule.getToDestinationPort() != null) { + // Skip PortForwarding rules continue; } - String insideCidr = rule.getDstIp() + "/32"; - String outsideCidr = rule.getSrcIp() + "/32"; - //if (insideCidr.equals(storedRule.getMatch().getDestinationIpAddresses())) + // Compare against Ip as it should be a /32 cidr and the /32 is omitted + if (outsideIp.equals(storedRule.getMatch().getDestinationIpAddresses())) { + // The incoming rule exists + incoming = storedRule; + } } + if (incoming != null && outgoing != null) { + if (insideIp.equals(incoming.getToDestinationIpAddressMin())) { + if (rule.revoked()) { + s_logger.debug("Deleting incoming rule " + incoming.getUuid()); + _niciraNvpApi.deleteLogicalRouterNatRule(cmd.getLogicalRouterUuid(), incoming.getUuid()); + + s_logger.debug("Deleting outgoing rule " + outgoing.getUuid()); + _niciraNvpApi.deleteLogicalRouterNatRule(cmd.getLogicalRouterUuid(), outgoing.getUuid()); + } + } + else { + s_logger.debug("Updating outgoing rule " + outgoing.getUuid()); + outgoing.setToDestinationIpAddressMin(insideIp); + outgoing.setToDestinationIpAddressMax(insideIp); + _niciraNvpApi.modifyLogicalRouterNatRule(cmd.getLogicalRouterUuid(), outgoing); + + s_logger.debug("Updating incoming rule " + outgoing.getUuid()); + incoming.setToSourceIpAddressMin(insideIp); + incoming.setToSourceIpAddressMax(insideIp); + _niciraNvpApi.modifyLogicalRouterNatRule(cmd.getLogicalRouterUuid(), incoming); + break; + } + } + else { + if (rule.revoked()) { + s_logger.warn("Tried deleting a rule that does not exist, " + + rule.getSrcIp() + " -> " + rule.getDstIp()); + break; + } + + // api createLogicalRouterNatRule + // create the dnat rule + Match m = new Match(); + m.setDestinationIpAddresses(outsideCidr); + DestinationNatRule newDnatRule = new DestinationNatRule(); + newDnatRule.setMatch(m); + newDnatRule.setToDestinationIpAddressMin(insideIp); + newDnatRule.setToDestinationIpAddressMax(insideIp); + newDnatRule = (DestinationNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newDnatRule); + s_logger.debug("Created " + newDnatRule.getType() + " rule " + + newDnatRule.getUuid() + ", " + + newDnatRule.getMatch().getDestinationIpAddresses() + + " -> " + newDnatRule.getToDestinationIpAddressMin()); + + // create matching snat rule + m = new Match(); + m.setSourceIpAddresses(insideIp + "/32"); + SourceNatRule newSnatRule = new SourceNatRule(); + newSnatRule.setMatch(m); + newSnatRule.setToSourceIpAddressMin(outsideIp); + newSnatRule.setToSourceIpAddressMax(outsideIp); + newSnatRule = (SourceNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newSnatRule); + s_logger.debug("Created " + newSnatRule.getType() + " rule " + + newSnatRule.getUuid() + ", " + + newSnatRule.getMatch().getSourceIpAddresses() + " -> " + + newSnatRule.getToSourceIpAddressMin()); + + } } - //FIXME implement! return new ConfigureStaticNatRulesOnLogicalRouterAnswer(cmd, true, cmd.getRules().size() +" StaticNat rules applied"); } catch (NiciraNvpApiException e) { if (numRetries > 0) { From ab8ba3d61d9d380452bc789b39046a6321cd2187 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 12 Oct 2012 11:57:27 +0200 Subject: [PATCH 14/15] Implement the portforwarding code in the resource Fix a small naming typo --- ...nfigurePublicIpsOnLogicalRouterAnswer.java | 14 ++ ...figurePublicIpsOnLogicalRouterCommand.java | 50 +++++ .../dao/NiciraNvpRouterMappingDao.java | 2 +- .../dao/NiciraNvpRouterMappingDaoImpl.java | 2 +- .../network/element/NiciraNvpElement.java | 49 ++++- .../cloud/network/nicira/NiciraNvpApi.java | 15 ++ .../network/resource/NiciraNvpResource.java | 203 ++++++++++++++++-- 7 files changed, 310 insertions(+), 25 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterAnswer.java create mode 100644 plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterAnswer.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterAnswer.java new file mode 100644 index 00000000000..d0749eda398 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterAnswer.java @@ -0,0 +1,14 @@ +package com.cloud.agent.api; + +public class ConfigurePublicIpsOnLogicalRouterAnswer extends Answer { + + public ConfigurePublicIpsOnLogicalRouterAnswer(Command command, + boolean success, String details) { + super(command, success, details); + } + + public ConfigurePublicIpsOnLogicalRouterAnswer(Command command, Exception e) { + super(command, e); + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java new file mode 100644 index 00000000000..8c7c8d71b09 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java @@ -0,0 +1,50 @@ +package com.cloud.agent.api; + +import java.util.List; + +public class ConfigurePublicIpsOnLogicalRouterCommand extends Command { + + private String logicalRouterUuid; + private String l3GatewayServiceUuid; + private List publicCidrs; + + public ConfigurePublicIpsOnLogicalRouterCommand(String logicalRouterUuid, + String l3GatewayServiceUuid, + List publicCidrs) { + super(); + this.logicalRouterUuid = logicalRouterUuid; + this.publicCidrs = publicCidrs; + this.l3GatewayServiceUuid = l3GatewayServiceUuid; + } + + public String getLogicalRouterUuid() { + return logicalRouterUuid; + } + + public void setLogicalRouterUuid(String logicalRouterUuid) { + this.logicalRouterUuid = logicalRouterUuid; + } + + public String getL3GatewayServiceUuid() { + return l3GatewayServiceUuid; + } + + public void setL3GatewayServiceUuid(String l3GatewayServiceUuid) { + this.l3GatewayServiceUuid = l3GatewayServiceUuid; + } + + public List getPublicCidrs() { + return publicCidrs; + } + + public void setPublicCidrs(List publicCidrs) { + this.publicCidrs = publicCidrs; + } + + @Override + public boolean executeInSequence() { + // TODO Auto-generated method stub + return false; + } + +} diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java index 6fae52eb773..bb3454a3809 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDao.java @@ -5,5 +5,5 @@ import com.cloud.utils.db.GenericDao; public interface NiciraNvpRouterMappingDao extends GenericDao { - public NiciraNvpRouterMappingVO findByNetworkIdI(long id); + public NiciraNvpRouterMappingVO findByNetworkId(long id); } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java index 303b7608f31..1edcc03a51c 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/dao/NiciraNvpRouterMappingDaoImpl.java @@ -20,7 +20,7 @@ public class NiciraNvpRouterMappingDaoImpl extends GenericDaoBase sc = networkSearch.create(); sc.setParameters("network_id", id); return findOneBy(sc); 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 487a64d8f06..71f8454f8d5 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 @@ -49,6 +49,8 @@ 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.ConfigurePublicIpsOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer; import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalRouterAnswer; @@ -469,7 +471,7 @@ public class NiciraNvpElement extends AdapterBase implements // Deleting the LogicalRouter will also take care of all provisioned // nat rules. NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao - .findByNetworkIdI(network.getId()); + .findByNetworkId(network.getId()); if (routermapping == null) { s_logger.warn("No logical router uuid found for network " + network.getDisplayText()); @@ -834,13 +836,40 @@ public class NiciraNvpElement extends AdapterBase implements public boolean applyIps(Network network, List ipAddress, Set services) throws ResourceUnavailableException { - s_logger.debug("Entering applyIps"); // TODO Remove this line - - // The Nicira Nvp L3 Gateway doesn't need the addresses configured on - // the router interface. Only configure the CIDR and individual ip - // addresses become available when DNAT rules are created for them. - // The cidr setup is done during implementation of the logical router - // in the function implement + if (services.contains(Service.SourceNat)) { + // Only if we need to provide SourceNat we need to configure the logical router + // SourceNat is required for StaticNat and PortForwarding + List devices = _niciraNvpDao + .listByPhysicalNetwork(network.getPhysicalNetworkId()); + if (devices.isEmpty()) { + s_logger.error("No NiciraNvp Controller on physical network " + + network.getPhysicalNetworkId()); + return false; + } + NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); + HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); + _hostDao.loadDetails(niciraNvpHost); + + NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao + .findByNetworkId(network.getId()); + if (routermapping == null) { + s_logger.error("No logical router uuid found for network " + + network.getDisplayText()); + return false; + } + + List cidrs = new ArrayList(); + for (PublicIpAddress ip : ipAddress) { + cidrs.add(ip.getAddress().addr() + "/" + NetUtils.getCidrSize(ip.getNetmask())); + } + ConfigurePublicIpsOnLogicalRouterCommand cmd = new ConfigurePublicIpsOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), + niciraNvpHost.getDetail("l3gatewayserviceuuid"), cidrs); + ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer) _agentMgr.easySend(niciraNvpHost.getId(), cmd); + return answer.getResult(); + } + else { + s_logger.debug("No need to provision ip addresses as we are not providing L3 services."); + } return true; } @@ -867,7 +896,7 @@ public class NiciraNvpElement extends AdapterBase implements HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao - .findByNetworkIdI(network.getId()); + .findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); @@ -915,7 +944,7 @@ public class NiciraNvpElement extends AdapterBase implements HostVO niciraNvpHost = _hostDao.findById(niciraNvpDevice.getHostId()); NiciraNvpRouterMappingVO routermapping = _niciraNvpRouterMappingDao - .findByNetworkIdI(network.getId()); + .findByNetworkId(network.getId()); if (routermapping == null) { s_logger.error("No logical router uuid found for network " + network.getDisplayText()); 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 77feb0fbd9c..a714e57f614 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 @@ -237,6 +237,12 @@ public class NiciraNvpApi { executeDeleteObject(uri); } + + public void modifyLogicalRouterPort(String logicalRouterUuid, LogicalRouterPort logicalRouterPort) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport/" + logicalRouterPort.getUuid(); + + executeUpdateObject(logicalRouterPort, uri, Collections.emptyMap()); + } public void modifyLogicalRouterPortAttachment(String logicalRouterUuid, String logicalRouterPortUuid, Attachment attachment) throws NiciraNvpApiException { String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport/" + logicalRouterPortUuid + "/attachment"; @@ -298,6 +304,15 @@ public class NiciraNvpApi { return executeRetrieveObject(new TypeToken>(){}.getType(), uri, params); } + public NiciraNvpList findLogicalRouterPortByGatewayServiceUuid(String logicalRouterUuid, String l3GatewayServiceUuid) throws NiciraNvpApiException { + String uri = "/ws.v1/lrouter/" + logicalRouterUuid + "/lport"; + Map params = new HashMap(); + params.put("fields", "*"); + params.put("attachment_gwsvc_uuid", l3GatewayServiceUuid); + + return executeRetrieveObject(new TypeToken>(){}.getType(), uri, params); + } + private void executeUpdateObject(T newObject, String uri, Map parameters) throws NiciraNvpApiException { String url; try { 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 83a889f55a0..5f445fa70dc 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 @@ -29,6 +29,8 @@ import com.cloud.agent.api.Answer; import com.cloud.agent.api.Command; import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer; import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer; import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand; import com.cloud.agent.api.CreateLogicalRouterAnswer; @@ -54,6 +56,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.PortForwardingRuleTO; import com.cloud.agent.api.to.StaticNatRuleTO; import com.cloud.host.Host; import com.cloud.host.Host.Type; @@ -230,7 +233,10 @@ public class NiciraNvpResource implements ServerResource { } else if (cmd instanceof ConfigurePortForwardingRulesOnLogicalRouterCommand) { return executeRequest((ConfigurePortForwardingRulesOnLogicalRouterCommand) cmd, numRetries); - } + } + else if (cmd instanceof ConfigurePublicIpsOnLogicalRouterCommand) { + return executeRequest((ConfigurePublicIpsOnLogicalRouterCommand) cmd, numRetries); + } s_logger.debug("Received unsupported command " + cmd.toString()); return Answer.createUnsupportedCommandAnswer(cmd); } @@ -483,9 +489,30 @@ public class NiciraNvpResource implements ServerResource { } } + private Answer executeRequest(ConfigurePublicIpsOnLogicalRouterCommand cmd, int numRetries) { + try { + NiciraNvpList ports = _niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(cmd.getLogicalRouterUuid(), cmd.getL3GatewayServiceUuid()); + if (ports.getResultCount() != 1) { + return new ConfigurePublicIpsOnLogicalRouterAnswer(cmd, false, "No logical router ports found, unable to set ip addresses"); + } + LogicalRouterPort lrp = ports.getResults().get(0); + lrp.setIpAddresses(cmd.getPublicCidrs()); + _niciraNvpApi.modifyLogicalRouterPort(cmd.getLogicalRouterUuid(), lrp); + + return new ConfigurePublicIpsOnLogicalRouterAnswer(cmd, true, "Logical Router deleted (uuid " + cmd.getLogicalRouterUuid() + ")"); + } catch (NiciraNvpApiException e) { + if (numRetries > 0) { + return retry(cmd, --numRetries); + } + else { + return new ConfigurePublicIpsOnLogicalRouterAnswer(cmd, e); + } + } + + } + private Answer executeRequest(ConfigureStaticNatRulesOnLogicalRouterCommand cmd, int numRetries) { try { - LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid()); NiciraNvpList 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 @@ -493,7 +520,6 @@ public class NiciraNvpResource implements ServerResource { for (StaticNatRuleTO rule : cmd.getRules()) { // Find if a DestinationNat rule exists for this rule - boolean found = false; String insideIp = rule.getDstIp(); String insideCidr = rule.getDstIp() + "/32"; String outsideIp = rule.getSrcIp(); @@ -605,10 +631,7 @@ public class NiciraNvpResource implements ServerResource { newDnatRule.setToDestinationIpAddressMin(insideIp); newDnatRule.setToDestinationIpAddressMax(insideIp); newDnatRule = (DestinationNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newDnatRule); - s_logger.debug("Created " + newDnatRule.getType() + " rule " - + newDnatRule.getUuid() + ", " - + newDnatRule.getMatch().getDestinationIpAddresses() - + " -> " + newDnatRule.getToDestinationIpAddressMin()); + s_logger.debug("Created " + natRuleToString(newDnatRule)); // create matching snat rule m = new Match(); @@ -618,10 +641,7 @@ public class NiciraNvpResource implements ServerResource { newSnatRule.setToSourceIpAddressMin(outsideIp); newSnatRule.setToSourceIpAddressMax(outsideIp); newSnatRule = (SourceNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newSnatRule); - s_logger.debug("Created " + newSnatRule.getType() + " rule " - + newSnatRule.getUuid() + ", " - + newSnatRule.getMatch().getSourceIpAddresses() + " -> " - + newSnatRule.getToSourceIpAddressMin()); + s_logger.debug("Created " + natRuleToString(newSnatRule)); } } @@ -639,8 +659,120 @@ public class NiciraNvpResource implements ServerResource { private Answer executeRequest(ConfigurePortForwardingRulesOnLogicalRouterCommand cmd, int numRetries) { try { - LogicalRouterConfig lrc = _niciraNvpApi.findOneLogicalRouterByUuid(cmd.getLogicalRouterUuid()); - //FIXME implement! + NiciraNvpList 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 + + for (PortForwardingRuleTO rule : cmd.getRules()) { + if (rule.isAlreadyAdded()) { + // Don't need to do anything + continue; + } + + // Find if a DestinationNat rule exists for this rule + String insideIp = rule.getDstIp(); + String insideCidr = rule.getDstIp() + "/32"; + String outsideIp = rule.getSrcIp(); + String outsideCidr = rule.getSrcIp() + "/32"; + + NatRule incoming = null; + NatRule outgoing = null; + + for (NatRule storedRule : existingRules.getResults()) { + if ("SourceNatRule".equals(storedRule.getType())) { + if (outsideIp.equals(storedRule.getToSourceIpAddressMin()) && + outsideIp.equals(storedRule.getToSourceIpAddressMax()) && + storedRule.getToSourcePortMin() == rule.getSrcPortRange()[0] && + storedRule.getToSourcePortMax() == rule.getSrcPortRange()[1]) { + // The outgoing rule exists + outgoing = storedRule; + } + } + else if ("DestinationNatRule".equals(storedRule.getType())) { + if (insideIp.equals(storedRule.getToDestinationIpAddressMin()) && + insideIp.equals(storedRule.getToDestinationIpAddressMax()) && + storedRule.getToDestinationPort() == rule.getDstPortRange()[0]) { + // The incoming rule exists + incoming = storedRule; + } + } + } + if (incoming != null && outgoing != null) { + if (insideIp.equals(incoming.getToDestinationIpAddressMin())) { + if (rule.revoked()) { + s_logger.debug("Deleting incoming rule " + incoming.getUuid()); + _niciraNvpApi.deleteLogicalRouterNatRule(cmd.getLogicalRouterUuid(), incoming.getUuid()); + + s_logger.debug("Deleting outgoing rule " + outgoing.getUuid()); + _niciraNvpApi.deleteLogicalRouterNatRule(cmd.getLogicalRouterUuid(), outgoing.getUuid()); + } + } + else { + s_logger.debug("Updating outgoing rule " + outgoing.getUuid()); + outgoing.setToDestinationIpAddressMin(insideIp); + outgoing.setToDestinationIpAddressMax(insideIp); + outgoing.setToDestinationPort(rule.getDstPortRange()[0]); + _niciraNvpApi.modifyLogicalRouterNatRule(cmd.getLogicalRouterUuid(), outgoing); + + s_logger.debug("Updating incoming rule " + outgoing.getUuid()); + incoming.setToSourceIpAddressMin(insideIp); + incoming.setToSourceIpAddressMax(insideIp); + incoming.setToSourcePortMin(rule.getSrcPortRange()[0]); + incoming.setToSourcePortMax(rule.getSrcPortRange()[1]); + _niciraNvpApi.modifyLogicalRouterNatRule(cmd.getLogicalRouterUuid(), incoming); + break; + } + } + else { + if (rule.revoked()) { + s_logger.warn("Tried deleting a rule that does not exist, " + + rule.getSrcIp() + " -> " + rule.getDstIp()); + break; + } + + // api createLogicalRouterNatRule + // create the dnat rule + Match m = new Match(); + m.setDestinationIpAddresses(outsideCidr); + if ("tcp".equals(rule.getProtocol())) { + m.setProtocol(6); + } + else if ("udp".equals(rule.getProtocol())) { + m.setProtocol(17); + } + m.setDestinationPortMin(rule.getSrcPortRange()[0]); + m.setDestinationPortMax(rule.getSrcPortRange()[1]); + DestinationNatRule newDnatRule = new DestinationNatRule(); + newDnatRule.setMatch(m); + newDnatRule.setToDestinationIpAddressMin(insideIp); + newDnatRule.setToDestinationIpAddressMax(insideIp); + newDnatRule.setToDestinationPort(rule.getDstPortRange()[0]); + newDnatRule = (DestinationNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newDnatRule); + s_logger.debug("Created " + natRuleToString(newDnatRule)); + + // create matching snat rule + m = new Match(); + m.setSourceIpAddresses(insideIp + "/32"); + if ("tcp".equals(rule.getProtocol())) { + m.setProtocol(6); + } + else if ("udp".equals(rule.getProtocol())) { + m.setProtocol(17); + } + m.setSourcePortMin(rule.getDstPortRange()[0]); + m.setSourcePortMax(rule.getDstPortRange()[1]); + SourceNatRule newSnatRule = new SourceNatRule(); + newSnatRule.setMatch(m); + newSnatRule.setToSourceIpAddressMin(outsideIp); + newSnatRule.setToSourceIpAddressMax(outsideIp); + newSnatRule.setToSourcePortMin(rule.getSrcPortRange()[0]); + newSnatRule.setToSourcePortMax(rule.getSrcPortRange()[1]); + newSnatRule = (SourceNatRule) _niciraNvpApi.createLogicalRouterNatRule(cmd.getLogicalRouterUuid(), newSnatRule); + s_logger.debug("Created " + natRuleToString(newSnatRule)); + + } + } return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(cmd, true, cmd.getRules().size() +" PortForwarding rules applied"); } catch (NiciraNvpApiException e) { if (numRetries > 0) { @@ -667,4 +799,49 @@ public class NiciraNvpResource implements ServerResource { return executeRequest(cmd, numRetriesRemaining); } + private String natRuleToString(NatRule rule) { + + StringBuilder natRuleStr = new StringBuilder(); + natRuleStr.append("Rule "); + natRuleStr.append(rule.getUuid()); + natRuleStr.append(" ("); + natRuleStr.append(rule.getType()); + natRuleStr.append(") :"); + Match m = rule.getMatch(); + natRuleStr.append("match ("); + natRuleStr.append(m.getProtocol()); + natRuleStr.append(" "); + natRuleStr.append(m.getSourceIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getSource_port_min()); + natRuleStr.append("-"); + natRuleStr.append(m.getSourcePortMax()); + natRuleStr.append(" ] -> "); + natRuleStr.append(m.getDestinationIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getDestinationPortMin()); + natRuleStr.append("-"); + natRuleStr.append(m.getDestinationPortMax()); + natRuleStr.append(" ]) -->"); + if ("SourceNatRule".equals(rule.getType())) { + natRuleStr.append(rule.getToSourceIpAddressMin()); + natRuleStr.append("-"); + natRuleStr.append(rule.getToSourceIpAddressMax()); + natRuleStr.append(" ["); + natRuleStr.append(rule.getToSourcePortMin()); + natRuleStr.append("-"); + natRuleStr.append(rule.getToSourcePortMax()); + natRuleStr.append(" ])"); + } + else { + natRuleStr.append(rule.getToDestinationIpAddressMin()); + natRuleStr.append("-"); + natRuleStr.append(rule.getToDestinationIpAddressMax()); + natRuleStr.append(" ["); + natRuleStr.append(rule.getToDestinationPort()); + natRuleStr.append(" ])"); + } + return natRuleStr.toString(); + } + } From 51f9a9887197cd2079b9030ffddce0db0474398f Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 12 Oct 2012 12:02:54 +0200 Subject: [PATCH 15/15] Check all TODO and FIXME tasks --- .../ConfigurePortForwardingRulesOnLogicalRouterCommand.java | 1 - .../agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java | 1 - .../api/ConfigureStaticNatRulesOnLogicalRouterCommand.java | 1 - .../src/com/cloud/network/element/NiciraNvpElement.java | 5 +---- 4 files changed, 1 insertion(+), 7 deletions(-) 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 index 1d57e35a95c..5f0ea384af3 100644 --- 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 @@ -54,7 +54,6 @@ public class ConfigurePortForwardingRulesOnLogicalRouterCommand extends Command */ @Override public boolean executeInSequence() { - // TODO Auto-generated method stub return false; } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java index 8c7c8d71b09..0da0b4ec9da 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/agent/api/ConfigurePublicIpsOnLogicalRouterCommand.java @@ -43,7 +43,6 @@ public class ConfigurePublicIpsOnLogicalRouterCommand extends Command { @Override public boolean executeInSequence() { - // TODO Auto-generated method stub return false; } 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 index 4e5d37cb417..960f609cb54 100644 --- 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 @@ -57,7 +57,6 @@ public class ConfigureStaticNatRulesOnLogicalRouterCommand extends Command { */ @Override public boolean executeInSequence() { - // TODO Auto-generated method stub return false; } 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 71f8454f8d5..1c8b8fb1dd3 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 @@ -268,10 +268,7 @@ public class NiciraNvpElement extends AdapterBase implements + network.getId()); } try { - // FIXME Check if any services other than connectiviy are required - // If that is the case start the logical router with only the - // internal interface, leave the external interfaces to the - // IpDeployer + // Implement SourceNat immediately as we have al the info already if (_networkManager.isProviderSupportServiceInNetwork( network.getId(), Service.SourceNat, Provider.NiciraNvp)) { s_logger.debug("Apparently we are supposed to provide SourceNat on this network");