3333import com .cloud .utils .net .NetUtils ;
3434
3535public 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 ();
0 commit comments