summaryrefslogtreecommitdiffstats
path: root/csarvalidation/python/main/validation/rules/providers/ActiveRulesProvider.py
blob: 1d601f258f5f5d3e541fcef4dbac4ed236d801db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# ============LICENSE_START====================================
# vnfsdk-validation
# =========================================================
# Copyright (C) 2020 Nokia. All rights reserved.
# =========================================================
# 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.
# ============LICENSE_END=====================================

class ActiveRulesProvider:

    def __init__(self, properties_file_path: str, rules_separator: str = ",",  key_value_separator: str = "="):
        self.properties_file_path = properties_file_path
        self._rules_separator = rules_separator
        self._key_value_separator = key_value_separator

    def get_active_rules(self, reqs: str) -> list:
        active_rules = []
        for line in self._load_file_by_lines():
            if line.startswith(reqs):
                active_rules = self._split_properties_line(line, self._rules_separator, self._key_value_separator)
                break
        return active_rules

    def _load_file_by_lines(self) -> list:
        with open(self.properties_file_path, 'r') as properties_file:
            lines = properties_file.read().splitlines()
        return lines

    @staticmethod
    def _split_properties_line(line: str, splitter: str, key_value_separator: str) -> list:
        key_value_separation_index = line.index(key_value_separator) + 1
        value = line[key_value_separation_index:]
        return value.split(splitter)