Create a validator for the config json 82/11882/1
authorMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Tue, 10 Oct 2023 14:22:01 +0000 (16:22 +0200)
committerMartin Skorupski <martin.skorupski@highstreet-technologies.com>
Tue, 10 Oct 2023 14:22:06 +0000 (16:22 +0200)
- parameter_validator.py added to app controller

Issue-ID: OAM-356
Change-Id: I743cf9b2d09ad030f2ff95cc5ab394003c3dd5e4
Signed-off-by: Martin Skorupski <martin.skorupski@highstreet-technologies.com>
code/network-generator/controller/parameter_validator.py [new file with mode: 0644]

diff --git a/code/network-generator/controller/parameter_validator.py b/code/network-generator/controller/parameter_validator.py
new file mode 100644 (file)
index 0000000..0375498
--- /dev/null
@@ -0,0 +1,101 @@
+# Copyright 2023 highstreet technologies GmbH
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#!/usr/bin/python
+"""
+Module containing a class for parameter validation
+"""
+import os
+import os.path
+import json
+from typing import Dict, Union
+import jsonschema
+
+
+class ParameterValidator:
+    """
+    Class validating the configuration as input for the generator.
+    """
+
+    __config_file: str = "config.json"
+    __configuration: dict = {}
+    __configuration_schema_file: str = os.path.dirname(os.path.realpath(
+        __file__)) + "/../model/jsonSchema/configuration.schema.json"
+    __config_schema: dict = {}
+    __error_messsage: str = ""
+    __is_valid: bool = False
+
+    # constructor
+    def __init__(self, args):
+        self.args = args
+
+        if len(self.args) > 1:
+            self.__config_file = args[1]
+
+        if os.path.isfile(self.__config_file) is False:
+            print("File", self.__config_file, "does not exist.")
+        else:
+            with open(self.__config_file) as content:
+                self.__configuration = json.load(content)
+
+        if os.path.isfile(self.__configuration_schema_file) is False:
+            print("File", self.__configuration_schema_file, "does not exist.")
+        else:
+            with open(self.__configuration_schema_file) as content:
+                self.__config_schema = json.load(content)
+        self.__is_valid = self.__is_json_valid(
+            self.__configuration, self.__config_schema)
+
+    def configuration_file(self) -> str:
+        """
+        Getter for the configuration filename.
+        :return Filename (path) for the init configuration.
+        """
+        return self.__config_file
+
+    def configuration(self) -> Dict[str, Union[str, Dict[str, int]]]:
+        """
+        Getter for the configuration as input parameter.
+        :return Init configuration as Dict.
+        """
+        return self.__configuration
+
+    def is_valid(self) -> bool:
+        """
+        Getter for the validation result.
+        :return Init configuration as Dict.
+        """
+        return self.__is_valid
+
+    def error_message(self) -> str:
+        """
+        Getter for the error message after validation process or an empty sting,
+        when configuration is valid.
+        :return Errormessage as string.
+        """
+        return self.__error_messsage
+
+    # private
+
+    def __is_json_valid(self, json_data, json_schema) -> bool:
+        """
+        Method validating json against a schema
+        """
+        try:
+            jsonschema.validate(instance=json_data, schema=json_schema)
+            self.__error_messsage = ""
+        except jsonschema.exceptions.ValidationError as err:
+            self.__error_messsage = err
+            return False
+        return True