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