Towards A1 v1.0.0
[ric-plt/a1.git] / a1 / data.py
1 # ==================================================================================
2 #       Copyright (c) 2019 Nokia
3 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
4 #
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
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
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 # ==================================================================================
17
18 """
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
22
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
25 """
26 from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists
27 from a1 import get_module_logger
28
29 logger = get_module_logger(__name__)
30
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
33 POLICY_DATA = {}
34 I = "instances"
35 H = "handlers"
36 D = "data"
37
38 # Types
39
40
41 def type_is_valid(policy_type_id):
42     """
43     check that a type is valid
44     """
45     if policy_type_id not in POLICY_DATA:
46         logger.error("%s not found", policy_type_id)
47         raise PolicyTypeNotFound()
48
49
50 def store_policy_type(policy_type_id, body):
51     """
52     store a policy type if it doesn't already exist
53     """
54     if policy_type_id in POLICY_DATA:
55         raise PolicyTypeAlreadyExists()
56
57     POLICY_DATA[policy_type_id] = {}
58     POLICY_DATA[policy_type_id][D] = body
59     POLICY_DATA[policy_type_id][I] = {}
60
61
62 def get_policy_type(policy_type_id):
63     """
64     retrieve a type
65     """
66     type_is_valid(policy_type_id)
67     return POLICY_DATA[policy_type_id][D]
68
69
70 # Instances
71
72
73 def instance_is_valid(policy_type_id, policy_instance_id):
74     """
75     check that an instance is valid
76     """
77     type_is_valid(policy_type_id)
78     if policy_instance_id not in POLICY_DATA[policy_type_id][I]:
79         raise PolicyInstanceNotFound
80
81
82 def store_policy_instance(policy_type_id, policy_instance_id, instance):
83     """
84     Store a policy instance
85     """
86     type_is_valid(policy_type_id)
87
88     # store the instance
89     # Reset the statuses because this is a new policy instance, even if it was overwritten
90     POLICY_DATA[policy_type_id][I][policy_instance_id] = {}
91     POLICY_DATA[policy_type_id][I][policy_instance_id][D] = instance
92     POLICY_DATA[policy_type_id][I][policy_instance_id][H] = {}
93
94
95 def get_policy_instance(policy_type_id, policy_instance_id):
96     """
97     Retrieve a policy instance
98     """
99     type_is_valid(policy_type_id)
100     instance_is_valid(policy_type_id, policy_instance_id)
101     return POLICY_DATA[policy_type_id][I][policy_instance_id][D]
102
103
104 def get_policy_instance_statuses(policy_type_id, policy_instance_id):
105     """
106     Retrieve the status vector for a policy instance
107     """
108     type_is_valid(policy_type_id)
109     instance_is_valid(policy_type_id, policy_instance_id)
110
111     return [{"handler_id": k, "status": v} for k, v in POLICY_DATA[policy_type_id][I][policy_instance_id][H].items()]
112
113
114 def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, status):
115     """
116     Update the status of a handler id of a policy instance
117     """
118     type_is_valid(policy_type_id)
119     instance_is_valid(policy_type_id, policy_instance_id)
120
121     POLICY_DATA[policy_type_id][I][policy_instance_id][H][handler_id] = status