Updates:
[ric-plt/a1.git] / a1 / controller.py
1 """
2 Main a1 controller
3 """
4 # ==================================================================================
5 #       Copyright (c) 2019-2020 Nokia
6 #       Copyright (c) 2018-2020 AT&T Intellectual Property.
7 #
8 #   Licensed under the Apache License, Version 2.0 (the "License");
9 #   you may not use this file except in compliance with the License.
10 #   You may obtain a copy of the License at
11 #
12 #          http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #   Unless required by applicable law or agreed to in writing, software
15 #   distributed under the License is distributed on an "AS IS" BASIS,
16 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #   See the License for the specific language governing permissions and
18 #   limitations under the License.
19 # ==================================================================================
20 from jsonschema import validate
21 from jsonschema.exceptions import ValidationError
22 import connexion
23 from mdclogpy import Logger
24 from ricsdl.exceptions import RejectedByBackend, NotConnected, BackendError
25 from a1 import a1rmr, exceptions, data
26
27
28 mdc_logger = Logger(name=__name__)
29
30
31 def _try_func_return(func):
32     """
33     generic caller that returns the apporp http response if exceptions are raised
34     """
35     try:
36         return func()
37     except (ValidationError, exceptions.PolicyTypeAlreadyExists, exceptions.CantDeleteNonEmptyType):
38         return "", 400
39     except (exceptions.PolicyTypeNotFound, exceptions.PolicyInstanceNotFound):
40         return "", 404
41     except (RejectedByBackend, NotConnected, BackendError):
42         """
43         These are SDL errors. At the time of development here, we do not have a good understanding which of these errors are "try again later it may work"
44         and which are "never going to work". There is some discussion that RejectedByBackend is in the latter category, suggesting it should map to 400,
45         but until we understand the root cause of these errors, it's confusing to clients to give them a 400 (a "your fault" code) because they won't know how to fix
46         For now, we log, and 503, and investigate the logs later to improve the handling/reporting.
47         """
48         # mdc_logger.exception(exc)  # waiting for https://jira.o-ran-sc.org/browse/RIC-39
49         return "", 503
50
51     # let other types of unexpected exceptions blow up and log
52
53
54 # Healthcheck
55
56
57 def get_healthcheck():
58     """
59     Handles healthcheck GET
60     Currently, this checks:
61     1. whether the a1 webserver is up (if it isn't, this won't even be called, so even entering this function confirms it is)
62     2. checks whether the rmr thread is running and has completed a loop recently
63     3. checks that our SDL connection is healthy
64     """
65     if not a1rmr.healthcheck_rmr_thread():
66         mdc_logger.debug("A1 is not healthy due to the rmr thread")
67         return "rmr thread is unhealthy", 500
68     if not data.SDL.healthcheck():
69         mdc_logger.debug("A1 is not healthy because it does not have a connection to SDL")
70         return "sdl connection is unhealthy", 500
71     return "", 200
72
73
74 # Policy types
75
76
77 def get_all_policy_types():
78     """
79     Handles GET /a1-p/policytypes
80     """
81     return _try_func_return(data.get_type_list)
82
83
84 def create_policy_type(policy_type_id):
85     """
86     Handles PUT /a1-p/policytypes/policy_type_id
87     """
88
89     def put_type_handler():
90         data.store_policy_type(policy_type_id, body)
91         mdc_logger.debug("Policy type {} created.".format(policy_type_id))
92         return "", 201
93
94     body = connexion.request.json
95     return _try_func_return(put_type_handler)
96
97
98 def get_policy_type(policy_type_id):
99     """
100     Handles GET /a1-p/policytypes/policy_type_id
101     """
102     return _try_func_return(lambda: data.get_policy_type(policy_type_id))
103
104
105 def delete_policy_type(policy_type_id):
106     """
107     Handles DELETE /a1-p/policytypes/policy_type_id
108     """
109
110     def delete_policy_type_handler():
111         data.delete_policy_type(policy_type_id)
112         mdc_logger.debug("Policy type {} deleted.".format(policy_type_id))
113         return "", 204
114
115     return _try_func_return(delete_policy_type_handler)
116
117
118 # Policy instances
119
120
121 def get_all_instances_for_type(policy_type_id):
122     """
123     Handles GET /a1-p/policytypes/policy_type_id/policies
124     """
125     return _try_func_return(lambda: data.get_instance_list(policy_type_id))
126
127
128 def get_policy_instance(policy_type_id, policy_instance_id):
129     """
130     Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id
131     """
132     return _try_func_return(lambda: data.get_policy_instance(policy_type_id, policy_instance_id))
133
134
135 def get_policy_instance_status(policy_type_id, policy_instance_id):
136     """
137     Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id/status
138
139     Return the aggregated status. The order of rules is as follows:
140         1. If a1 has received at least one status, and *all* received statuses are "DELETED", we blow away the instance and return a 404
141         2. if a1 has received at least one status and at least one is OK, we return "IN EFFECT"
142         3. "NOT IN EFFECT" otherwise (no statuses, or none are OK but not all are deleted)
143     """
144
145     return _try_func_return(lambda: data.get_policy_instance_status(policy_type_id, policy_instance_id))
146
147
148 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
149     """
150     Handles PUT /a1-p/policytypes/polidyid/policies/policy_instance_id
151     """
152     instance = connexion.request.json
153
154     def put_instance_handler():
155         """
156         Handles policy instance put
157
158         For now, policy_type_id is used as the message type
159         """
160         #  validate the PUT against the schema
161         schema = data.get_policy_type(policy_type_id)["create_schema"]
162         validate(instance=instance, schema=schema)
163
164         # store the instance
165         data.store_policy_instance(policy_type_id, policy_instance_id, instance)
166
167         # queue rmr send (best effort)
168         a1rmr.queue_instance_send(("CREATE", policy_type_id, policy_instance_id, instance))
169
170         return "", 202
171
172     return _try_func_return(put_instance_handler)
173
174
175 def delete_policy_instance(policy_type_id, policy_instance_id):
176     """
177     Handles DELETE /a1-p/policytypes/polidyid/policies/policy_instance_id
178     """
179
180     def delete_instance_handler():
181         data.delete_policy_instance(policy_type_id, policy_instance_id)
182
183         # queue rmr send (best effort)
184         a1rmr.queue_instance_send(("DELETE", policy_type_id, policy_instance_id, ""))
185
186         return "", 202
187
188     return _try_func_return(delete_instance_handler)