c63ae5ab8aca23661a7432e4fdc8c2afbeace4c8
[optf/osdf.git] / 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
22
23 def get_local_policies(local_policy_folder, local_policy_list, policy_id_list=None):
24     """
25     Get policies from a local file system.
26     Required for the following scenarios:
27     (a) doing work-arounds (e.g. if we are asked to drop some policies for testing purposes)
28     (b) work-arounds when policy platform is giving issues (e.g. if dev/IST policies are wiped out in an upgrade)
29     :param local_policy_folder: where the policy files are present
30     :param local_policy_list: list of local policies
31     :param policy_id_list: list of policies to get (if unspecified or None, get all)
32     :return: get policies
33     """
34     policies = []
35     for fname in local_policy_list:  # ugly removal of .json from file name
36         if policy_id_list and fname[:-5] not in policy_id_list:
37             continue
38         with open(os.path.join(local_policy_folder, fname)) as fid:
39             policies.append(json.load(fid))
40     return policies