Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B91F7200B40 for ; Thu, 16 Jun 2016 10:12:20 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B7C27160A60; Thu, 16 Jun 2016 08:12:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 6BFCB160A63 for ; Thu, 16 Jun 2016 10:12:18 +0200 (CEST) Received: (qmail 63619 invoked by uid 500); 16 Jun 2016 08:12:05 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 61202 invoked by uid 99); 16 Jun 2016 08:12:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Jun 2016 08:12:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 47F1FED310; Thu, 16 Jun 2016 08:12:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sateesh@apache.org To: commits@cloudstack.apache.org Date: Thu, 16 Jun 2016 08:12:35 -0000 Message-Id: <331636a600fb41f18d66f31bd9a87d43@git.apache.org> In-Reply-To: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> References: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [34/50] [abbrv] git commit: updated refs/heads/deploy-from-snapshot to 44ba14d archived-at: Thu, 16 Jun 2016 08:12:20 -0000 http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SingleDefaultRouteImplicitRoutingConfig.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SingleDefaultRouteImplicitRoutingConfig.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SingleDefaultRouteImplicitRoutingConfig.java new file mode 100644 index 0000000..ea5df2c --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SingleDefaultRouteImplicitRoutingConfig.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.network.nicira; + +/** + * + */ +public class SingleDefaultRouteImplicitRoutingConfig extends RoutingConfig { + public RouterNextHop defaultRouteNextHop; + public final String type = "SingleDefaultRouteImplicitRoutingConfig"; + + public SingleDefaultRouteImplicitRoutingConfig(final RouterNextHop routerNextHop) { + defaultRouteNextHop = routerNextHop; + } + + public RouterNextHop getDefaultRouteNextHop() { + return defaultRouteNextHop; + } + + public void setDefaultRouteNextHop(final RouterNextHop defaultRouteNextHop) { + this.defaultRouteNextHop = defaultRouteNextHop; + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SourceNatRule.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SourceNatRule.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SourceNatRule.java new file mode 100644 index 0000000..b95cb9a --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/SourceNatRule.java @@ -0,0 +1,119 @@ +// +// 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 String toSourceIpAddressMax; + private String toSourceIpAddressMin; + private Integer toSourcePort; + + public SourceNatRule() { + setType("SourceNatRule"); + } + + public String getToSourceIpAddressMax() { + return toSourceIpAddressMax; + } + + public void setToSourceIpAddressMax(final String toSourceIpAddressMax) { + this.toSourceIpAddressMax = toSourceIpAddressMax; + } + + public String getToSourceIpAddressMin() { + return toSourceIpAddressMin; + } + + public void setToSourceIpAddressMin(final String toSourceIpAddressMin) { + this.toSourceIpAddressMin = toSourceIpAddressMin; + } + + public Integer getToSourcePort() { + return toSourcePort; + } + + public void setToSourcePort(final Integer toSourcePort) { + this.toSourcePort = toSourcePort; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((toSourceIpAddressMax == null) ? 0 : toSourceIpAddressMax.hashCode()); + result = prime * result + ((toSourceIpAddressMin == null) ? 0 : toSourceIpAddressMin.hashCode()); + result = prime * result + ((toSourcePort == null) ? 0 : toSourcePort.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + SourceNatRule other = (SourceNatRule)obj; + if (toSourceIpAddressMax == null) { + if (other.toSourceIpAddressMax != null) + return false; + } else if (!toSourceIpAddressMax.equals(other.toSourceIpAddressMax)) + return false; + if (toSourceIpAddressMin == null) { + if (other.toSourceIpAddressMin != null) + return false; + } else if (!toSourceIpAddressMin.equals(other.toSourceIpAddressMin)) + return false; + if (toSourcePort == null) { + if (other.toSourcePort != null) + return false; + } else if (!toSourcePort.equals(other.toSourcePort)) + return false; + return true; + } + + @Override + public boolean equalsIgnoreUuid(final Object obj) { + if (this == obj) + return true; + if (!super.equalsIgnoreUuid(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + SourceNatRule other = (SourceNatRule)obj; + if (toSourceIpAddressMax == null) { + if (other.toSourceIpAddressMax != null) + return false; + } else if (!toSourceIpAddressMax.equals(other.toSourceIpAddressMax)) + return false; + if (toSourceIpAddressMin == null) { + if (other.toSourceIpAddressMin != null) + return false; + } else if (!toSourceIpAddressMin.equals(other.toSourceIpAddressMin)) + return false; + if (toSourcePort == null) { + if (other.toSourcePort != null) + return false; + } else if (!toSourcePort.equals(other.toSourcePort)) + return false; + return true; + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/TransportZoneBinding.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/TransportZoneBinding.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/TransportZoneBinding.java new file mode 100644 index 0000000..c643876 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/TransportZoneBinding.java @@ -0,0 +1,50 @@ +// +// 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 TransportZoneBinding { + private String zoneUuid; + private String transportType; + + public TransportZoneBinding() { + } + + public TransportZoneBinding(String zoneUuid, String transportType) { + this.zoneUuid = zoneUuid; + this.transportType = transportType; + } + + public String getZoneUuid() { + return zoneUuid; + } + + public void setZoneUuid(String zoneUuid) { + this.zoneUuid = zoneUuid; + } + + public String getTransportType() { + return transportType; + } + + public void setTransportType(String transportType) { + this.transportType = transportType; + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/VifAttachment.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/VifAttachment.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/VifAttachment.java new file mode 100644 index 0000000..fe7d93a --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/VifAttachment.java @@ -0,0 +1,78 @@ +// +// 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 org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +public class VifAttachment extends Attachment { + private final String type = "VifAttachment"; + private String vifUuid; + + public VifAttachment() { + } + + public VifAttachment(final String vifUuid) { + this.vifUuid = vifUuid; + } + + public String getVifUuid() { + return vifUuid; + } + + public void setVifUuid(final String vifUuid) { + this.vifUuid = vifUuid; + } + + public String getType() { + return type; + } + + + @Override + public String toString() { + return ReflectionToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE, false); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 31) + .append(this.getClass()) + .append(vifUuid) + .toHashCode(); + } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (!(this.getClass().isInstance(obj))) { + return false; + } + final VifAttachment another = (VifAttachment) obj; + return new EqualsBuilder().append(vifUuid, another.vifUuid).isEquals(); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpRequestWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpRequestWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpRequestWrapper.java new file mode 100644 index 0000000..9d261b2 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpRequestWrapper.java @@ -0,0 +1,77 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource; + +import java.util.Hashtable; +import java.util.Set; + +import org.reflections.Reflections; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.Command; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.RequestWrapper; +import com.cloud.resource.ServerResource; + +public class NiciraNvpRequestWrapper extends RequestWrapper { + + private static NiciraNvpRequestWrapper instance; + + static { + instance = new NiciraNvpRequestWrapper(); + } + + Reflections baseWrappers = new Reflections("com.cloud.network.resource.wrapper"); + @SuppressWarnings("rawtypes") + Set> baseSet = baseWrappers.getSubTypesOf(CommandWrapper.class); + + private NiciraNvpRequestWrapper() { + init(); + } + + @SuppressWarnings("rawtypes") + private void init() { + // NiciraNvpResource commands + final Hashtable, CommandWrapper> niciraCommands = processAnnotations(baseSet); + + resources.put(NiciraNvpResource.class, niciraCommands); + } + + public static NiciraNvpRequestWrapper getInstance() { + return instance; + } + + @SuppressWarnings({"rawtypes" }) + @Override + public Answer execute(final Command command, final ServerResource serverResource) { + final Class resourceClass = serverResource.getClass(); + + final Hashtable, CommandWrapper> resourceCommands = retrieveResource(command, resourceClass); + + CommandWrapper commandWrapper = retrieveCommands(command.getClass(), resourceCommands); + + while (commandWrapper == null) { + //Could not find the command in the given resource, will traverse the family tree. + commandWrapper = retryWhenAllFails(command, resourceClass, resourceCommands); + } + + return commandWrapper.execute(command, serverResource); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java new file mode 100644 index 0000000..df40db4 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java @@ -0,0 +1,319 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource; + +import java.util.Map; + +import javax.naming.ConfigurationException; + +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.PingCommand; +import com.cloud.agent.api.StartupCommand; +import com.cloud.agent.api.StartupNiciraNvpCommand; +import com.cloud.host.Host; +import com.cloud.host.Host.Type; +import com.cloud.network.nicira.ControlClusterStatus; +import com.cloud.network.nicira.DestinationNatRule; +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.SourceNatRule; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.ServerResource; + +public class NiciraNvpResource implements ServerResource { + + private static final Logger s_logger = Logger.getLogger(NiciraNvpResource.class); + + public static final int NAME_MAX_LEN = 40; + public static final int NUM_RETRIES = 2; + + private String name; + private String guid; + private String zoneId; + + private NiciraNvpApi niciraNvpApi; + private NiciraNvpUtilities niciraNvpUtilities; + private CommandRetryUtility retryUtility; + + protected NiciraNvpApi createNiciraNvpApi() { + return new NiciraNvpApi(); + } + + @Override + public boolean configure(final String ignoredName, final Map params) throws ConfigurationException { + + name = (String)params.get("name"); + if (name == null) { + throw new ConfigurationException("Unable to find name"); + } + + guid = (String)params.get("guid"); + if (guid == null) { + throw new ConfigurationException("Unable to find the guid"); + } + + zoneId = (String)params.get("zoneId"); + if (zoneId == null) { + throw new ConfigurationException("Unable to find zone"); + } + + final String ip = (String)params.get("ip"); + if (ip == null) { + throw new ConfigurationException("Unable to find IP"); + } + + final String adminuser = (String)params.get("adminuser"); + if (adminuser == null) { + throw new ConfigurationException("Unable to find admin username"); + } + + final String adminpass = (String)params.get("adminpass"); + if (adminpass == null) { + throw new ConfigurationException("Unable to find admin password"); + } + + niciraNvpUtilities = NiciraNvpUtilities.getInstance(); + retryUtility = CommandRetryUtility.getInstance(); + retryUtility.setServerResource(this); + + niciraNvpApi = createNiciraNvpApi(); + niciraNvpApi.setControllerAddress(ip); + niciraNvpApi.setAdminCredentials(adminuser, adminpass); + + return true; + } + + public NiciraNvpApi getNiciraNvpApi() { + return niciraNvpApi; + } + + public NiciraNvpUtilities getNiciraNvpUtilities() { + return niciraNvpUtilities; + } + + public CommandRetryUtility getRetryUtility() { + return retryUtility; + } + + @Override + public boolean start() { + return true; + } + + @Override + public boolean stop() { + return true; + } + + @Override + public String getName() { + return name; + } + + @Override + public Type getType() { + // Think up a better name for this Type? + return Host.Type.L2Networking; + } + + @Override + public StartupCommand[] initialize() { + final StartupNiciraNvpCommand sc = new StartupNiciraNvpCommand(); + sc.setGuid(guid); + sc.setName(name); + sc.setDataCenter(zoneId); + sc.setPod(""); + sc.setPrivateIpAddress(""); + sc.setStorageIpAddress(""); + sc.setVersion(NiciraNvpResource.class.getPackage().getImplementationVersion()); + return new StartupCommand[] {sc}; + } + + @Override + public PingCommand getCurrentStatus(final long id) { + try { + final ControlClusterStatus ccs = niciraNvpApi.getControlClusterStatus(); + if (!"stable".equals(ccs.getClusterStatus())) { + s_logger.error("ControlCluster state is not stable: " + ccs.getClusterStatus()); + return null; + } + } catch (final NiciraNvpApiException e) { + s_logger.error("getControlClusterStatus failed", e); + return null; + } + return new PingCommand(Host.Type.L2Networking, id); + } + + @Override + public Answer executeRequest(final Command cmd) { + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + try { + return wrapper.execute(cmd, this); + } catch (final Exception e) { + s_logger.debug("Received unsupported command " + cmd.toString()); + return Answer.createUnsupportedCommandAnswer(cmd); + } + } + + @Override + public void disconnected() { + } + + @Override + public IAgentControl getAgentControl() { + return null; + } + + @Override + public void setAgentControl(final IAgentControl agentControl) { + } + + public String natRuleToString(final NatRule rule) { + + final StringBuilder natRuleStr = new StringBuilder(); + natRuleStr.append("Rule "); + natRuleStr.append(rule.getUuid()); + natRuleStr.append(" ("); + natRuleStr.append(rule.getType()); + natRuleStr.append(") :"); + final Match m = rule.getMatch(); + natRuleStr.append("match ("); + natRuleStr.append(m.getProtocol()); + natRuleStr.append(" "); + natRuleStr.append(m.getSourceIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getSourcePort()); + natRuleStr.append(" ] -> "); + natRuleStr.append(m.getDestinationIpAddresses()); + natRuleStr.append(" ["); + natRuleStr.append(m.getDestinationPort()); + natRuleStr.append(" ]) -->"); + if ("SourceNatRule".equals(rule.getType())) { + natRuleStr.append(((SourceNatRule)rule).getToSourceIpAddressMin()); + natRuleStr.append("-"); + natRuleStr.append(((SourceNatRule)rule).getToSourceIpAddressMax()); + natRuleStr.append(" ["); + natRuleStr.append(((SourceNatRule)rule).getToSourcePort()); + natRuleStr.append(" ])"); + } else { + natRuleStr.append(((DestinationNatRule)rule).getToDestinationIpAddress()); + natRuleStr.append(" ["); + natRuleStr.append(((DestinationNatRule)rule).getToDestinationPort()); + natRuleStr.append(" ])"); + } + return natRuleStr.toString(); + } + + public String truncate(final String string, final int length) { + if (string.length() <= length) { + return string; + } else { + return string.substring(0, length); + } + } + + public NatRule[] generateStaticNatRulePair(final String insideIp, final String outsideIp) { + final NatRule[] rulepair = new NatRule[2]; + rulepair[0] = new DestinationNatRule(); + rulepair[0].setType("DestinationNatRule"); + rulepair[0].setOrder(100); + rulepair[1] = new SourceNatRule(); + rulepair[1].setType("SourceNatRule"); + rulepair[1].setOrder(100); + + Match m = new Match(); + m.setDestinationIpAddresses(outsideIp); + rulepair[0].setMatch(m); + ((DestinationNatRule)rulepair[0]).setToDestinationIpAddress(insideIp); + + // create matching snat rule + m = new Match(); + m.setSourceIpAddresses(insideIp); + rulepair[1].setMatch(m); + ((SourceNatRule)rulepair[1]).setToSourceIpAddressMin(outsideIp); + ((SourceNatRule)rulepair[1]).setToSourceIpAddressMax(outsideIp); + + return rulepair; + + } + + public NatRule[] generatePortForwardingRulePair(final String insideIp, final int[] insidePorts, final String outsideIp, final int[] outsidePorts, + final String protocol) { + // Start with a basic static nat rule, then add port and protocol details + final NatRule[] rulepair = generateStaticNatRulePair(insideIp, outsideIp); + + ((DestinationNatRule)rulepair[0]).setToDestinationPort(insidePorts[0]); + rulepair[0].getMatch().setDestinationPort(outsidePorts[0]); + rulepair[0].setOrder(50); + rulepair[0].getMatch().setEthertype("IPv4"); + if ("tcp".equals(protocol)) { + rulepair[0].getMatch().setProtocol(6); + } else if ("udp".equals(protocol)) { + rulepair[0].getMatch().setProtocol(17); + } + + ((SourceNatRule)rulepair[1]).setToSourcePort(outsidePorts[0]); + rulepair[1].getMatch().setSourcePort(insidePorts[0]); + rulepair[1].setOrder(50); + rulepair[1].getMatch().setEthertype("IPv4"); + if ("tcp".equals(protocol)) { + rulepair[1].getMatch().setProtocol(6); + } else if ("udp".equals(protocol)) { + rulepair[1].getMatch().setProtocol(17); + } + + return rulepair; + + } + + @Override + public void setName(final String name) { + // TODO Auto-generated method stub + } + + @Override + public void setConfigParams(final Map params) { + // TODO Auto-generated method stub + } + + @Override + public Map getConfigParams() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getRunLevel() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void setRunLevel(final int level) { + // TODO Auto-generated method stub + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpUtilities.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpUtilities.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpUtilities.java new file mode 100644 index 0000000..4edf5c3 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpUtilities.java @@ -0,0 +1,65 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource; + +import java.util.ArrayList; +import java.util.List; + +import com.cloud.agent.api.CreateLogicalSwitchPortCommand; +import com.cloud.network.nicira.LogicalSwitch; +import com.cloud.network.nicira.LogicalSwitchPort; +import com.cloud.network.nicira.NiciraNvpTag; +import com.cloud.network.nicira.VifAttachment; + +public class NiciraNvpUtilities { + + private static NiciraNvpUtilities instance; + + static { + instance = new NiciraNvpUtilities(); + } + + private NiciraNvpUtilities() { + } + + public static NiciraNvpUtilities getInstance() { + return instance; + } + + public LogicalSwitch createLogicalSwitch() { + final LogicalSwitch logicalSwitch = new LogicalSwitch(); + return logicalSwitch; + } + + public LogicalSwitchPort createLogicalSwitchPort(final CreateLogicalSwitchPortCommand command) { + final String attachmentUuid = command.getAttachmentUuid(); + + // Tags set to scope cs_account and account name + final List tags = new ArrayList(); + tags.add(new NiciraNvpTag("cs_account", command.getOwnerName())); + + final LogicalSwitchPort logicalSwitchPort = new LogicalSwitchPort(attachmentUuid, tags, true); + return logicalSwitchPort; + } + + public VifAttachment createVifAttachment(final String attachmentUuid) { + return new VifAttachment(attachmentUuid); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java new file mode 100644 index 0000000..89e7a6e --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java @@ -0,0 +1,120 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand; +import com.cloud.agent.api.to.PortForwardingRuleTO; +import com.cloud.network.nicira.NatRule; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = ConfigurePortForwardingRulesOnLogicalRouterCommand.class) +public final class NiciraNvpConfigurePortForwardingRulesCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(NiciraNvpConfigurePortForwardingRulesCommandWrapper.class); + + @Override + public Answer execute(final ConfigurePortForwardingRulesOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + try { + final NiciraNvpList existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.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 (final PortForwardingRuleTO rule : command.getRules()) { + if (rule.isAlreadyAdded() && !rule.revoked()) { + // Don't need to do anything + continue; + } + + if (rule.getDstPortRange()[0] != rule.getDstPortRange()[1] || rule.getSrcPortRange()[0] != rule.getSrcPortRange()[1]) { + return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(command, false, "Nicira NVP doesn't support port ranges for port forwarding"); + } + + final NatRule[] rulepair = niciraNvpResource.generatePortForwardingRulePair(rule.getDstIp(), rule.getDstPortRange(), rule.getSrcIp(), rule.getSrcPortRange(), rule.getProtocol()); + + NatRule incoming = null; + NatRule outgoing = null; + + for (final NatRule storedRule : existingRules.getResults()) { + if (storedRule.equalsIgnoreUuid(rulepair[1])) { + // The outgoing rule exists + outgoing = storedRule; + s_logger.debug("Found matching outgoing rule " + outgoing.getUuid()); + if (incoming != null) { + break; + } + } else if (storedRule.equalsIgnoreUuid(rulepair[0])) { + // The incoming rule exists + incoming = storedRule; + s_logger.debug("Found matching incoming rule " + incoming.getUuid()); + if (outgoing != null) { + break; + } + } + } + if (incoming != null && outgoing != null) { + if (rule.revoked()) { + s_logger.debug("Deleting incoming rule " + incoming.getUuid()); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), incoming.getUuid()); + + s_logger.debug("Deleting outgoing rule " + outgoing.getUuid()); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), outgoing.getUuid()); + } + } else { + if (rule.revoked()) { + s_logger.warn("Tried deleting a rule that does not exist, " + rule.getSrcIp() + " -> " + rule.getDstIp()); + break; + } + + rulepair[0] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0]); + s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[0])); + + try { + rulepair[1] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[1]); + s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[1])); + } catch (final NiciraNvpApiException ex) { + s_logger.warn("NiciraNvpApiException during create call, rolling back previous create"); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0].getUuid()); + throw ex; // Rethrow the original exception + } + + } + } + return new ConfigurePortForwardingRulesOnLogicalRouterAnswer(command, true, command.getRules().size() + " PortForwarding rules applied"); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, ConfigurePortForwardingRulesOnLogicalRouterAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java new file mode 100644 index 0000000..e5d2dcd --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java @@ -0,0 +1,60 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; +import com.cloud.network.nicira.LogicalRouterPort; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = ConfigurePublicIpsOnLogicalRouterCommand.class) +public final class NiciraNvpConfigurePublicIpsCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final ConfigurePublicIpsOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + final NiciraNvpList ports = niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid()); + if (ports.getResultCount() != 1) { + return new ConfigurePublicIpsOnLogicalRouterAnswer(command, false, "No logical router ports found, unable to set ip addresses"); + } + final LogicalRouterPort lrp = ports.getResults().get(0); + lrp.setIpAddresses(command.getPublicCidrs()); + niciraNvpApi.updateLogicalRouterPort(command.getLogicalRouterUuid(), lrp); + + return new ConfigurePublicIpsOnLogicalRouterAnswer(command, true, "Configured " + command.getPublicCidrs().size() + " ip addresses on logical router uuid " + + command.getLogicalRouterUuid()); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, ConfigurePublicIpsOnLogicalRouterAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java new file mode 100644 index 0000000..1ce3ceb --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java @@ -0,0 +1,113 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand; +import com.cloud.agent.api.to.StaticNatRuleTO; +import com.cloud.network.nicira.NatRule; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = ConfigureStaticNatRulesOnLogicalRouterCommand.class) +public final class NiciraNvpConfigureStaticNatRulesCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(NiciraNvpConfigureStaticNatRulesCommandWrapper.class); + + @Override + public Answer execute(final ConfigureStaticNatRulesOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + final NiciraNvpList existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.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 (final StaticNatRuleTO rule : command.getRules()) { + + final NatRule[] rulepair = niciraNvpResource.generateStaticNatRulePair(rule.getDstIp(), rule.getSrcIp()); + + NatRule incoming = null; + NatRule outgoing = null; + + for (final NatRule storedRule : existingRules.getResults()) { + if (storedRule.equalsIgnoreUuid(rulepair[1])) { + // The outgoing rule exists + outgoing = storedRule; + s_logger.debug("Found matching outgoing rule " + outgoing.getUuid()); + if (incoming != null) { + break; + } + } else if (storedRule.equalsIgnoreUuid(rulepair[0])) { + // The incoming rule exists + incoming = storedRule; + s_logger.debug("Found matching incoming rule " + incoming.getUuid()); + if (outgoing != null) { + break; + } + } + } + if (incoming != null && outgoing != null) { + if (rule.revoked()) { + s_logger.debug("Deleting incoming rule " + incoming.getUuid()); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), incoming.getUuid()); + + s_logger.debug("Deleting outgoing rule " + outgoing.getUuid()); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), outgoing.getUuid()); + } + } else { + if (rule.revoked()) { + s_logger.warn("Tried deleting a rule that does not exist, " + rule.getSrcIp() + " -> " + rule.getDstIp()); + break; + } + + rulepair[0] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0]); + s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[0])); + + try { + rulepair[1] = niciraNvpApi.createLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[1]); + s_logger.debug("Created " + niciraNvpResource.natRuleToString(rulepair[1])); + } catch (final NiciraNvpApiException ex) { + s_logger.debug("Failed to create SourceNatRule, rolling back DestinationNatRule"); + niciraNvpApi.deleteLogicalRouterNatRule(command.getLogicalRouterUuid(), rulepair[0].getUuid()); + throw ex; // Rethrow original exception + } + + } + } + return new ConfigureStaticNatRulesOnLogicalRouterAnswer(command, true, command.getRules().size() + " StaticNat rules applied"); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, ConfigureStaticNatRulesOnLogicalRouterAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalRouterCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalRouterCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalRouterCommandWrapper.java new file mode 100644 index 0000000..1cddf4f --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalRouterCommandWrapper.java @@ -0,0 +1,152 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NAME_MAX_LEN; +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CreateLogicalRouterAnswer; +import com.cloud.agent.api.CreateLogicalRouterCommand; +import com.cloud.network.nicira.L3GatewayAttachment; +import com.cloud.network.nicira.LogicalRouter; +import com.cloud.network.nicira.LogicalRouterPort; +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.NiciraNvpTag; +import com.cloud.network.nicira.PatchAttachment; +import com.cloud.network.nicira.RouterNextHop; +import com.cloud.network.nicira.SingleDefaultRouteImplicitRoutingConfig; +import com.cloud.network.nicira.SourceNatRule; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = CreateLogicalRouterCommand.class) +public final class NiciraNvpCreateLogicalRouterCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(NiciraNvpCreateLogicalRouterCommandWrapper.class); + + @Override + public Answer execute(final CreateLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) { + final String routerName = command.getName(); + final String gatewayServiceUuid = command.getGatewayServiceUuid(); + final String logicalSwitchUuid = command.getLogicalSwitchUuid(); + + final List tags = new ArrayList(); + tags.add(new NiciraNvpTag("cs_account", command.getOwnerName())); + + final String publicNetworkNextHopIp = command.getPublicNextHop(); + final String publicNetworkIpAddress = command.getPublicIpCidr(); + final String internalNetworkAddress = command.getInternalIpCidr(); + + s_logger.debug("Creating a logical router with external ip " + publicNetworkIpAddress + " and internal ip " + internalNetworkAddress + "on gateway service " + + gatewayServiceUuid); + + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + // Create the Router + LogicalRouter lrc = new LogicalRouter(); + lrc.setDisplayName(niciraNvpResource.truncate(routerName, NAME_MAX_LEN)); + lrc.setTags(tags); + lrc.setRoutingConfig(new SingleDefaultRouteImplicitRoutingConfig(new RouterNextHop(publicNetworkNextHopIp))); + lrc = niciraNvpApi.createLogicalRouter(lrc); + + // store the switchport for rollback + LogicalSwitchPort lsp = null; + + try { + // Create the outside port for the router + LogicalRouterPort lrpo = new LogicalRouterPort(); + lrpo.setAdminStatusEnabled(true); + lrpo.setDisplayName(niciraNvpResource.truncate(routerName + "-outside-port", NAME_MAX_LEN)); + lrpo.setTags(tags); + final 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 + final L3GatewayAttachment attachment = new L3GatewayAttachment(gatewayServiceUuid); + if (command.getVlanId() != 0) { + attachment.setVlanId(command.getVlanId()); + } + niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpo.getUuid(), attachment); + + // Create the inside port for the router + LogicalRouterPort lrpi = new LogicalRouterPort(); + lrpi.setAdminStatusEnabled(true); + lrpi.setDisplayName(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN)); + lrpi.setTags(tags); + final List insideIpAddresses = new ArrayList(); + insideIpAddresses.add(internalNetworkAddress); + lrpi.setIpAddresses(insideIpAddresses); + lrpi = niciraNvpApi.createLogicalRouterPort(lrc.getUuid(), lrpi); + + // Create the inside port on the lswitch + lsp = new LogicalSwitchPort(niciraNvpResource.truncate(routerName + "-inside-port", NAME_MAX_LEN), tags, true); + lsp = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp); + + // Attach the inside router port to the lswitch port with a PatchAttachment + niciraNvpApi.updateLogicalRouterPortAttachment(lrc.getUuid(), lrpi.getUuid(), new PatchAttachment(lsp.getUuid())); + + // Attach the inside lswitch port to the router with a PatchAttachment + niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lsp.getUuid(), new PatchAttachment(lrpi.getUuid())); + + // Setup the source nat rule + final SourceNatRule snr = new SourceNatRule(); + snr.setToSourceIpAddressMin(publicNetworkIpAddress.split("/")[0]); + snr.setToSourceIpAddressMax(publicNetworkIpAddress.split("/")[0]); + final Match match = new Match(); + match.setSourceIpAddresses(internalNetworkAddress); + snr.setMatch(match); + snr.setOrder(200); + niciraNvpApi.createLogicalRouterNatRule(lrc.getUuid(), snr); + } catch (final NiciraNvpApiException e) { + // We need to destroy the router if we already created it + // this will also take care of any router ports and rules + try { + niciraNvpApi.deleteLogicalRouter(lrc.getUuid()); + if (lsp != null) { + niciraNvpApi.deleteLogicalSwitchPort(logicalSwitchUuid, lsp.getUuid()); + } + } catch (final NiciraNvpApiException ex) { + } + + throw e; + } + + return new CreateLogicalRouterAnswer(command, true, "Logical Router created (uuid " + lrc.getUuid() + ")", lrc.getUuid()); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, CreateLogicalRouterAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java new file mode 100644 index 0000000..7d66ada --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchCommandWrapper.java @@ -0,0 +1,73 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import java.util.ArrayList; +import java.util.List; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CreateLogicalSwitchAnswer; +import com.cloud.agent.api.CreateLogicalSwitchCommand; +import com.cloud.network.nicira.LogicalSwitch; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpTag; +import com.cloud.network.nicira.TransportZoneBinding; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.resource.NiciraNvpUtilities; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = CreateLogicalSwitchCommand.class) +public final class NiciraNvpCreateLogicalSwitchCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final CreateLogicalSwitchCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities(); + + LogicalSwitch logicalSwitch = niciraNvpUtilities.createLogicalSwitch(); + logicalSwitch.setDisplayName(niciraNvpResource.truncate("lswitch-" + command.getName(), NiciraNvpResource.NAME_MAX_LEN)); + logicalSwitch.setPortIsolationEnabled(false); + + // Set transport binding + final List ltzb = new ArrayList(); + ltzb.add(new TransportZoneBinding(command.getTransportUuid(), command.getTransportType())); + logicalSwitch.setTransportZones(ltzb); + + // Tags set to scope cs_account and account name + final List tags = new ArrayList(); + tags.add(new NiciraNvpTag("cs_account", command.getOwnerName())); + logicalSwitch.setTags(tags); + + try { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + logicalSwitch = niciraNvpApi.createLogicalSwitch(logicalSwitch); + final String switchUuid = logicalSwitch.getUuid(); + return new CreateLogicalSwitchAnswer(command, true, "Logicalswitch " + switchUuid + " created", switchUuid); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, CreateLogicalSwitchAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchPortCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchPortCommandWrapper.java new file mode 100644 index 0000000..42a4067 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpCreateLogicalSwitchPortCommandWrapper.java @@ -0,0 +1,70 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; +import com.cloud.agent.api.CreateLogicalSwitchPortCommand; +import com.cloud.network.nicira.LogicalSwitchPort; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.VifAttachment; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.resource.NiciraNvpUtilities; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = CreateLogicalSwitchPortCommand.class) +public final class NiciraNvpCreateLogicalSwitchPortCommandWrapper extends CommandWrapper { + + private static final Logger s_logger = Logger.getLogger(NiciraNvpCreateLogicalSwitchPortCommandWrapper.class); + + @Override + public Answer execute(final CreateLogicalSwitchPortCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities(); + + final String logicalSwitchUuid = command.getLogicalSwitchUuid(); + final String attachmentUuid = command.getAttachmentUuid(); + + try { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + final LogicalSwitchPort logicalSwitchPort = niciraNvpUtilities.createLogicalSwitchPort(command); + final LogicalSwitchPort newPort = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, logicalSwitchPort); + try { + niciraNvpApi.updateLogicalSwitchPortAttachment(command.getLogicalSwitchUuid(), newPort.getUuid(), new VifAttachment(attachmentUuid)); + } catch (final NiciraNvpApiException ex) { + s_logger.warn("modifyLogicalSwitchPort failed after switchport was created, removing switchport"); + niciraNvpApi.deleteLogicalSwitchPort(command.getLogicalSwitchUuid(), newPort.getUuid()); + throw ex; // Rethrow the original exception + } + return new CreateLogicalSwitchPortAnswer(command, true, "Logical switch port " + newPort.getUuid() + " created", newPort.getUuid()); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, CreateLogicalSwitchPortAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalRouterCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalRouterCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalRouterCommandWrapper.java new file mode 100644 index 0000000..43665b2 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalRouterCommandWrapper.java @@ -0,0 +1,50 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.DeleteLogicalRouterAnswer; +import com.cloud.agent.api.DeleteLogicalRouterCommand; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = DeleteLogicalRouterCommand.class) +public final class NiciraNvpDeleteLogicalRouterCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final DeleteLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + niciraNvpApi.deleteLogicalRouter(command.getLogicalRouterUuid()); + return new DeleteLogicalRouterAnswer(command, true, "Logical Router deleted (uuid " + command.getLogicalRouterUuid() + ")"); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, DeleteLogicalRouterAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchCommandWrapper.java new file mode 100644 index 0000000..f7b8ab9 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchCommandWrapper.java @@ -0,0 +1,49 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.DeleteLogicalSwitchAnswer; +import com.cloud.agent.api.DeleteLogicalSwitchCommand; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = DeleteLogicalSwitchCommand.class) +public final class NiciraNvpDeleteLogicalSwitchCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final DeleteLogicalSwitchCommand command, final NiciraNvpResource niciraNvpResource) { + try { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + niciraNvpApi.deleteLogicalSwitch(command.getLogicalSwitchUuid()); + return new DeleteLogicalSwitchAnswer(command, true, "Logicalswitch " + command.getLogicalSwitchUuid() + " deleted"); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, DeleteLogicalSwitchAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchPortCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchPortCommandWrapper.java new file mode 100644 index 0000000..b8c69f2 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpDeleteLogicalSwitchPortCommandWrapper.java @@ -0,0 +1,50 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer; +import com.cloud.agent.api.DeleteLogicalSwitchPortCommand; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = DeleteLogicalSwitchPortCommand.class) +public final class NiciraNvpDeleteLogicalSwitchPortCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final DeleteLogicalSwitchPortCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + niciraNvpApi.deleteLogicalSwitchPort(command.getLogicalSwitchUuid(), command.getLogicalSwitchPortUuid()); + return new DeleteLogicalSwitchPortAnswer(command, true, "Logical switch port " + command.getLogicalSwitchPortUuid() + " deleted"); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, DeleteLogicalSwitchPortAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java new file mode 100644 index 0000000..40d58fc --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java @@ -0,0 +1,59 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.FindLogicalSwitchPortAnswer; +import com.cloud.agent.api.FindLogicalSwitchPortCommand; +import com.cloud.network.nicira.LogicalSwitchPort; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = FindLogicalSwitchPortCommand.class) +public final class NiciraNvpFindLogicalSwitchPortCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final FindLogicalSwitchPortCommand command, final NiciraNvpResource niciraNvpResource) { + final String logicalSwitchUuid = command.getLogicalSwitchUuid(); + final String logicalSwitchPortUuid = command.getLogicalSwitchPortUuid(); + + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + final NiciraNvpList ports = niciraNvpApi.findLogicalSwitchPortsByUuid(logicalSwitchUuid, logicalSwitchPortUuid); + if (ports.getResultCount() == 0) { + return new FindLogicalSwitchPortAnswer(command, false, "Logical switchport " + logicalSwitchPortUuid + " not found", null); + } else { + return new FindLogicalSwitchPortAnswer(command, true, "Logical switchport " + logicalSwitchPortUuid + " found", logicalSwitchPortUuid); + } + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, FindLogicalSwitchPortAnswer.class, e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpMaintainCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpMaintainCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpMaintainCommandWrapper.java new file mode 100644 index 0000000..3015378 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpMaintainCommandWrapper.java @@ -0,0 +1,36 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.MaintainAnswer; +import com.cloud.agent.api.MaintainCommand; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = MaintainCommand.class) +public final class NiciraNvpMaintainCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final MaintainCommand command, final NiciraNvpResource niciraNvpResource) { + return new MaintainAnswer(command); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpReadyCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpReadyCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpReadyCommandWrapper.java new file mode 100644 index 0000000..3d0d3f3 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpReadyCommandWrapper.java @@ -0,0 +1,36 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ReadyAnswer; +import com.cloud.agent.api.ReadyCommand; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = ReadyCommand.class) +public final class NiciraNvpReadyCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final ReadyCommand command, final NiciraNvpResource niciraNvpResource) { + return new ReadyAnswer(command); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpUpdateLogicalSwitchPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpUpdateLogicalSwitchPortCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpUpdateLogicalSwitchPortCommandWrapper.java new file mode 100644 index 0000000..a0d7dc2 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpUpdateLogicalSwitchPortCommandWrapper.java @@ -0,0 +1,68 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.network.resource.wrapper; + +import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES; + +import java.util.ArrayList; +import java.util.List; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer; +import com.cloud.agent.api.UpdateLogicalSwitchPortCommand; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpTag; +import com.cloud.network.nicira.VifAttachment; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.network.resource.NiciraNvpUtilities; +import com.cloud.network.utils.CommandRetryUtility; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = UpdateLogicalSwitchPortCommand.class) +public final class NiciraNvpUpdateLogicalSwitchPortCommandWrapper extends CommandWrapper { + + @Override + public Answer execute(final UpdateLogicalSwitchPortCommand command, final NiciraNvpResource niciraNvpResource) { + final NiciraNvpUtilities niciraNvpUtilities = niciraNvpResource.getNiciraNvpUtilities(); + + final String logicalSwitchUuid = command.getLogicalSwitchUuid(); + final String logicalSwitchPortUuid = command.getLogicalSwitchPortUuid(); + final String attachmentUuid = command.getAttachmentUuid(); + + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + try { + // Tags set to scope cs_account and account name + final List tags = new ArrayList(); + tags.add(new NiciraNvpTag("cs_account", command.getOwnerName())); + + final VifAttachment vifAttachment = niciraNvpUtilities.createVifAttachment(attachmentUuid); + + niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, logicalSwitchPortUuid, vifAttachment); + return new UpdateLogicalSwitchPortAnswer(command, true, "Attachment for " + logicalSwitchPortUuid + " updated", logicalSwitchPortUuid); + } catch (final NiciraNvpApiException e) { + final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility(); + retryUtility.addRetry(command, NUM_RETRIES); + return retryUtility.retry(command, UpdateLogicalSwitchPortAnswer.class, e); + } + } +} \ No newline at end of file