Skip to content

Commit b9b5967

Browse files
committed
SecurityGroupRulesCmd code cleanup review comments handled
1 parent d39182f commit b9b5967

2 files changed

Lines changed: 42 additions & 48 deletions

File tree

core/src/com/cloud/agent/api/SecurityGroupRulesCmd.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.cloud.utils.net.NetUtils;
3434

3535
public class SecurityGroupRulesCmd extends Command {
36+
private static final String CIDR_LENGTH_SEPARATOR = "/";
3637
private static final char RULE_TARGET_SEPARATOR = ',';
3738
private static final char RULE_COMMAND_SEPARATOR = ':';
3839
protected static final String EGRESS_RULE = "E:";
@@ -155,11 +156,10 @@ public String getVmName() {
155156
return vmName;
156157
}
157158

158-
//convert cidrs in the form "a.b.c.d/e" to "hexvalue of 32bit ip/e"
159-
private String compressCidr(final String cidr) {
160-
final String[] toks = cidr.split("/");
159+
private String compressCidrToHexRepresentation(final String cidr) {
160+
final String[] toks = cidr.split(CIDR_LENGTH_SEPARATOR);
161161
final long ipnum = NetUtils.ip2Long(toks[0]);
162-
return Long.toHexString(ipnum) + "/" + toks[1];
162+
return Long.toHexString(ipnum) + CIDR_LENGTH_SEPARATOR + toks[1];
163163
}
164164

165165
public String getSecIpsString() {
@@ -189,42 +189,41 @@ public String stringifyCompressedRules() {
189189
return ruleBuilder.toString();
190190
}
191191

192-
/**
193-
* @param ipPandPs
194-
* @param gression
195-
* @param compress
196-
* @param ruleBuilder
197-
*/
198-
private void stringifyRulesFor(
199-
final List<IpPortAndProto> ipPandPs,
200-
final String gression,
201-
final boolean compress,
202-
final StringBuilder ruleBuilder) {
203-
for (final IpPortAndProto ipPandP : ipPandPs) {
204-
ruleBuilder.append(gression).append(ipPandP.getProto()).append(RULE_COMMAND_SEPARATOR).append(ipPandP.getStartPort()).append(RULE_COMMAND_SEPARATOR).append(ipPandP.getEndPort()).append(RULE_COMMAND_SEPARATOR);
192+
private void stringifyRulesFor(final List<IpPortAndProto> ipPortAndProtocols, final String inOrEgress, final boolean compressed, final StringBuilder ruleBuilder) {
193+
for (final IpPortAndProto ipPandP : ipPortAndProtocols) {
194+
ruleBuilder.append(inOrEgress).append(ipPandP.getProto()).append(RULE_COMMAND_SEPARATOR).append(ipPandP.getStartPort()).append(RULE_COMMAND_SEPARATOR)
195+
.append(ipPandP.getEndPort()).append(RULE_COMMAND_SEPARATOR);
205196
for (final String cidr : ipPandP.getAllowedCidrs()) {
206-
ruleBuilder.append(compress?compressCidr(cidr):cidr).append(RULE_TARGET_SEPARATOR);
197+
ruleBuilder.append(represent(cidr, compressed)).append(RULE_TARGET_SEPARATOR);
207198
}
208199
ruleBuilder.append("NEXT ");
209200
}
210201
}
211202

212-
/*
203+
private String represent(final String cidr, final boolean compressed) {
204+
if (compressed) {
205+
return compressCidrToHexRepresentation(cidr);
206+
} else {
207+
return cidr;
208+
}
209+
}
210+
211+
/**
213212
* Compress the security group rules using zlib compression to allow the call to the hypervisor
214213
* to scale beyond 8k cidrs.
214+
* Note : not using {@see GZipOutputStream} since that is for files, using {@see DeflaterOutputStream} instead.
215+
* {@see GZipOutputStream} gives a different header, although the compression is the same
215216
*/
216217
public String compressStringifiedRules() {
217218
final String stringified = stringifyRules();
218219
final ByteArrayOutputStream out = new ByteArrayOutputStream();
219220
String encodedResult = null;
220221
try {
221-
//Note : not using GZipOutputStream since that is for files
222-
//GZipOutputStream gives a different header, although the compression is the same
223222
final DeflaterOutputStream dzip = new DeflaterOutputStream(out);
224223
dzip.write(stringified.getBytes());
225224
dzip.close();
226225
encodedResult = Base64.encodeBase64String(out.toByteArray());
227-
} catch (IOException e) {
226+
} catch (final IOException e) {
228227
LOGGER.warn("Exception while compressing security group rules");
229228
}
230229
return encodedResult;
@@ -246,8 +245,11 @@ public Long getVmId() {
246245
return vmId;
247246
}
248247

248+
/**
249+
* used for logging
250+
* @return the number of Cidrs in the in and egress rule sets for this security group rules command.
251+
*/
249252
public int getTotalNumCidrs() {
250-
//useful for logging
251253
int count = 0;
252254
for (final IpPortAndProto i : ingressRuleSet) {
253255
count += i.allowedCidrs.size();

core/test/com/cloud/agent/api/SecurityGroupRulesCmdTest.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Vector;
2626

2727
import org.junit.Before;
28-
import org.junit.BeforeClass;
2928
import org.junit.Test;
3029
import org.junit.runner.RunWith;
3130
import org.mockito.runners.MockitoJUnitRunner;
@@ -40,31 +39,24 @@
4039
public class SecurityGroupRulesCmdTest {
4140
private SecurityGroupRulesCmd securityGroupRulesCmd;
4241

43-
/**
44-
* @throws java.lang.Exception
45-
*/
46-
@BeforeClass
47-
public static void setUpBeforeClass() throws Exception {
48-
}
49-
5042
/**
5143
* @throws java.lang.Exception
5244
*/
5345
@Before
5446
public void setUp() throws Exception {
55-
String guestIp = "10.10.10.10";
56-
String guestMac = "aa:aa:aa:aa:aa:aa";
57-
String vmName = "vm";
58-
Long vmId = 1L;
59-
String signature = "sig";
60-
Long seqNum = 0L;
61-
String proto = "abc";
62-
int startPort = 1;
63-
int endPort = 2;
64-
String[] allowedCidrs = new String[] {"1.2.3.4/5","6.7.8.9/0"};
65-
IpPortAndProto[] ingressRuleSet = new IpPortAndProto[]{new IpPortAndProto(proto, startPort, endPort, allowedCidrs)};
66-
IpPortAndProto[] egressRuleSet = new IpPortAndProto[]{new IpPortAndProto(proto, startPort, endPort, allowedCidrs)};
67-
List<String> secIps = new Vector<String>();
47+
final String guestIp = "10.10.10.10";
48+
final String guestMac = "aa:aa:aa:aa:aa:aa";
49+
final String vmName = "vm";
50+
final Long vmId = 1L;
51+
final String signature = "sig";
52+
final Long seqNum = 0L;
53+
final String proto = "abc";
54+
final int startPort = 1;
55+
final int endPort = 2;
56+
final String[] allowedCidrs = new String[] {"1.2.3.4/5","6.7.8.9/0"};
57+
final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[]{new IpPortAndProto(proto, startPort, endPort, allowedCidrs)};
58+
final IpPortAndProto[] egressRuleSet = new IpPortAndProto[]{new IpPortAndProto(proto, startPort, endPort, allowedCidrs)};
59+
final List<String> secIps = new Vector<String>();
6860
securityGroupRulesCmd = new SecurityGroupRulesCmd(guestIp, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
6961
}
7062

@@ -73,7 +65,7 @@ public void setUp() throws Exception {
7365
*/
7466
@Test
7567
public void testStringifyRules() throws Exception {
76-
String a = securityGroupRulesCmd.stringifyRules();
68+
final String a = securityGroupRulesCmd.stringifyRules();
7769
// do verification on a
7870
assertTrue(a.contains(SecurityGroupRulesCmd.EGRESS_RULE));
7971
}
@@ -83,7 +75,7 @@ public void testStringifyRules() throws Exception {
8375
*/
8476
@Test
8577
public void testStringifyCompressedRules() throws Exception {
86-
String a = securityGroupRulesCmd.stringifyCompressedRules();
78+
final String a = securityGroupRulesCmd.stringifyCompressedRules();
8779
// do verification on a
8880
assertTrue(a.contains(SecurityGroupRulesCmd.EGRESS_RULE));
8981
}
@@ -93,8 +85,8 @@ public void testStringifyCompressedRules() throws Exception {
9385
*/
9486
@Test
9587
public void testCompressStringifiedRules() throws Exception {
96-
String compressed = "eJzztEpMSrYytDKyMtQz0jPWM9E31THTM9ez0LPUN9Dxc40IUXAlrAQAPdoP3Q==";
97-
String a = securityGroupRulesCmd.compressStringifiedRules();
88+
final String compressed = "eJzztEpMSrYytDKyMtQz0jPWM9E31THTM9ez0LPUN9Dxc40IUXAlrAQAPdoP3Q==";
89+
final String a = securityGroupRulesCmd.compressStringifiedRules();
9890
assertTrue(compressed.equals(a));
9991
}
10092

0 commit comments

Comments
 (0)