diff mbox series

[net-next,v2,1/2] tools/net/ynl: configuration through json

Message ID 20230727120353.3020678-2-mtahhan@redhat.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series tools/net/ynl: enable json configuration | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 134 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Maryam Tahhan July 27, 2023, 12:03 p.m. UTC
Enable YNL configuration through a json file alongside the
current commandline arguments. This will allow one to cycle
through multiple specs and commands at once.

Signed-off-by: Maryam Tahhan <mtahhan@redhat.com>
---
 tools/net/ynl/cli.py | 108 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 87 insertions(+), 21 deletions(-)

Comments

Jakub Kicinski July 28, 2023, 12:39 a.m. UTC | #1
On Thu, 27 Jul 2023 08:03:30 -0400 Maryam Tahhan wrote:
>  tools/net/ynl/cli.py | 108 ++++++++++++++++++++++++++++++++++---------

I think we should keep cli.py nice and simple, it'd be better to
create a separate script which suits you use case.
diff mbox series

Patch

diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py
index ffaa8038aa8c..1749851f8460 100755
--- a/tools/net/ynl/cli.py
+++ b/tools/net/ynl/cli.py
@@ -5,13 +5,54 @@  import argparse
 import json
 import pprint
 import time
+import os
 
 from lib import YnlFamily
 
+class ynlConfig():
+    def __init__(self):
+        self.no_schema = True
+        self.schema = None
+        self.spec = None
+        self.json_text = None
+        self.ntf = None
+        self.sleep = None
+        self.do = None
+        self.dump = None
+    def run(self):
+        ynl_cfg(self.no_schema, self.spec, self.schema, self.json_text, self.ntf, self.sleep, self.do, self.dump)
+
+def ynl_cfg(no_schema, spec, schema, json_text, ntf, sleep, do, dump):
+
+        if no_schema:
+            schema = ''
+
+        attrs = {}
+        if json_text:
+            attrs = json.loads(json_text)
+
+        ynl = YnlFamily(spec, schema)
+
+        if ntf:
+            ynl.ntf_subscribe(ntf)
+
+        if sleep:
+            time.sleep(sleep)
+
+        if do:
+            reply = ynl.do(do, attrs)
+            pprint.PrettyPrinter().pprint(reply)
+        if dump:
+            reply = ynl.dump(dump, attrs)
+            pprint.PrettyPrinter().pprint(reply)
+
+        if ntf:
+            ynl.check_ntf()
+            pprint.PrettyPrinter().pprint(ynl.async_msg_queue)
 
 def main():
     parser = argparse.ArgumentParser(description='YNL CLI sample')
-    parser.add_argument('--spec', dest='spec', type=str, required=True)
+    parser.add_argument('--spec', dest='spec', type=str)
     parser.add_argument('--schema', dest='schema', type=str)
     parser.add_argument('--no-schema', action='store_true')
     parser.add_argument('--json', dest='json_text', type=str)
@@ -19,34 +60,59 @@  def main():
     parser.add_argument('--dump', dest='dump', type=str)
     parser.add_argument('--sleep', dest='sleep', type=int)
     parser.add_argument('--subscribe', dest='ntf', type=str)
+    parser.add_argument('--config', dest='config', type=str)
     args = parser.parse_args()
 
-    if args.no_schema:
-        args.schema = ''
-
-    attrs = {}
-    if args.json_text:
-        attrs = json.loads(args.json_text)
+    if args.config:
+        directory = ""
+        yamls = {}
 
-    ynl = YnlFamily(args.spec, args.schema)
+        if not os.path.exists(args.config):
+             print("Error: ", args.config, " doesn't exist")
+             exit(-1)
 
-    if args.ntf:
-        ynl.ntf_subscribe(args.ntf)
+        f = open(args.config)
+        data = json.load(f)
 
-    if args.sleep:
-        time.sleep(args.sleep)
+        for k in data:
+            if k == 'yaml-specs-path':
+                directory = data[k]
 
-    if args.do:
-        reply = ynl.do(args.do, attrs)
-        pprint.PrettyPrinter().pprint(reply)
-    if args.dump:
-        reply = ynl.dump(args.dump, attrs)
-        pprint.PrettyPrinter().pprint(reply)
+                # Scan the dir and get all the yaml files.
+                for filename in os.scandir(directory):
+                    if filename.is_file():
+                        if filename.name.endswith('.yaml'):
+                            yamls[filename.name] = filename.path
 
-    if args.ntf:
-        ynl.check_ntf()
-        pprint.PrettyPrinter().pprint(ynl.async_msg_queue)
+            elif k == 'spec-args':
+               for v in data[k]:
+                    print("############### ",v," ###############\n")
+                    cfg = ynlConfig()
+                    # Check for yaml from the specs we found earlier
+                    if v in yamls:
+                        # FOUND
+                        cfg.spec = yamls[v]
+                        if 'no-schema' in data[k][v]:
+                            cfg.no_schema = data[k][v]['no-schema']
+                        if 'schema' in data[k][v]:
+                            cfg.schema = data[k][v]['schema']
+                            cfg.no_schema = False
+                        if 'do' in data[k][v]:
+                            cfg.do = data[k][v]['do']
+                        if 'dump' in data[k][v]:
+                            cfg.dump = data[k][v]['dump']
+                        if 'subscribe' in data[k][v]:
+                            cfg.ntf = data[k][v]['subscribe']
+                        if 'sleep' in data[k][v]:
+                            cfg.sleep = data[k][v]['sleep']
+                        if 'json-params' in data[k][v]:
+                            cfg.json_text = json.dumps(data[k][v]['json-params'])
 
+                        cfg.run()
+                    else:
+                        print("Error: ", v, " doesn't have a valid yaml file in ", directory, "\n")
+    else:
+        ynl_cfg(args.no_schema, args.spec, args.schema, args.json_text, args.ntf, args.sleep, args.do, args.dump)
 
 if __name__ == "__main__":
     main()