Move all business logic code under template folder
[oam.git] / code / network-generator / controller / parameter_validator.py
1 # Copyright 2023 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 import jsonschema
23
24
25 class ParameterValidator:
26     """
27     Class validating the configuration as input for the generator.
28     """
29
30     __config_file: str = "config.json"
31     __configuration: dict = {}
32     __configuration_schema_file: str = (
33         os.path.dirname(os.path.realpath(__file__))
34         + "/../model/jsonSchema/configuration.schema.json"
35     )
36     __config_schema: dict = {}
37     __error_message: str = ""
38     __is_valid: bool = False
39
40     # constructor
41     def __init__(self, args):
42         self.args = args
43
44         if len(self.args) > 1:
45             self.__config_file = args[1]
46
47         if os.path.isfile(self.__config_file) is False:
48             print("File", self.__config_file, "does not exist.")
49         else:
50             with open(self.__config_file) as content:
51                 self.__configuration = json.load(content)
52
53         if os.path.isfile(self.__configuration_schema_file) is False:
54             print("File", self.__configuration_schema_file, "does not exist.")
55         else:
56             with open(self.__configuration_schema_file) as content:
57                 self.__config_schema = json.load(content)
58         self.__is_valid = self.__is_json_valid(
59             self.__configuration, self.__config_schema
60         )
61
62     def configuration_file(self) -> str:
63         """
64         Getter for the configuration filename.
65         :return Filename (path) for the init configuration.
66         """
67         return self.__config_file
68
69     def configuration(self) -> dict[str, str | dict[str, int]]:
70         """
71         Getter for the configuration as input parameter.
72         :return Init configuration as dict.
73         """
74         return self.__configuration
75
76     def is_valid(self) -> bool:
77         """
78         Getter for the validation result.
79         :return Init configuration as dict.
80         """
81         return self.__is_valid
82
83     def error_message(self) -> str:
84         """
85         Getter for the error message after validation process or an empty sting,
86         when configuration is valid.
87         :return Error message as string.
88         """
89         return self.__error_message
90
91     # private
92
93     def __is_json_valid(self, json_data, json_schema) -> bool:
94         """
95         Method validating json against a schema
96         """
97         try:
98             jsonschema.validate(instance=json_data, schema=json_schema)
99             self.__error_message = ""
100         except jsonschema.exceptions.ValidationError as err:
101             self.__error_message = err
102             return False
103         return True