1414// KIND, either express or implied. See the License for the
1515// specific language governing permissions and limitations
1616// under the License.
17+
1718package com .cloud .agent .api .routing ;
1819
20+ import java .util .HashSet ;
1921import java .util .List ;
22+ import java .util .Set ;
2023
2124import com .cloud .agent .api .to .NetworkACLTO ;
2225import com .cloud .agent .api .to .NicTO ;
2326
27+ /**
28+ * @author Alena Prokharchyk
29+ */
2430public class SetNetworkACLCommand extends NetworkElementCommand {
2531 NetworkACLTO [] rules ;
2632 NicTO nic ;
@@ -36,6 +42,57 @@ public SetNetworkACLCommand(List<NetworkACLTO> rules, NicTO nic) {
3642 public NetworkACLTO [] getRules () {
3743 return rules ;
3844 }
45+ public String [][] generateFwRules () {
46+ String [][] result = new String [2 ][];
47+ Set <String > toAdd = new HashSet <String >();
48+
49+
50+ for (NetworkACLTO aclTO : rules ) {
51+ /* example : Ingress:tcp:80:80:0.0.0.0/0:,Egress:tcp:220:220:0.0.0.0/0:,
52+ * each entry format Ingress/Egress:protocol:start port: end port:scidrs:
53+ * reverted entry format Ingress/Egress:reverted:0:0:0:
54+ */
55+ if (aclTO .revoked () == true )
56+ {
57+ StringBuilder sb = new StringBuilder ();
58+ /* This entry is added just to make sure atleast there will one entry in the list to get the ipaddress */
59+ sb .append (aclTO .getTrafficType ().toString ()).append (":reverted:0:0:0:" );
60+ String aclRuleEntry = sb .toString ();
61+ toAdd .add (aclRuleEntry );
62+ continue ;
63+ }
64+
65+ List <String > cidr ;
66+ StringBuilder sb = new StringBuilder ();
67+ sb .append (aclTO .getTrafficType ().toString ()).append (":" ).append (aclTO .getProtocol ()).append (":" );
68+ if ("icmp" .compareTo (aclTO .getProtocol ()) == 0 )
69+ {
70+ sb .append (aclTO .getIcmpType ()).append (":" ).append (aclTO .getIcmpCode ()).append (":" );
71+ } else {
72+ sb .append (aclTO .getStringPortRange ()).append (":" );
73+ }
74+ cidr = aclTO .getSourceCidrList ();
75+ if (cidr == null || cidr .isEmpty ())
76+ {
77+ sb .append ("0.0.0.0/0" );
78+ }else {
79+ Boolean firstEntry = true ;
80+ for (String tag : cidr ) {
81+ if (!firstEntry ) sb .append ("-" );
82+ sb .append (tag );
83+ firstEntry = false ;
84+ }
85+ }
86+ sb .append (":" );
87+ String aclRuleEntry = sb .toString ();
88+
89+ toAdd .add (aclRuleEntry );
90+
91+ }
92+ result [0 ] = toAdd .toArray (new String [toAdd .size ()]);
93+
94+ return result ;
95+ }
3996
4097 public NicTO getNic () {
4198 return nic ;
0 commit comments