Skip to content

Commit 252038f

Browse files
Move the protocol number helpers to NetUtils
createNetworkACL and updateNetworkACLItem each turned protocol number 1 into icmp with their own copy of the same code; they now share NetUtils.icmpProtocolNumberAsName. The TCP and UDP protocol numbers move there too, next to ICMP_PROTO_NUMBER, in place of the private copies in NetworkACLServiceImpl and SetNetworkACLCommand. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent e32fbb8 commit 252038f

6 files changed

Lines changed: 39 additions & 28 deletions

File tree

‎api/src/main/java/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.cloudstack.api.response.NetworkACLResponse;
3232
import org.apache.cloudstack.api.response.NetworkResponse;
3333
import org.apache.cloudstack.context.CallContext;
34-
import org.apache.commons.lang3.StringUtils;
3534

3635
import com.cloud.event.EventTypes;
3736
import com.cloud.exception.InvalidParameterValueException;
@@ -102,15 +101,7 @@ public boolean isDisplay() {
102101
}
103102

104103
public String getProtocol() {
105-
String p = protocol.trim();
106-
// Deal with ICMP(protocol number 1) specially because it need to be paired with icmp type and code
107-
if (StringUtils.isNumeric(p)) {
108-
int protoNumber = Integer.parseInt(p);
109-
if (protoNumber == 1) {
110-
p = "icmp";
111-
}
112-
}
113-
return p;
104+
return NetUtils.icmpProtocolNumberAsName(protocol);
114105
}
115106

