Skip to content

Commit d1c1bde

Browse files
committed
code cleanup
1 parent 1ead444 commit d1c1bde

1 file changed

Lines changed: 26 additions & 68 deletions

File tree

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

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public IpPortAndProto() {
4747
}
4848

4949
public IpPortAndProto(String proto, int startPort, int endPort, String[] allowedCidrs) {
50-
super();
50+
this();
5151
this.proto = proto;
5252
this.startPort = startPort;
5353
this.endPort = endPort;
@@ -93,7 +93,7 @@ public SecurityGroupRulesCmd() {
9393

9494
public SecurityGroupRulesCmd(String guestIp, String guestMac, String vmName, Long vmId, String signature, Long seqNum, IpPortAndProto[] ingressRuleSet,
9595
IpPortAndProto[] egressRuleSet) {
96-
super();
96+
this();
9797
this.guestIp = guestIp;
9898
this.vmName = vmName;
9999
this.ingressRuleSet = ingressRuleSet;
@@ -110,19 +110,7 @@ public SecurityGroupRulesCmd(String guestIp, String guestMac, String vmName, Lon
110110

111111
public SecurityGroupRulesCmd(String guestIp, String guestMac, String vmName, Long vmId, String signature, Long seqNum, IpPortAndProto[] ingressRuleSet,
112112
IpPortAndProto[] egressRuleSet, List<String> secIps) {
113-
super();
114-
this.guestIp = guestIp;
115-
this.vmName = vmName;
116-
this.ingressRuleSet = ingressRuleSet;
117-
this.egressRuleSet = egressRuleSet;
118-
this.guestMac = guestMac;
119-
this.signature = signature;
120-
this.seqNum = seqNum;
121-
this.vmId = vmId;
122-
if (signature == null) {
123-
String stringified = stringifyRules();
124-
this.signature = DigestUtils.md5Hex(stringified);
125-
}
113+
this(guestIp, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet);
126114
this.secIps = secIps;
127115
}
128116

@@ -159,27 +147,6 @@ public String getVmName() {
159147
return vmName;
160148
}
161149

162-
public String stringifyRules() {
163-
StringBuilder ruleBuilder = new StringBuilder();
164-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getIngressRuleSet()) {
165-
ruleBuilder.append("I:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
166-
for (String cidr : ipPandP.getAllowedCidrs()) {
167-
ruleBuilder.append(cidr).append(",");
168-
}
169-
ruleBuilder.append("NEXT");
170-
ruleBuilder.append(" ");
171-
}
172-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getEgressRuleSet()) {
173-
ruleBuilder.append("E:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
174-
for (String cidr : ipPandP.getAllowedCidrs()) {
175-
ruleBuilder.append(cidr).append(",");
176-
}
177-
ruleBuilder.append("NEXT");
178-
ruleBuilder.append(" ");
179-
}
180-
return ruleBuilder.toString();
181-
}
182-
183150
//convert cidrs in the form "a.b.c.d/e" to "hexvalue of 32bit ip/e"
184151
private String compressCidr(String cidr) {
185152
String[] toks = cidr.split("/");
@@ -200,52 +167,43 @@ public String getSecIpsString() {
200167
return sb.toString();
201168
}
202169

170+
public String stringifyRules() {
171+
StringBuilder ruleBuilder = new StringBuilder();
172+
stringifyRulesFor(getIngressRuleSet(), INGRESS_RULE, false, ruleBuilder);
173+
stringifyRulesFor(getEgressRuleSet(), EGRESS_RULE, false, ruleBuilder);
174+
return ruleBuilder.toString();
175+
}
176+
203177
public String stringifyCompressedRules() {
204178
StringBuilder ruleBuilder = new StringBuilder();
205-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getIngressRuleSet()) {
206-
ruleBuilder.append("I:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
207-
for (String cidr : ipPandP.getAllowedCidrs()) {
208-
//convert cidrs in the form "a.b.c.d/e" to "hexvalue of 32bit ip/e"
209-
ruleBuilder.append(compressCidr(cidr)).append(",");
210-
}
211-
ruleBuilder.append("NEXT");
212-
ruleBuilder.append(" ");
213-
}
214-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getEgressRuleSet()) {
215-
ruleBuilder.append("E:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
179+
stringifyRulesFor(getEgressRuleSet(), INGRESS_RULE, true, ruleBuilder);
180+
stringifyRulesFor(getEgressRuleSet(), EGRESS_RULE, true, ruleBuilder);
181+
return ruleBuilder.toString();
182+
}
183+
184+
/**
185+
* @param ipPandPs
186+
* @param gression
187+
* @param compress
188+
* @param ruleBuilder
189+
*/
190+
void stringifyRulesFor(SecurityGroupRulesCmd.IpPortAndProto[] ipPandPs, String gression, boolean compress, StringBuilder ruleBuilder) {
191+
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : ipPandPs) {
192+
ruleBuilder.append(gression).append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
216193
for (String cidr : ipPandP.getAllowedCidrs()) {
217-
//convert cidrs in the form "a.b.c.d/e" to "hexvalue of 32bit ip/e"
218-
ruleBuilder.append(compressCidr(cidr)).append(",");
194+
ruleBuilder.append(compress?compressCidr(cidr):cidr).append(",");
219195
}
220196
ruleBuilder.append("NEXT");
221197
ruleBuilder.append(" ");
222198
}
223-
return ruleBuilder.toString();
224199
}
225200

226201
/*
227202
* Compress the security group rules using zlib compression to allow the call to the hypervisor
228203
* to scale beyond 8k cidrs.
229204
*/
230205
public String compressStringifiedRules() {
231-
StringBuilder ruleBuilder = new StringBuilder();
232-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getIngressRuleSet()) {
233-
ruleBuilder.append("I:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
234-
for (String cidr : ipPandP.getAllowedCidrs()) {
235-
ruleBuilder.append(cidr).append(",");
236-
}
237-
ruleBuilder.append("NEXT");
238-
ruleBuilder.append(" ");
239-
}
240-
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getEgressRuleSet()) {
241-
ruleBuilder.append("E:").append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":").append(ipPandP.getEndPort()).append(":");
242-
for (String cidr : ipPandP.getAllowedCidrs()) {
243-
ruleBuilder.append(cidr).append(",");
244-
}
245-
ruleBuilder.append("NEXT");
246-
ruleBuilder.append(" ");
247-
}
248-
String stringified = ruleBuilder.toString();
206+
String stringified = stringifyRules();
249207
ByteArrayOutputStream out = new ByteArrayOutputStream();
250208
try {
251209
//Note : not using GZipOutputStream since that is for files

0 commit comments

Comments
 (0)