1 # ==================================================================================
2 # Copyright (c) 2019 Nokia
3 # Copyright (c) 2018-2019 AT&T Intellectual Property.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
19 Represents A1s database and database access functions.
20 In the future, this may change to use a different backend, possibly dramatically.
21 Hopefully, the access functions are a good api so nothing else has to change when this happens
23 For now, the database is in memory.
24 We use dict data structures (KV) with the expectation of having to move this into Redis
26 from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound
27 from a1 import get_module_logger
29 logger = get_module_logger(__name__)
31 # This is essentially mockouts for future KV
32 # Note that the D subkey won't be needed when in redis, since you can store data at x anx x_y
39 # TODO: REMOVE THIS!! (should be done via PUT)
40 POLICY_DATA[20000] = {}
41 POLICY_DATA[20000][D] = {"foo": "bar"}
42 POLICY_DATA[20000][I] = {}
43 POLICY_DATA[20001] = {}
44 POLICY_DATA[20001][D] = {"foo": "bar"}
45 POLICY_DATA[20001][I] = {}
48 def type_is_valid(policy_type_id):
50 check that a type is valid
52 if policy_type_id not in POLICY_DATA:
53 logger.error("%s not found", policy_type_id)
54 raise PolicyTypeNotFound()
57 def instance_is_valid(policy_type_id, policy_instance_id):
59 check that an instance is valid
61 type_is_valid(policy_type_id)
62 if policy_instance_id not in POLICY_DATA[policy_type_id][I]:
63 raise PolicyInstanceNotFound
66 def store_policy_instance(policy_type_id, policy_instance_id, instance):
68 Store a policy instance
70 type_is_valid(policy_type_id)
73 # Reset the statuses because this is a new policy instance, even if it was overwritten
74 POLICY_DATA[policy_type_id][I][policy_instance_id] = {}
75 POLICY_DATA[policy_type_id][I][policy_instance_id][D] = instance
76 POLICY_DATA[policy_type_id][I][policy_instance_id][H] = {}
79 def get_policy_instance(policy_type_id, policy_instance_id):
81 Retrieve a policy instance
83 type_is_valid(policy_type_id)
84 instance_is_valid(policy_type_id, policy_instance_id)
85 return POLICY_DATA[policy_type_id][I][policy_instance_id][D]
88 def get_policy_instance_statuses(policy_type_id, policy_instance_id):
90 Retrieve the status vector for a policy instance
92 type_is_valid(policy_type_id)
93 instance_is_valid(policy_type_id, policy_instance_id)
95 return [{"handler_id": k, "status": v} for k, v in POLICY_DATA[policy_type_id][I][policy_instance_id][H].items()]
98 def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, status):
100 Update the status of a handler id of a policy instance
102 type_is_valid(policy_type_id)
103 instance_is_valid(policy_type_id, policy_instance_id)
105 POLICY_DATA[policy_type_id][I][policy_instance_id][H][handler_id] = status