116107
public void setProtocol(String protocol) {

‎api/src/main/java/org/apache/cloudstack/api/command/user/network/UpdateNetworkACLItemCmd.java‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.cloudstack.api.Parameter;
2727
import org.apache.cloudstack.api.response.NetworkACLItemResponse;
2828
import org.apache.cloudstack.context.CallContext;
29-
import org.apache.commons.lang3.StringUtils;
3029

3130
import com.cloud.event.EventTypes;
3231
import com.cloud.exception.ResourceUnavailableException;
@@ -101,15 +100,7 @@ public Long getId() {
101100
}
102101

103102
public String getProtocol() {
104-
if (protocol == null) {
105-
return null;
106-
}
107-
String p = protocol.trim();
108-
// As on create, protocol number 1 is ICMP so that it keeps its icmp type and code
109-
if (StringUtils.isNumeric(p) && Integer.parseInt(p) == NetUtils.ICMP_PROTO_NUMBER) {
110-
p = NetUtils.ICMP_PROTO;
111-
}
112-
return p;
103+
return NetUtils.icmpProtocolNumberAsName(protocol);
113104
}
114105

115106
public List<String> getSourceCidrList() {

‎core/src/main/java/com/cloud/agent/api/routing/SetNetworkACLCommand.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
public class SetNetworkACLCommand extends NetworkElementCommand {
3535
public static final String RULE_DETAIL_SEPARATOR = ";";
36-
private static final int TCP_PROTO_NUMBER = 6;
37-
private static final int UDP_PROTO_NUMBER = 17;
3836

3937
NetworkACLTO[] rules;
4038
NicTO nic;
@@ -128,10 +126,10 @@ protected static String normalizeProtocol(String protocol, boolean hasPorts) {
128126
if (number == NetUtils.ICMP_PROTO_NUMBER) {
129127
return NetUtils.ICMP_PROTO;
130128
}
131-
if (hasPorts && number == TCP_PROTO_NUMBER) {
129+
if (hasPorts && number == NetUtils.TCP_PROTO_NUMBER) {
132130
return NetUtils.TCP_PROTO;
133131
}
134-
if (hasPorts && number == UDP_PROTO_NUMBER) {
132+
if (hasPorts && number == NetUtils.UDP_PROTO_NUMBER) {
135133
return NetUtils.UDP_PROTO;
136134
}
137135
return String.valueOf(number);

‎server/src/main/java/com/cloud/network/vpc/NetworkACLServiceImpl.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ public class NetworkACLServiceImpl extends ManagerBase implements NetworkACLServ
117117
private VpcManager vpcManager;
118118

119119
private String supportedProtocolsForAclRules = "tcp,udp,icmp,all";
120-
private static final int TCP_PROTO_NUMBER = 6;
121-
private static final int UDP_PROTO_NUMBER = 17;
122120

123121
@Override
124122
public NetworkACL createNetworkACL(final String name, final String description, final long vpcId, final Boolean forDisplay) {
@@ -703,9 +701,9 @@ protected void validatePortsAreUsableWithProtocol(String protocol, Integer sourc
703701
return;
704702
}
705703
int protoNumber = NumberUtils.toInt(protocol, -1);
706-
if (protoNumber != TCP_PROTO_NUMBER && protoNumber != UDP_PROTO_NUMBER) {
704+
if (protoNumber != NetUtils.TCP_PROTO_NUMBER && protoNumber != NetUtils.UDP_PROTO_NUMBER) {
707705
throw new InvalidParameterValueException(String.format("Start and end port can only be given for TCP or UDP (protocol number %d or %d), not for protocol number [%s]",
708-
TCP_PROTO_NUMBER, UDP_PROTO_NUMBER, protocol));
706+
NetUtils.TCP_PROTO_NUMBER, NetUtils.UDP_PROTO_NUMBER, protocol));
709707
}
710708
}
711709

‎utils/src/main/java/com/cloud/utils/net/NetUtils.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.commons.collections.CollectionUtils;
4949
import org.apache.commons.lang.SystemUtils;
5050
import org.apache.commons.lang3.StringUtils;
51+
import org.apache.commons.lang3.math.NumberUtils;
5152
import org.apache.commons.net.util.SubnetUtils;
5253
import org.apache.commons.validator.routines.InetAddressValidator;
5354
import org.apache.commons.validator.routines.RegexValidator;
@@ -82,6 +83,8 @@ public class NetUtils {
8283
public final static String ICMP_PROTO = "icmp";
8384
public static final String ICMP6_PROTO = "icmp6";
8485
public final static int ICMP_PROTO_NUMBER = 1;
86+
public final static int TCP_PROTO_NUMBER = 6;
87+
public final static int UDP_PROTO_NUMBER = 17;
8588
public final static String ALL_PROTO = "all";
8689
public final static String HTTP_PROTO = "http";
8790
public final static String HTTPS_PROTO = "https";
@@ -1013,6 +1016,21 @@ public static boolean isValidSecurityGroupProto(final String p) {
10131016
return proto.equals(TCP_PROTO) || proto.equals(UDP_PROTO) || proto.equals(ICMP_PROTO) || proto.equals(ALL_PROTO);
10141017
}
10151018

1019+
/**
1020+
* Gives a rule's protocol trimmed, with protocol number 1 as icmp, so that the rule keeps its ICMP
1021+
* type and code.
1022+
*/
1023+
public static String icmpProtocolNumberAsName(final String protocol) {
1024+
if (protocol == null) {
1025+
return null;
1026+
}
1027+
final String p = protocol.trim();
1028+
if (StringUtils.isNumeric(p) && NumberUtils.toInt(p, -1) == ICMP_PROTO_NUMBER) {
1029+
return ICMP_PROTO;
1030+
}
1031+
return p;
1032+
}
1033+
10161034
public static boolean isValidAlgorithm(final String p) {
10171035
final String algo = p.toLowerCase();
10181036
return algo.equals("roundrobin") || algo.equals("leastconn") || algo.equals("source");

‎utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,19 @@ public void testVpnIpRange() {
955955
String cidr = NetUtils.transformCidr(startIp + "/" + cidrSize);
956956
Assert.assertEquals("10.1.2.0/28", cidr);
957957
}
958+
959+
@Test
960+
public void icmpProtocolNumberAsNameTurnsProtocolNumberOneIntoIcmp() {
961+
assertEquals(NetUtils.ICMP_PROTO, NetUtils.icmpProtocolNumberAsName("1"));
962+
assertEquals(NetUtils.ICMP_PROTO, NetUtils.icmpProtocolNumberAsName(" 1 "));
963+
assertEquals(NetUtils.ICMP_PROTO, NetUtils.icmpProtocolNumberAsName("01"));
964+
}
965+
966+
@Test
967+
public void icmpProtocolNumberAsNameLeavesOtherProtocolsTrimmed() {
968+
assertEquals("tcp", NetUtils.icmpProtocolNumberAsName(" tcp "));
969+
assertEquals("47", NetUtils.icmpProtocolNumberAsName("47"));
970+
assertEquals("99999999999", NetUtils.icmpProtocolNumberAsName("99999999999"));
971+
Assert.assertNull(NetUtils.icmpProtocolNumberAsName(null));
972+
}
958973
}

0 commit comments

Comments
 (0)