Merge "Traffic Distributtion support added"
[optf/osdf.git] / osdf / adapters / local_data / local_policies.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import json
20 import os
21 import re
22 from osdf.logging.osdf_logging import debug_log
23
24 def get_local_policies(local_policy_folder, local_policy_list, policy_id_list=None):
25     """
26     Get policies from a local file system.
27     Required for the following scenarios:
28     (a) doing work-arounds (e.g. if we are asked to drop some policies for testing purposes)
29     (b) work-arounds when policy platform is giving issues (e.g. if dev/IST policies are wiped out in an upgrade)
30     :param local_policy_folder: where the policy files are present
31     :param local_policy_list: list of local policies
32     :param policy_id_list: list of policies to get (if unspecified or None, get all)
33     :return: get policies
34     """
35     debug_log.debug("Policy folder: {}, local_list {}, policy id list {}".format(local_policy_folder, local_policy_list, policy_id_list))
36     policies = []
37     if policy_id_list:
38         for policy_id in policy_id_list:
39             with open(os.path.join(local_policy_folder, policy_id + ".json")) as fid:
40                 policies.append(json.load(fid))
41     else:
42         for fname in local_policy_list:
43             with open(os.path.join(local_policy_folder, fname)) as fid:
44                 policies.append(json.load(fid))
45     return policies
46
47
48 def get_policy_names_from_file(fname_for_list_of_files):
49     """Get policy names; take care of comments, empty lines, etc"""
50     with open(fname_for_list_of_files) as fid:
51         return [
52             re.sub(r'#.*$', '', x).strip()  # remove inline comments and strip spaces
53             for x in fid
54             if not re.search(r'^#|^$', x.strip())  # remove blank or comments-only lines
55         ]