From 5769fde46b5a20aa48b8a854eb8c3bfc99960e24 Mon Sep 17 00:00:00 2001 From: Murali Reddy Date: Thu, 19 May 2011 18:30:24 +0530 Subject: [PATCH] bug 7194:New API for adding range of public & private ports in port forwarding rule addition adding support for port ranges in port forwarding rules. --- api/src/com/cloud/api/ApiConstants.java | 4 ++ .../commands/CreatePortForwardingRuleCmd.java | 32 ++++++------- .../api/response/FirewallRuleResponse.java | 48 ++++++++++++++----- .../src/com/cloud/api/ApiResponseHelper.java | 6 ++- .../cloud/network/rules/RulesManagerImpl.java | 5 ++ 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/api/src/com/cloud/api/ApiConstants.java b/api/src/com/cloud/api/ApiConstants.java index 6faa8879713..a3e6e5365ab 100755 --- a/api/src/com/cloud/api/ApiConstants.java +++ b/api/src/com/cloud/api/ApiConstants.java @@ -126,12 +126,16 @@ public class ApiConstants { public static final String PRIVATE_INTERFACE = "privateinterface"; public static final String PRIVATE_IP = "privateip"; public static final String PRIVATE_PORT = "privateport"; + public static final String PRIVATE_START_PORT = "privateport"; + public static final String PRIVATE_END_PORT = "privateendport"; public static final String PRIVATE_ZONE = "privatezone"; public static final String PROTOCOL = "protocol"; public static final String PUBLIC_INTERFACE = "publicinterface"; public static final String PUBLIC_IP_ID = "publicipid"; public static final String PUBLIC_IP = "publicip"; public static final String PUBLIC_PORT = "publicport"; + public static final String PUBLIC_START_PORT = "publicport"; + public static final String PUBLIC_END_PORT = "publicendport"; public static final String PUBLIC_ZONE = "publiczone"; public static final String RECEIVED_BYTES = "receivedbytes"; public static final String REQUIRES_HVM = "requireshvm"; diff --git a/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java b/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java index 94c93a097ac..cb0c8b489e0 100644 --- a/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java +++ b/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java @@ -55,14 +55,20 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P @Parameter(name = ApiConstants.IP_ADDRESS_ID, type = CommandType.LONG, required = true, description = "the IP address id of the port forwarding rule") private Long ipAddressId; - @Parameter(name = ApiConstants.PRIVATE_PORT, type = CommandType.INTEGER, required = true, description = "the private port of the port forwarding rule") - private Integer privatePort; + @Parameter(name = ApiConstants.PRIVATE_START_PORT, type = CommandType.INTEGER, required = true, description = "the starting port of port forwarding rule's private port range") + private Integer privateStartPort; + + @Parameter(name = ApiConstants.PRIVATE_END_PORT, type = CommandType.INTEGER, required = false, description = "the ending port of port forwarding rule's private port range") + private Integer privateEndPort; @Parameter(name = ApiConstants.PROTOCOL, type = CommandType.STRING, required = true, description = "the protocol for the port fowarding rule. Valid values are TCP or UDP.") private String protocol; - @Parameter(name = ApiConstants.PUBLIC_PORT, type = CommandType.INTEGER, required = true, description = "the public port of the port forwarding rule") - private Integer publicPort; + @Parameter(name = ApiConstants.PUBLIC_START_PORT, type = CommandType.INTEGER, required = true, description = "the starting port of port forwarding rule's public port range") + private Integer publicStartPort; + + @Parameter(name = ApiConstants.PUBLIC_END_PORT, type = CommandType.INTEGER, required = false, description = "the ending port of port forwarding rule's private port range") + private Integer publicEndPort; @Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.LONG, required = true, description = "the ID of the virtual machine for the port forwarding rule") private Long virtualMachineId; @@ -70,7 +76,7 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P @Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from") private List cidrlist; - + // /////////////////////////////////////////////////// // ///////////////// Accessors /////////////////////// // /////////////////////////////////////////////////// @@ -79,19 +85,11 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P return ipAddressId; } - public Integer getPrivatePort() { - return privatePort; - } - @Override public String getProtocol() { return protocol.trim(); } - public Integer getPublicPort() { - return publicPort; - } - @Override public long getVirtualMachineId() { return virtualMachineId; @@ -158,12 +156,12 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P @Override public int getSourcePortStart() { - return publicPort.intValue(); + return publicStartPort.intValue(); } @Override public int getSourcePortEnd() { - return publicPort.intValue(); + return (publicEndPort == null)? publicStartPort.intValue() : publicEndPort.intValue(); } @Override @@ -205,12 +203,12 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P @Override public int getDestinationPortStart() { - return privatePort.intValue(); + return privateStartPort.intValue(); } @Override public int getDestinationPortEnd() { - return privatePort.intValue(); + return (privateEndPort == null)? privateStartPort.intValue() : privateEndPort.intValue(); } @Override diff --git a/api/src/com/cloud/api/response/FirewallRuleResponse.java b/api/src/com/cloud/api/response/FirewallRuleResponse.java index f9be318639b..f89ad2c4e8a 100644 --- a/api/src/com/cloud/api/response/FirewallRuleResponse.java +++ b/api/src/com/cloud/api/response/FirewallRuleResponse.java @@ -18,6 +18,8 @@ package com.cloud.api.response; import com.cloud.api.ApiConstants; +import com.cloud.api.Parameter; +import com.cloud.api.BaseCmd.CommandType; import com.cloud.serializer.Param; import com.google.gson.annotations.SerializedName; @@ -25,14 +27,20 @@ public class FirewallRuleResponse extends BaseResponse { @SerializedName(ApiConstants.ID) @Param(description="the ID of the port forwarding rule") private Long id; - @SerializedName(ApiConstants.PRIVATE_PORT) @Param(description="the private port for the port forwarding rule") - private String privatePort; + @SerializedName(ApiConstants.PRIVATE_START_PORT) @Param(description = "the starting port of port forwarding rule's private port range") + private String privateStartPort; + + @SerializedName(ApiConstants.PRIVATE_END_PORT) @Param(description = "the ending port of port forwarding rule's private port range") + private String privateEndPort; @SerializedName(ApiConstants.PROTOCOL) @Param(description="the protocol of the port forwarding rule") private String protocol; - @SerializedName(ApiConstants.PUBLIC_PORT) @Param(description="the public port for the port forwarding rule") - private String publicPort; + @SerializedName(ApiConstants.PUBLIC_START_PORT) @Param(description="the starting port of port forwarding rule's public port range") + private String publicStartPort; + + @SerializedName(ApiConstants.PUBLIC_END_PORT) @Param(description = "the ending port of port forwarding rule's private port range") + private String publicEndPort; @SerializedName(ApiConstants.VIRTUAL_MACHINE_ID) @Param(description="the VM ID for the port forwarding rule") private Long virtualMachineId; @@ -60,14 +68,22 @@ public class FirewallRuleResponse extends BaseResponse { this.id = id; } - public String getPrivatePort() { - return privatePort; + public String getPrivateStartPort() { + return privateStartPort; } - public void setPrivatePort(String privatePort) { - this.privatePort = privatePort; + public String getPrivateEndPort() { + return privateEndPort; } + public void setPrivateStartPort(String privatePort) { + this.privateStartPort = privatePort; + } + + public void setPrivateEndPort(String privatePort) { + this.privateEndPort = privatePort; + } + public String getProtocol() { return protocol; } @@ -76,12 +92,20 @@ public class FirewallRuleResponse extends BaseResponse { this.protocol = protocol; } - public String getPublicPort() { - return publicPort; + public String getPublicStartPort() { + return publicStartPort; } - public void setPublicPort(String publicPort) { - this.publicPort = publicPort; + public String getPublicEndPort() { + return publicEndPort; + } + + public void setPublicStartPort(String publicPort) { + this.publicStartPort = publicPort; + } + + public void setPublicEndPort(String publicPort) { + this.publicEndPort = publicPort; } public Long getVirtualMachineId() { diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 463a1843d58..7157640c86a 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -952,9 +952,11 @@ public class ApiResponseHelper implements ResponseGenerator { public FirewallRuleResponse createFirewallRuleResponse(PortForwardingRule fwRule) { FirewallRuleResponse response = new FirewallRuleResponse(); response.setId(fwRule.getId()); - response.setPrivatePort(Integer.toString(fwRule.getDestinationPortStart())); + response.setPrivateStartPort(Integer.toString(fwRule.getDestinationPortStart())); + response.setPrivateEndPort(Integer.toString(fwRule.getDestinationPortEnd())); response.setProtocol(fwRule.getProtocol()); - response.setPublicPort(Integer.toString(fwRule.getSourcePortStart())); + response.setPublicStartPort(Integer.toString(fwRule.getSourcePortStart())); + response.setPublicEndPort(Integer.toString(fwRule.getSourcePortEnd())); IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId()); response.setPublicIpAddressId(ip.getId()); diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java index a07aaded690..e39afd25beb 100755 --- a/server/src/com/cloud/network/rules/RulesManagerImpl.java +++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java @@ -224,6 +224,11 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { if (rule.getDestinationPortStart() > rule.getDestinationPortEnd() || rule.getSourcePortStart() > rule.getSourcePortEnd()) { throw new InvalidParameterValueException("Start port can't be bigger than end port"); } + + // check that the port ranges are of equal size + if ((rule.getDestinationPortEnd() - rule.getDestinationPortStart()) != (rule.getSourcePortEnd() - rule.getSourcePortStart())) { + throw new InvalidParameterValueException("Source port and destination port ranges should be of equal sizes."); + } Network network = _networkMgr.getNetwork(networkId); assert network != null : "Can't create port forwarding rule as network associated with public ip address is null...how is it possible?";