A1 v2.1.0
[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 flask import Response
21 from jsonschema import validate
22 from jsonschema.exceptions import ValidationError
23 import connexion
24 from mdclogpy import Logger
25 from ricsdl.exceptions import RejectedByBackend, NotConnected, BackendError
26 from a1 import a1rmr, exceptions, data
27
28
29 mdc_logger = Logger(name=__name__)
30
31
32 def _try_func_return(func):
33     """
34     generic caller that returns the apporp http response if exceptions are raised
35     """
36     try:
37         return func()
38     except (ValidationError, exceptions.PolicyTypeAlreadyExists, exceptions.CantDeleteNonEmptyType):
39         return "", 400
40     except (exceptions.PolicyTypeNotFound, exceptions.PolicyInstanceNotFound):
41         return "", 404
42     except (RejectedByBackend, NotConnected, BackendError):
43         """
44         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"
45         and which are "never going to work". There is some discussion that RejectedByBackend is in the latter category, suggesting it should map to 400,
46         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
47         For now, we log, and 503, and investigate the logs later to improve the handling/reporting.
48         """
49         # mdc_logger.exception(exc)  # waiting for https://jira.o-ran-sc.org/browse/RIC-39
50         return "", 503
51     except BaseException:
52         # catch all, should never happen...
53         # mdc_logger.exception(exc)  # waiting for https://jira.o-ran-sc.org/browse/RIC-39
54         return Response(status=500)
55
56
57 # Healthcheck
58
59
60 def get_healthcheck():
61     """
62     Handles healthcheck GET
63     Currently, this checks:
64     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)
65     2. checks whether the rmr thread is running and has completed a loop recently
66     TODO: make "seconds" to pass in a configurable parameter?
67     """
68     if a1rmr.healthcheck_rmr_thread():
69         return "", 200
70     return "rmr thread is unhealthy", 500
71
72
73 # Policy types
74
75
76 def get_all_policy_types():
77     """
78     Handles GET /a1-p/policytypes
79     """
80     return _try_func_return(data.get_type_list)
81
82
83 def create_policy_type(policy_type_id):
84     """
85     Handles PUT /a1-p/policytypes/policy_type_id
86     """
87
88     def put_type_handler():
89         data.store_policy_type(policy_type_id, body)
90         return "", 201
91
92     body = connexion.request.json
93     return _try_func_return(put_type_handler)
94
95
96 def get_policy_type(policy_type_id):
97     """
98     Handles GET /a1-p/policytypes/policy_type_id
99     """
100     return _try_func_return(lambda: data.get_policy_type(policy_type_id))
101
102
103 def delete_policy_type(policy_type_id):
104     """
105     Handles DELETE /a1-p/policytypes/policy_type_id
106     """
107
108     def delete_policy_type_handler():
109         data.delete_policy_type(policy_type_id)
110         return "", 204
111
112     return _try_func_return(delete_policy_type_handler)
113
114
115 # Policy instances
116
117
118 def get_all_instances_for_type(policy_type_id):
119     """
120     Handles GET /a1-p/policytypes/policy_type_id/policies
121     """
122     return _try_func_return(lambda: data.get_instance_list(policy_type_id))
123
124
125 def get_policy_instance(policy_type_id, policy_instance_id):
126     """
127     Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id
128     """
129     return _try_func_return(lambda: data.get_policy_instance(policy_type_id, policy_instance_id))
130
131
132 def get_policy_instance_status(policy_type_id, policy_instance_id):
133     """
134     Handles GET /a1-p/policytypes/polidyid/policies/policy_instance_id/status
135
136     Return the aggregated status. The order of rules is as follows:
137         1. If a1 has received at least one status, and *all* received statuses are "DELETED", we blow away the instance and return a 404
138         2. if a1 has received at least one status and at least one is OK, we return "IN EFFECT"
139         3. "NOT IN EFFECT" otherwise (no statuses, or none are OK but not all are deleted)
140     """
141
142     return _try_func_return(lambda: data.get_policy_instance_status(policy_type_id, policy_instance_id))
143
144
145 def create_or_replace_policy_instance(policy_type_id, policy_instance_id):
146     """
147     Handles PUT /a1-p/policytypes/polidyid/policies/policy_instance_id
148     """
149     instance = connexion.request.json
150
151     def put_instance_handler():
152         """
153         Handles policy instance put
154
155         For now, policy_type_id is used as the message type
156         """
157         #  validate the PUT against the schema
158         schema = data.get_policy_type(policy_type_id)["create_schema"]
159         validate(instance=instance, schema=schema)
160
161         # store the instance
162         data.store_policy_instance(policy_type_id, policy_instance_id, instance)
163
164         # queue rmr send (best effort)
165         a1rmr.queue_instance_send(("CREATE", policy_type_id, policy_instance_id, instance))
166
167         return "", 202
168
169     return _try_func_return(put_instance_handler)
170
171
172 def delete_policy_instance(policy_type_id, policy_instance_id):
173     """
174     Handles DELETE /a1-p/policytypes/polidyid/policies/policy_instance_id
175     """
176
177     def delete_instance_handler():
178         """
179         here we send out the DELETEs but we don't delete the instance until a GET is called where we check the statuses
180         We also set the status as deleted which would be reflected in a GET to ../status (before the DELETE completes)
181         """
182         data.delete_policy_instance(policy_type_id, policy_instance_id)
183
184         # queue rmr send (best effort)
185         a1rmr.queue_instance_send(("DELETE", policy_type_id, policy_instance_id, ""))
186
187         return "", 202
188
189     return _try_func_return(delete_instance_handler)