packetforge: add option to show spec and mask only
[vpp.git] / extras / packetforge / flow_create.py
index fbbdec5..3d4e4b6 100644 (file)
@@ -29,17 +29,44 @@ VPP_JSON_DIR_PLUGIN = (
 API_FILE_SUFFIX = "*.api.json"
 
 
+def load_json_api_files(suffix=API_FILE_SUFFIX):
+    jsonfiles = []
+    json_dir = VPP_JSON_DIR
+    for root, dirnames, filenames in os.walk(json_dir):
+        for filename in fnmatch.filter(filenames, suffix):
+            jsonfiles.append(os.path.join(json_dir, filename))
+    json_dir = VPP_JSON_DIR_PLUGIN
+    for root, dirnames, filenames in os.walk(json_dir):
+        for filename in fnmatch.filter(filenames, suffix):
+            jsonfiles.append(os.path.join(json_dir, filename))
+    if not jsonfiles:
+        raise RuntimeError("Error: no json api files found")
+    else:
+        print("load json api file done")
+    return jsonfiles
+
+
+def connect_vpp(jsonfiles):
+    vpp = VPPApiClient(apifiles=jsonfiles)
+    r = vpp.connect("CLIENT_ID")
+    print("VPP api opened with code: %s" % r)
+    return vpp
+
+
 def Main(argv):
     file_flag = False
     operation = None
+    actions = ""
+    iface = ""
     try:
         opts, args = getopt.getopt(
             argv,
             "hf:p:a:i:I:",
             [
-                "help=",
+                "help",
                 "add",
                 "del",
+                "show",
                 "file=",
                 "pattern=",
                 "actions=",
@@ -49,21 +76,22 @@ def Main(argv):
         )
     except getopt.GetoptError:
         print(
-            "flow_create.py --add|del -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
+            "flow_create.py --add|del|show -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
         )
         sys.exit()
     for opt, arg in opts:
         if opt == "-h":
             print(
-                "flow_create.py --add|del -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
+                "flow_create.py --add|del|show -f <file> -p <pattern> -a <actions> -i <interface> -I <flow-index>"
             )
             sys.exit()
         elif opt == "--add":
             operation = "add"
         elif opt == "--del":
             operation = "del"
+        elif opt == "--show":
+            operation = "show"
         elif opt in ("-f", "--file"):
-            actions = ""
             json_file = arg
             file_flag = True
         elif opt in ("-p", "--pattern") and not file_flag:
@@ -79,50 +107,37 @@ def Main(argv):
         print("Error: Please choose the operation: add or del")
         sys.exit()
 
-    if operation == "add":
+    if operation == "show":
         if not file_flag:
-            result = packetforge.Forge(pattern, actions, False)
+            result = packetforge.Forge(pattern, actions, False, True)
         else:
-            result = packetforge.Forge(json_file, actions, True)
-        return result, int(iface), operation, None
-    elif operation == "del":
-        return None, int(iface), operation, int(flow_index)
-
+            result = packetforge.Forge(json_file, actions, True, True)
+        return result, None, operation, None, None
 
-def load_json_api_files(suffix=API_FILE_SUFFIX):
-    jsonfiles = []
-    json_dir = VPP_JSON_DIR
-    for root, dirnames, filenames in os.walk(json_dir):
-        for filename in fnmatch.filter(filenames, suffix):
-            jsonfiles.append(os.path.join(json_dir, filename))
-    json_dir = VPP_JSON_DIR_PLUGIN
-    for root, dirnames, filenames in os.walk(json_dir):
-        for filename in fnmatch.filter(filenames, suffix):
-            jsonfiles.append(os.path.join(json_dir, filename))
-    if not jsonfiles:
-        raise RuntimeError("Error: no json api files found")
-    else:
-        print("load json api file done")
-    return jsonfiles
+    # Python API need json definitions to interpret messages
+    vpp = connect_vpp(load_json_api_files())
 
+    # set inteface states
+    vpp.api.sw_interface_set_flags(sw_if_index=int(iface), flags=1)
 
-def connect_vpp(jsonfiles):
-    vpp = VPPApiClient(apifiles=jsonfiles)
-    r = vpp.connect("CLIENT_ID")
-    print("VPP api opened with code: %s" % r)
-    return vpp
+    if operation == "add":
+        if not file_flag:
+            result = packetforge.Forge(pattern, actions, False, False)
+        else:
+            result = packetforge.Forge(json_file, actions, True, False)
+        return result, int(iface), operation, None, vpp
+    elif operation == "del":
+        return None, int(iface), operation, int(flow_index), vpp
 
 
 if __name__ == "__main__":
-    # Python API need json definitions to interpret messages
-    vpp = connect_vpp(load_json_api_files())
-    print(vpp.api.show_version())
-
     # Parse the arguments
-    my_flow, iface, operation, del_flow_index = Main(sys.argv[1:])
+    my_flow, iface, operation, del_flow_index, vpp = Main(sys.argv[1:])
 
-    # set inteface states
-    vpp.api.sw_interface_set_flags(sw_if_index=iface, flags=1)
+    # if operation is show, just show spec and mask, then exit
+    if operation == "show":
+        print(my_flow)
+        sys.exit()
 
     if operation == "add":
         # add flow
@@ -155,3 +170,4 @@ if __name__ == "__main__":
 # command example:
 # python flow_create.py --add -p "mac()/ipv4(src=1.1.1.1,dst=2.2.2.2)/udp()" -a "redirect-to-queue 3" -i 1
 # python flow_create.py --del -i 1 -I 0
+# python flow_create.py --show -p "mac()/ipv4(src=1.1.1.1,dst=2.2.2.2)/udp()"