Add to_directory method to relevant object classes
[oam.git] / code / network-topology-instance-generator / controller / parameter_validator.py
1 # Copyright 2022 highstreet technologies GmbH
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 #!/usr/bin/python
16 """
17 Module containing a class for parameter validation
18 """
19 import os
20 import os.path
21 import json
22 from typing import Dict, Union
23 import jsonschema
24
25
26 class ParameterValidator:
27     """
28     Class validating the configuration as input for the generator.
29     """
30
31     __config_file: str = "config.json"
32     __configuration: dict = {}
33     __configuration_schema_file: str = os.path.dirname(os.path.realpath(
34         __file__)) + "/../model/jsonSchema/configuration.schema.json"
35     __config_schema: dict = {}
36     __error_messsage: str = ""
37     __is_valid: bool = False
38
39     # constructor
40     def __init__(self, args):
41         self.args = args
42
43         if len(self.args) > 1:
44             self.__config_file = args[1]
45
46         if os.path.isfile(self.__config_file) is False:
47             print("File", self.__config_file, "does not exist.")
48         else:
49             with open(self.__config_file) as content:
50                 self.__configuration = json.load(content)
51
52         if os.path.isfile(self.__configuration_schema_file) is False:
53             print("File", self.__configuration_schema_file, "does not exist.")
54         else:
55             with open(self.__configuration_schema_file) as content:
56                 self.__config_schema = json.load(content)
57         self.__is_valid = self.__is_json_valid(
58             self.__configuration, self.__config_schema)
59
60     def configuration_file(self) -> str:
61         """
62         Getter for the configuration filename.
63         :return Filename (path) for the init configuration.
64         """
65         return self.__config_file
66
67     def configuration(self) -> Dict[str, Union[str, Dict[str, int]]]:
68         """
69         Getter for the configuration as input parameter.
70         :return Init configuration as Dict.
71         """
72         return self.__configuration
73
74     def is_valid(self) -> bool:
75         """
76         Getter for the validation result.
77         :return Init configuration as Dict.
78         """
79         return self.__is_valid
80
81     def error_message(self) -> str:
82         """
83         Getter for the error message after validation process or an empty sting,
84         when configuration is valid.
85         :return Errormessage as string.
86         """
87         return self.__error_messsage
88
89     # private
90
91     def __is_json_valid(self, json_data, json_schema) -> bool:
92         """
93         Method validating json against a schema
94         """
95         try:
96             jsonschema.validate(instance=json_data, schema=json_schema)
97             self.__error_messsage = ""
98         except jsonschema.exceptions.ValidationError as err:
99             self.__error_messsage = err
100             return False
101         return True