From b7662dbcd98cec2342410f5ab1432f71ae72775e Mon Sep 17 00:00:00 2001 From: Martin Skorupski Date: Tue, 10 Oct 2023 16:22:01 +0200 Subject: [PATCH] Create a validator for the config json - parameter_validator.py added to app controller Issue-ID: OAM-356 Change-Id: I743cf9b2d09ad030f2ff95cc5ab394003c3dd5e4 Signed-off-by: Martin Skorupski --- .../controller/parameter_validator.py | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 code/network-generator/controller/parameter_validator.py diff --git a/code/network-generator/controller/parameter_validator.py b/code/network-generator/controller/parameter_validator.py new file mode 100644 index 0000000..0375498 --- /dev/null +++ b/code/network-generator/controller/parameter_validator.py @@ -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 -- 2.16.6