Add: Dot1Q + L2BD + GBP
[csit.git] / resources / libraries / python / Classify.py
index b2cc3a6..46785dd 100644 (file)
@@ -21,7 +21,7 @@ from ipaddress import ip_address
 from robot.api import logger
 
 from resources.libraries.python.topology import Topology
-from resources.libraries.python.PapiExecutor import PapiExecutor
+from resources.libraries.python.PapiExecutor import PapiSocketExecutor
 
 
 class Classify(object):
@@ -289,7 +289,7 @@ class Classify(object):
         err_msg = "Failed to create a classify table on host {host}".format(
             host=node['host'])
 
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             reply = papi_exec.add(cmd, **args).get_reply(err_msg)
 
         return int(reply["new_table_index"]), int(reply["skip_n_vectors"]),\
@@ -355,7 +355,7 @@ class Classify(object):
         err_msg = "Failed to create a classify session on host {host}".format(
             host=node['host'])
 
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             papi_exec.add(cmd, **args).get_reply(err_msg)
 
     @staticmethod
@@ -379,7 +379,7 @@ class Classify(object):
         err_msg = "Failed to create a classify session on host {host}".format(
             host=node['host'])
 
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             papi_exec.add(cmd, **args).get_reply(err_msg)
 
     @staticmethod
@@ -407,7 +407,7 @@ class Classify(object):
         err_msg = "Failed to set acl list for interface {idx} on host {host}".\
             format(idx=sw_if_index, host=node['host'])
 
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             papi_exec.add(cmd, **args).get_reply(err_msg)
 
     @staticmethod
@@ -425,7 +425,7 @@ class Classify(object):
         """
         cmd = "acl_add_replace"
         args = dict(
-            tag=tag,
+            tag=tag.encode("utf-8"),
             acl_index=4294967295 if acl_idx is None else acl_idx,
             count=len(rules),
             r=rules
@@ -434,7 +434,7 @@ class Classify(object):
         err_msg = "Failed to add/replace acls on host {host}".format(
             host=node['host'])
 
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             papi_exec.add(cmd, **args).get_reply(err_msg)
 
     @staticmethod
@@ -732,7 +732,7 @@ class Classify(object):
         args = dict(
             table_id=int(table_index)
         )
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             reply = papi_exec.add(cmd, **args).get_reply(err_msg)
         return reply
 
@@ -751,7 +751,7 @@ class Classify(object):
         args = dict(
             table_id=int(table_index)
         )
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             details = papi_exec.add(cmd, **args).get_details()
 
         return details
@@ -764,7 +764,7 @@ class Classify(object):
         :param node: VPP node.
         :type node: dict
         """
-        PapiExecutor.dump_and_log(node, ["acl_dump", ])
+        PapiSocketExecutor.dump_and_log(node, ["acl_dump", ])
 
     @staticmethod
     def vpp_log_plugin_acl_interface_assignment(node):
@@ -774,7 +774,7 @@ class Classify(object):
         :param node: VPP node.
         :type node: dict
         """
-        PapiExecutor.dump_and_log(node, ["acl_interface_list_dump", ])
+        PapiSocketExecutor.dump_and_log(node, ["acl_interface_list_dump", ])
 
     @staticmethod
     def set_acl_list_for_interface(node, interface, acl_type, acl_idx=None):
@@ -803,21 +803,24 @@ class Classify(object):
                                              acls=acls)
 
     @staticmethod
-    def add_replace_acl_multi_entries(node, acl_idx=None, rules=None):
+    def add_replace_acl_multi_entries(node, acl_idx=None, rules=None, tag=""):
         """Add a new ACL or replace the existing one. To replace an existing
         ACL, pass the ID of this ACL.
 
         :param node: VPP node to set ACL on.
         :param acl_idx: ID of ACL. (Optional)
         :param rules: Required rules. (Optional)
+        :param tag: ACL tag (Optional).
         :type node: dict
         :type acl_idx: int
         :type rules: str
+        :type tag: str
         """
         reg_ex_src_ip = re.compile(r'(src [0-9a-fA-F.:/\d{1,2}]*)')
         reg_ex_dst_ip = re.compile(r'(dst [0-9a-fA-F.:/\d{1,2}]*)')
         reg_ex_sport = re.compile(r'(sport \d{1,5})')
         reg_ex_dport = re.compile(r'(dport \d{1,5})')
+        reg_ex_proto = re.compile(r'(proto \d{1,5})')
 
         acl_rules = list()
         for rule in rules.split(", "):
@@ -842,18 +845,30 @@ class Classify(object):
                 port = int(groups.group(1).split(' ')[1])
                 acl_rule["srcport_or_icmptype_first"] = port
                 acl_rule["srcport_or_icmptype_last"] = port
+            else:
+                acl_rule["srcport_or_icmptype_first"] = 0
+                acl_rule["srcport_or_icmptype_last"] = 65535
 
             groups = re.search(reg_ex_dport, rule)
             if groups:
                 port = int(groups.group(1).split(' ')[1])
                 acl_rule["dstport_or_icmpcode_first"] = port
                 acl_rule["dstport_or_icmpcode_last"] = port
+            else:
+                acl_rule["dstport_or_icmpcode_first"] = 0
+                acl_rule["dstport_or_icmpcode_last"] = 65535
 
-            acl_rule["proto"] = 0
+            groups = re.search(reg_ex_proto, rule)
+            if groups:
+                proto = int(groups.group(1).split(' ')[1])
+                acl_rule["proto"] = proto
+            else:
+                acl_rule["proto"] = 0
 
             acl_rules.append(acl_rule)
 
-        Classify._acl_add_replace(node, acl_idx=acl_idx, rules=acl_rules)
+        Classify._acl_add_replace(
+            node, acl_idx=acl_idx, rules=acl_rules, tag=tag)
 
     @staticmethod
     def add_macip_acl_multi_entries(node, rules=""):
@@ -902,7 +917,7 @@ class Classify(object):
         :param node: VPP node.
         :type node: dict
         """
-        PapiExecutor.dump_and_log(node, ["macip_acl_dump", ])
+        PapiSocketExecutor.dump_and_log(node, ["macip_acl_dump", ])
 
     @staticmethod
     def add_del_macip_acl_interface(node, interface, action, acl_idx):
@@ -933,7 +948,7 @@ class Classify(object):
             sw_if_index=int(sw_if_index),
             acl_index=int(acl_idx)
         )
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             papi_exec.add(cmd, **args).get_reply(err_msg)
 
     @staticmethod
@@ -946,6 +961,6 @@ class Classify(object):
         cmd = 'macip_acl_interface_get'
         err_msg = "Failed to get 'macip_acl_interface' on host {host}".format(
             host=node['host'])
-        with PapiExecutor(node) as papi_exec:
+        with PapiSocketExecutor(node) as papi_exec:
             reply = papi_exec.add(cmd).get_reply(err_msg)
         logger.info(reply)