From 262ecff70b9c28a87cb563c9b9781e12e2fa0e08 Mon Sep 17 00:00:00 2001 From: demx8as6 Date: Mon, 21 Feb 2022 16:00:13 +0100 Subject: [PATCH] Create a script to generate a Topology - add validation to the input configuration IssueID: OAM-249 Change-Id: I78dac0719f04a1074400563bc184e442ade54887 Signed-off-by: demx8as6 --- .../controller/parameter_validator.py | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 code/network-topology-instance-generator/controller/parameter_validator.py diff --git a/code/network-topology-instance-generator/controller/parameter_validator.py b/code/network-topology-instance-generator/controller/parameter_validator.py new file mode 100644 index 0000000..24b1166 --- /dev/null +++ b/code/network-topology-instance-generator/controller/parameter_validator.py @@ -0,0 +1,101 @@ +# Copyright 2022 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