diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java b/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java index 5503d24d837..bac33397bbb 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java @@ -63,6 +63,8 @@ import com.cloud.agent.resource.virtualnetwork.model.IpAddress; import com.cloud.agent.resource.virtualnetwork.model.IpAssociation; import com.cloud.agent.resource.virtualnetwork.model.NetworkACL; import com.cloud.agent.resource.virtualnetwork.model.ProtocolAclRule; +import com.cloud.agent.resource.virtualnetwork.model.StaticNatRule; +import com.cloud.agent.resource.virtualnetwork.model.StaticNatRules; import com.cloud.agent.resource.virtualnetwork.model.TcpAclRule; import com.cloud.agent.resource.virtualnetwork.model.UdpAclRule; import com.cloud.agent.resource.virtualnetwork.model.VmData; @@ -91,11 +93,11 @@ public class ConfigHelper { if (cmd instanceof SetPortForwardingRulesVpcCommand) { cfg = generateConfig((SetPortForwardingRulesVpcCommand)cmd); } else if (cmd instanceof SetPortForwardingRulesCommand) { - cfg = generateConfig((SetPortForwardingRulesCommand)cmd); + cfg = generateConfig((SetPortForwardingRulesCommand)cmd); // Migrated } else if (cmd instanceof SetStaticRouteCommand) { cfg = generateConfig((SetStaticRouteCommand)cmd); } else if (cmd instanceof SetStaticNatRulesCommand) { - cfg = generateConfig((SetStaticNatRulesCommand)cmd); + cfg = generateConfig((SetStaticNatRulesCommand)cmd); // Migrated } else if (cmd instanceof LoadBalancerConfigCommand) { cfg = generateConfig((LoadBalancerConfigCommand)cmd); } else if (cmd instanceof SavePasswordCommand) { @@ -227,34 +229,15 @@ public class ConfigHelper { } private static List generateConfig(SetStaticNatRulesCommand cmd) { - LinkedList cfg = new LinkedList<>(); - if (cmd.getVpcId() != null) { - for (StaticNatRuleTO rule : cmd.getRules()) { - String args = rule.revoked() ? " -D" : " -A"; - args += " -l " + rule.getSrcIp(); - args += " -r " + rule.getDstIp(); - cfg.add(new ScriptConfigItem(VRScripts.VPC_STATIC_NAT, args)); - } - } else { - for (StaticNatRuleTO rule : cmd.getRules()) { - //1:1 NAT needs instanceip;publicip;domrip;op - StringBuilder args = new StringBuilder(); - args.append(rule.revoked() ? " -D " : " -A "); - args.append(" -l ").append(rule.getSrcIp()); - args.append(" -r ").append(rule.getDstIp()); - - if (rule.getProtocol() != null) { - args.append(" -P ").append(rule.getProtocol().toLowerCase()); - } - - args.append(" -d ").append(rule.getStringSrcPortRange()); - args.append(" -G "); - - cfg.add(new ScriptConfigItem(VRScripts.FIREWALL_NAT, args.toString())); - } + LinkedList rules = new LinkedList<>(); + for (StaticNatRuleTO rule : cmd.getRules()) { + StaticNatRule staticNatRule = new StaticNatRule(rule.revoked(), rule.getProtocol(), rule.getSrcIp(), rule.getStringSrcPortRange(), rule.getDstIp()); + rules.add(staticNatRule); } - return cfg; + StaticNatRules staticNatRules = new StaticNatRules(rules); + + return generateConfigItems(staticNatRules); } private static List generateConfig(LoadBalancerConfigCommand cmd) { @@ -603,6 +586,9 @@ public class ConfigHelper { case ConfigBase.NETWORK_ACL: destinationFile = VRScripts.NETWORK_ACL_CONFIG; break; + case ConfigBase.STATICNAT_RULES: + destinationFile = VRScripts.STATICNAT_RULES_CONFIG; + break; case ConfigBase.VM_DHCP: destinationFile = VRScripts.VM_DHCP_CONFIG; break; diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java b/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java index 65c52e990ee..6c09831b862 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java @@ -29,6 +29,7 @@ public class VRScripts { protected final static String VM_PASSWORD_CONFIG = "vm_password.json"; protected static final String FORWARDING_RULES_CONFIG = "forwarding_rules.json"; protected static final String VPN_USER_LIST_CONFIG = "vpn_user_list.json"; + protected static final String STATICNAT_RULES_CONFIG = "staticnat_rules.json"; protected final static String CONFIG_CACHE_LOCATION = "/var/cache/cloud/"; protected final static int DEFAULT_EXECUTEINVR_TIMEOUT = 120; //Seconds diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java index 171261ad309..4b8b4cbc2e7 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java @@ -29,6 +29,7 @@ public abstract class ConfigBase { public static final String VM_PASSWORD = "vmpassword"; public static final String FORWARDING_RULES = "forwardrules"; public static final String VPN_USER_LIST = "vpnuserlist"; + public static final String STATICNAT_RULES = "staticnatrules"; private String type = UNKNOWN; diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRule.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRule.java new file mode 100644 index 00000000000..a375a913b28 --- /dev/null +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRule.java @@ -0,0 +1,82 @@ +// +// 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.resource.virtualnetwork.model; + +public class StaticNatRule { + private boolean revoke; + private String protocol; + private String sourceIpAddress; + private String sourcePortRange; + private String destinationIpAddress; + + public StaticNatRule() { + // Empty constructor for (de)serialization + } + + public StaticNatRule(boolean revoke, String protocol, String sourceIpAddress, String sourcePortRange, String destinationIpAddress) { + super(); + this.revoke = revoke; + this.protocol = protocol; + this.sourceIpAddress = sourceIpAddress; + this.sourcePortRange = sourcePortRange; + this.destinationIpAddress = destinationIpAddress; + } + + public boolean isRevoke() { + return revoke; + } + + public void setRevoke(boolean revoke) { + this.revoke = revoke; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public String getSourceIpAddress() { + return sourceIpAddress; + } + + public void setSourceIpAddress(String sourceIpAddress) { + this.sourceIpAddress = sourceIpAddress; + } + + public String getSourcePortRange() { + return sourcePortRange; + } + + public void setSourcePortRange(String sourcePortRange) { + this.sourcePortRange = sourcePortRange; + } + + public String getDestinationIpAddress() { + return destinationIpAddress; + } + + public void setDestinationIpAddress(String destinationIpAddress) { + this.destinationIpAddress = destinationIpAddress; + } + +} diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRules.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRules.java new file mode 100644 index 00000000000..606adddb90a --- /dev/null +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/StaticNatRules.java @@ -0,0 +1,44 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.agent.resource.virtualnetwork.model; + +import java.util.List; + +public class StaticNatRules extends ConfigBase { + private List rules; + + public StaticNatRules() { + super(ConfigBase.STATICNAT_RULES); + } + + public StaticNatRules(List rules) { + super(ConfigBase.STATICNAT_RULES); + this.rules = rules; + } + + public List getRules() { + return rules; + } + + public void setRules(List rules) { + this.rules = rules; + } + +}