[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230727120353.3020678-3-mtahhan@redhat.com>
Date: Thu, 27 Jul 2023 08:03:31 -0400
From: Maryam Tahhan <mtahhan@...hat.com>
To: netdev@...r.kernel.org
Cc: kuba@...nel.org,
davem@...emloft.net,
edumazet@...gle.com,
pabeni@...hat.com,
Maryam Tahhan <mtahhan@...hat.com>,
Keith Wiles <keith.wiles@...el.com>
Subject: [PATCH net-next v2 2/2] tools/net/ynl: validate config against schema
Validate the provided configuration file against
the ynl-config.schema.
Signed-off-by: Maryam Tahhan <mtahhan@...hat.com>
Signed-off-by: Keith Wiles <keith.wiles@...el.com>
Signed-off-by: Maryam Tahhan <mtahhan@...hat.com>
---
tools/net/ynl/cli.py | 43 ++++++++++++++++----
tools/net/ynl/ynl-config.schema | 72 +++++++++++++++++++++++++++++++++
2 files changed, 108 insertions(+), 7 deletions(-)
create mode 100644 tools/net/ynl/ynl-config.schema
diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py
index 1749851f8460..3071ef9e3117 100755
--- a/tools/net/ynl/cli.py
+++ b/tools/net/ynl/cli.py
@@ -9,6 +9,12 @@ import os
from lib import YnlFamily
+try:
+ import jsonschema
+except ModuleNotFoundError as e:
+ print('Error: {}. Try `pip install jsonschema`'.format(e))
+ raise SystemExit(1)
+
class ynlConfig():
def __init__(self):
self.no_schema = True
@@ -66,13 +72,36 @@ def main():
if args.config:
directory = ""
yamls = {}
-
- if not os.path.exists(args.config):
- print("Error: ", args.config, " doesn't exist")
- exit(-1)
-
- f = open(args.config)
- data = json.load(f)
+ configSchema = os.path.dirname(__file__) + "/ynl-config.schema"
+
+ # Load ynl-config json schema
+ try:
+ with open(configSchema, 'r') as f:
+ s = json.load(f)
+ except FileNotFoundError as e:
+ print('Error:', e)
+ raise SystemExit(1)
+ except json.decoder.JSONDecodeError as e:
+ print('Error: {}:'.format(args.schema), e)
+ raise SystemExit(1)
+
+ # Load config file
+ try:
+ with open(args.config, 'r') as f:
+ data = json.load(f)
+ except FileNotFoundError as e:
+ print('Error:', e)
+ raise SystemExit(1)
+ except json.decoder.JSONDecodeError as e:
+ print('Error: {}:'.format(args.schema), e)
+ raise SystemExit(1)
+
+ # Validate json config against the ynl-config schema
+ try:
+ jsonschema.validate(instance=data, schema=s)
+ except jsonschema.exceptions.ValidationError as e:
+ print('Error:', e)
+ raise SystemExit(1)
for k in data:
if k == 'yaml-specs-path':
diff --git a/tools/net/ynl/ynl-config.schema b/tools/net/ynl/ynl-config.schema
new file mode 100644
index 000000000000..c127e2acbabb
--- /dev/null
+++ b/tools/net/ynl/ynl-config.schema
@@ -0,0 +1,72 @@
+{
+ "$schema": "https://json-schema.org/draft-07/schema",
+ "description": "YNL specs configuration file",
+ "type": "object",
+
+ "properties": {
+ "yaml-specs-path": {
+ "description": "Path to Yaml specs",
+ "type": "string"
+ },
+ "spec-args": {
+ "description": "Individual spec args",
+ "type": "object",
+ "patternProperties": {
+ "^.*(\\.yaml)$": {
+ "description": "Specific yaml spec arguments",
+ "type": "object",
+ "properties": {
+ "schema": {
+ "description": "The schema to use",
+ "type": "string"
+ },
+ "no-schema": {
+ "description": "No schema",
+ "type": "boolean",
+ "default": true
+ },
+ "do": {
+ "description": "The do function to use",
+ "type": "string"
+ },
+ "dump": {
+ "description": "The dump function to use",
+ "type": "string"
+ },
+ "subscribe": {
+ "description": "The multicast group to subscribe to",
+ "type": "string"
+ },
+ "sleep": {
+ "description": "The number to seconds to sleep",
+ "type": "number",
+ "default": 0
+ },
+ "json-params": {
+ "description": "The json params to use for different functions",
+ "type": "object",
+ "patternProperties": {
+ "^.*$": {
+ "type": ["string", "number", "object"],
+ "patternProperties": {
+ "^.*$": {
+ "type": ["string", "number"]
+ }
+ },
+ "additionalProperties": false
+ }
+ }
+ },
+ "additionalProperties": false
+ },
+ "additionalProperties": false
+ }
+ },
+ "additionalProperties": false
+ }
+ },
+ "additionalProperties": false,
+ "required": [
+ "yaml-specs-path"
+ ]
+}
--
2.41.0
Powered by blists - more mailing lists