Add Subscription get and list API; Provide swagger openAPI doc
[pti/o2.git] / o2ims / views / ocloud_route.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from flask import jsonify
16 from flask_restx import Resource
17
18 from o2ims import config
19 from o2ims.views import ocloud_view
20 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
21     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
22
23 apibase = config.get_o2ims_api_base()
24 # api = Namespace("O2IMS", description='IMS')
25
26
27 # ----------  OClouds ---------- #
28 api_ocloud = OcloudDTO.api
29
30
31 @api_ocloud.route("/")
32 class OcloudsListRouter(Resource):
33     """Ocloud get endpoint
34     O2 interface ocloud endpoint
35     """
36
37     ocloud_get = OcloudDTO.ocloud_list
38
39     @api_ocloud.marshal_list_with(ocloud_get)
40     def get(self):
41         return ocloud_view.oclouds(uow)
42
43
44 # ----------  ResourceTypes ---------- #
45 api_rt = ResourceTypeDTO.api
46
47
48 @api_rt.route("/resourceTypes")
49 class ResourceTypesListRouter(Resource):
50
51     model = ResourceTypeDTO.resource_type_get
52
53     @api_rt.marshal_list_with(model)
54     def get(self):
55         return ocloud_view.resource_types(uow)
56
57     # @api.doc(response={405: 'Method Not Allowed'})
58     # def post(self):
59     #     api.abort(405)
60
61     # @api.doc(response={405: 'Method Not Allowed'})
62     # def put(self):
63     #     api.abort(405)
64
65     # @api.doc(response={405: 'Method Not Allowed'})
66     # def patch(self):
67     #     api.abort(405)
68
69     # @api.doc(response={405: 'Method Not Allowed'})
70     # def delete(self):
71     #     api.abort(405)
72
73
74 @api_rt.route("/resourceTypes/<resourceTypeID>")
75 @api_rt.param('resourceTypeID', 'ID of the resource type')
76 @api_rt.response(404, 'Resource type not found')
77 class ResourceTypeGetRouter(Resource):
78
79     model = ResourceTypeDTO.resource_type_get
80
81     @api_rt.doc('Get resource type')
82     @api_rt.marshal_with(model)
83     def get(self, resourceTypeID):
84         result = ocloud_view.resource_type_one(resourceTypeID, uow)
85         if result is not None:
86             return result
87         api_rt.abort(
88             404, "Resource type {} doesn't exist".format(resourceTypeID))
89
90
91 # ----------  ResourcePools ---------- #
92 api_rp = ResourcePoolDTO.api
93
94
95 @api_rp.route("/resourcePools")
96 class ResourcePoolsListRouter(Resource):
97
98     model = ResourcePoolDTO.resource_pool_get
99
100     @api_rp.marshal_list_with(model)
101     def get(self):
102         return ocloud_view.resource_pools(uow)
103
104
105 @api_rp.route("/resourcePools/<resourcePoolID>")
106 @api_rp.param('resourcePoolID', 'ID of the resource pool')
107 @api_rp.response(404, 'Resource pool not found')
108 class ResourcePoolGetRouter(Resource):
109
110     model = ResourcePoolDTO.resource_pool_get
111
112     @api_rp.doc('Get resource pool')
113     def get(self, resourcePoolID):
114         result = ocloud_view.resource_pool_one(resourcePoolID, uow)
115         if result is not None:
116             return result
117         api_rp.abort(
118             404, "Resource pool {} doesn't exist".format(resourcePoolID))
119
120
121 # ----------  Resources ---------- #
122 api_res = ResourceDTO.api
123
124
125 @api_res.route("/resourcePools/<resourcePoolID>/resources")
126 @api_res.param('resourcePoolID', 'ID of the resource pool')
127 class ResourcesListRouter(Resource):
128
129     model = ResourceDTO.resource_list
130
131     @api_res.marshal_list_with(model)
132     def get(self, resourcePoolID):
133         return ocloud_view.resources(resourcePoolID, uow)
134
135
136 @api_res.route("/resourcePools/<resourcePoolID>/resources/<resourceID>")
137 @api_res.param('resourcePoolID', 'ID of the resource pool')
138 @api_res.param('resourceID', 'ID of the resource')
139 @api_res.response(404, 'Resource not found')
140 class ResourceGetRouter(Resource):
141
142     model = ResourceDTO.resource_get
143
144     @api_res.doc('Get resource')
145     def get(self, resourcePoolID, resourceID):
146         result = ocloud_view.resource_one(resourceID, uow)
147         if result is not None:
148             return result
149         api_res.abort(404, "Resource {} doesn't exist".format(resourceID))
150
151
152 # ----------  DeploymentManagers ---------- #
153 api_dm = DeploymentManagerDTO.api
154
155
156 @api_dm.route("/deploymentManagers")
157 class DeploymentManagersListRouter(Resource):
158
159     model = DeploymentManagerDTO.deployment_manager_get
160
161     @api_dm.marshal_list_with(model)
162     def get(self):
163         return ocloud_view.deployment_managers(uow)
164
165
166 @api_dm.route("/deploymentManagers/<deploymentManagerID>")
167 @api_dm.param('deploymentManagerID', 'ID of the deployment manager')
168 @api_dm.response(404, 'Deployment manager not found')
169 class DeploymentManagerGetRouter(Resource):
170
171     model = DeploymentManagerDTO.deployment_manager_get
172
173     @api_dm.doc('Get deployment manager')
174     def get(self, deploymentManagerID):
175         result = ocloud_view.deployment_manager_one(
176             deploymentManagerID, uow)
177         if result is not None:
178             return result
179         api_dm.abort(404, "Deployment manager {} doesn't exist".format(
180             deploymentManagerID))
181
182
183 # ----------  Subscriptions ---------- #
184 api_sub = SubscriptionDTO.api
185
186
187 @api_sub.route("/subscriptions")
188 class SubscriptionsListRouter(Resource):
189
190     model = SubscriptionDTO.subscription_get
191
192     @api_sub.marshal_list_with(model)
193     def get(self):
194         return ocloud_view.subscriptions(uow)
195
196
197 @api_sub.route("/subscriptions/<subscriptionID>")
198 @api_sub.param('subscriptionID', 'ID of the subscription')
199 @api_sub.response(404, 'Subscription not found')
200 class SubscriptionGetRouter(Resource):
201
202     model = DeploymentManagerDTO.deployment_manager_get
203
204     @api_sub.doc('Get subscription by ID')
205     def get(self, subscriptionID):
206         result = ocloud_view.subscription_one(
207             subscriptionID, uow)
208         if result is not None:
209             return result
210         api_sub.abort(404, "Subscription {} doesn't exist".format(
211             subscriptionID))
212
213
214 def configure_namespace(app, bus):
215
216     app.add_namespace(api_ocloud, path=apibase)
217     app.add_namespace(api_rt, path=apibase)
218     app.add_namespace(api_rp, path=apibase)
219     app.add_namespace(api_res, path=apibase)
220     app.add_namespace(api_dm, path=apibase)
221     app.add_namespace(api_sub, path=apibase)
222
223     # Set global uow
224     global uow
225     uow = bus.uow
226
227
228 def configure_routes(app, bus):
229
230     # ----------  OClouds ---------- #
231     @app.route(apibase, methods=["GET"])
232     def oclouds():
233         result = ocloud_view.oclouds(bus.uow)
234         return jsonify(result), 200
235
236     # ----------  ResourceTypes ---------- #
237
238     @app.route(apibase + "/resourceTypes", methods=["GET"])
239     def resource_types():
240         result = ocloud_view.resource_types(bus.uow)
241         return jsonify(result), 200
242
243     @app.route(apibase + "/resourceTypes", methods=["POST", "PUT", "PATCH",
244                                                     "DELETE"])
245     def resource_types_not_allow():
246         return "Method Not Allowed", 405
247
248     @app.route(apibase + "/resourceTypes/<resourceTypeID>", methods=["GET"])
249     def resource_types_one(resourceTypeID):
250         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
251         if result is None:
252             return "", 200
253         return jsonify(result), 200
254
255     @app.route(apibase + "/resourceTypes/<resourceTypeID>",
256                methods=["POST", "PUT", "PATCH", "DELETE"])
257     def resource_types_one_not_allow(resourceTypeID):
258         return "Method Not Allowed", 405
259
260     # ----------  ResourcePools ---------- #
261
262     @app.route(apibase + "/resourcePools", methods=["GET"])
263     def resource_pools():
264         result = ocloud_view.resource_pools(bus.uow)
265         return jsonify(result), 200
266
267     @app.route(apibase + "/resourcePools", methods=["POST", "PUT", "PATCH",
268                                                     "DELETE"])
269     def resource_pools_not_allow():
270         return "Method Not Allowed", 405
271
272     @app.route(apibase + "/resourcePools/<resourcePoolID>", methods=["GET"])
273     def resource_pools_one(resourcePoolID):
274         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
275         if result is None:
276             return "", 200
277         return jsonify(result), 200
278
279     @app.route(apibase + "/resourcePools/<resourcePoolID>",
280                methods=["POST", "PUT", "PATCH", "DELETE"])
281     def resource_pools_one_not_allow(resourcePoolID):
282         return "Method Not Allowed", 405
283
284     # ----------  Resources ---------- #
285
286     @app.route(apibase + "/resourcePools/<resourcePoolID>/resources",
287                methods=["GET"])
288     def resources(resourcePoolID):
289         result = ocloud_view.resources(resourcePoolID, bus.uow)
290         return jsonify(result), 200
291
292     @app.route(apibase + "/resourcePools/<resourcePoolID>/resources",
293                methods=["POST", "PUT", "PATCH", "DELETE"])
294     def resource_not_allow(resourcePoolID):
295         return "Method Not Allowed", 405
296
297     @app.route(apibase +
298                "/resourcePools/<resourcePoolID>/resources/<resourceID>",
299                methods=["GET"])
300     def resources_one(resourcePoolID, resourceID):
301         result = ocloud_view.resource_one(resourceID, bus.uow)
302         if result is None:
303             return "", 200
304         return jsonify(result), 200
305
306     @app.route(apibase +
307                "/resourcePools/<resourcePoolID>/resources/<resourceID>",
308                methods=["POST", "PUT", "PATCH", "DELETE"])
309     def resource_one_not_allow(resourcePoolID, resourceID):
310         return "Method Not Allowed", 405
311
312     # ----------  DeploymentManagers ---------- #
313
314     @app.route(apibase + "/deploymentManagers", methods=["GET"])
315     def deployment_managers():
316         result = ocloud_view.deployment_managers(bus.uow)
317         return jsonify(result), 200
318
319     @app.route(apibase + "/deploymentManagers",
320                methods=["POST", "PUT", "PATCH", "DELETE"])
321     def deployment_managers_not_allow():
322         return "Method Not Allowed", 405
323
324     @app.route(apibase + "/deploymentManagers/<deploymentManagerID>",
325                methods=["GET"])
326     def deployment_manager_one(deploymentManagerID):
327         result = ocloud_view.deployment_manager_one(
328             deploymentManagerID, bus.uow)
329         if result is None:
330             return "", 200
331         return jsonify(result), 200
332
333     @app.route(apibase + "/deploymentManagers/<deploymentManagerID>",
334                methods=["POST", "PUT", "PATCH", "DELETE"])
335     def deployment_manager_one_not_allow(deploymentManagerID):
336         return "Method Not Allowed", 405