af5e8c6cf5a464a29c307b6c45c175997a138921
[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 request
16 from flask_restx import Resource, reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2common.views.pagination_route import link_header, PAGE_PARAM
20 from o2common.views.route_exception import NotFoundException
21 from o2ims.domain import ocloud
22 from o2ims.views import ocloud_view
23 from o2ims.views.api_ns import api_ims_inventory as api_ims_inventory_v1
24 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
25     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
26
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
29
30
31 def configure_api_route():
32     # Set global bus for resource
33     global bus
34     bus = MessageBus.get_instance()
35
36
37 # ----------  API versions ---------- #
38 @api_ims_inventory_v1.route("/v1/api_versions")
39 class VersionRouter(Resource):
40     def get(self):
41         return {
42             'uriPrefix': request.base_url.rsplit('/', 1)[0],
43             'apiVersions': [{
44                 'version': '1.0.0',
45                 # 'isDeprecated': 'False',
46                 # 'retirementDate': ''
47             }]
48         }
49
50
51 # ----------  OClouds ---------- #
52 @api_ims_inventory_v1.route(*["/v1", "/v1/"])
53 @api_ims_inventory_v1.response(404, 'oCloud not found')
54 @api_ims_inventory_v1.param(
55     'all_fields',
56     'Set any value for show all fields. This value will cover "fields" ' +
57     'and "all_fields".',
58     _in='query')
59 @api_ims_inventory_v1.param(
60     'fields',
61     'Set fields to show, split by comma, "/" for parent and children.' +
62     ' Like "name,parent/children". This value will cover "exculde_fields".',
63     _in='query')
64 @api_ims_inventory_v1.param(
65     'exclude_fields',
66     'Set fields to exclude showing, split by comma, "/" for parent and ' +
67     'children. Like "name,parent/children". This value will cover ' +
68     '"exclude_default".',
69     _in='query')
70 @api_ims_inventory_v1.param(
71     'exclude_default',
72     'Exclude showing all default fields, Set "true" to enable.',
73     _in='query')
74 class OcloudsListRouter(Resource):
75     """Ocloud get endpoint
76     O2 interface ocloud endpoint
77     """
78
79     ocloud_get = OcloudDTO.ocloud
80
81     @api_ims_inventory_v1.marshal_with(ocloud_get)
82     def get(self):
83         res = ocloud_view.oclouds(bus.uow)
84         if len(res) > 0:
85             return res[0]
86         raise NotFoundException("oCloud doesn't exist")
87
88
89 # ----------  ResourceTypes ---------- #
90 @api_ims_inventory_v1.route("/v1/resourceTypes")
91 @api_ims_inventory_v1.param(PAGE_PARAM,
92                             'Page number of the results to fetch.' +
93                             ' Default: 1',
94                             _in='query', default=1)
95 @api_ims_inventory_v1.param(
96     'all_fields',
97     'Set any value for show all fields. This value will cover "fields" ' +
98     'and "all_fields".',
99     _in='query')
100 @api_ims_inventory_v1.param(
101     'fields',
102     'Set fields to show, split by comma, "/" for parent and children.' +
103     ' Like "name,parent/children". This value will cover "exculde_fields".',
104     _in='query')
105 @api_ims_inventory_v1.param(
106     'exclude_fields',
107     'Set fields to exclude showing, split by comma, "/" for parent and ' +
108     'children. Like "name,parent/children". This value will cover ' +
109     '"exclude_default".',
110     _in='query')
111 @api_ims_inventory_v1.param(
112     'exclude_default',
113     'Exclude showing all default fields, Set "true" to enable.',
114     _in='query')
115 @api_ims_inventory_v1.param(
116     'filter',
117     'Filter of the query.',
118     _in='query')
119 class ResourceTypesListRouter(Resource):
120
121     model = ResourceTypeDTO.resource_type_get
122
123     @api_ims_inventory_v1.marshal_list_with(model)
124     def get(self):
125         parser = reqparse.RequestParser()
126         parser.add_argument(PAGE_PARAM, location='args')
127         parser.add_argument('filter', location='args')
128         args = parser.parse_args()
129         kwargs = {}
130         if args.nextpage_opaque_marker is not None:
131             kwargs['page'] = args.nextpage_opaque_marker
132         kwargs['filter'] = args.filter if args.filter is not None else ''
133
134         ret = ocloud_view.resource_types(bus.uow, **kwargs)
135         return link_header(request.full_path, ret)
136
137
138 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
139 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
140 @api_ims_inventory_v1.response(404, 'Resource type not found')
141 @api_ims_inventory_v1.param(
142     'all_fields',
143     'Set any value for show all fields. This value will cover "fields" ' +
144     'and "all_fields".',
145     _in='query')
146 @api_ims_inventory_v1.param(
147     'fields',
148     'Set fields to show, split by comma, "/" for parent and children.' +
149     ' Like "name,parent/children". This value will cover "exculde_fields".',
150     _in='query')
151 @api_ims_inventory_v1.param(
152     'exclude_fields',
153     'Set fields to exclude showing, split by comma, "/" for parent and ' +
154     'children. Like "name,parent/children". This value will cover ' +
155     '"exclude_default".',
156     _in='query')
157 @api_ims_inventory_v1.param(
158     'exclude_default',
159     'Exclude showing all default fields, Set "true" to enable.',
160     _in='query')
161 class ResourceTypeGetRouter(Resource):
162
163     model = ResourceTypeDTO.resource_type_get
164
165     @api_ims_inventory_v1.doc('Get resource type')
166     @api_ims_inventory_v1.marshal_with(model)
167     def get(self, resourceTypeID):
168         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
169         if result is not None:
170             return result
171         raise NotFoundException("Resource type {} doesn't exist".format(
172             resourceTypeID))
173
174
175 # ----------  ResourcePools ---------- #
176 @api_ims_inventory_v1.route("/v1/resourcePools")
177 @api_ims_inventory_v1.param(PAGE_PARAM,
178                             'Page number of the results to fetch.' +
179                             ' Default: 1',
180                             _in='query', default=1)
181 @api_ims_inventory_v1.param(
182     'all_fields',
183     'Set any value for show all fields. This value will cover "fields" ' +
184     'and "all_fields".',
185     _in='query')
186 @api_ims_inventory_v1.param(
187     'fields',
188     'Set fields to show, split by comma, "/" for parent and children.' +
189     ' Like "name,parent/children". This value will cover "exculde_fields".',
190     _in='query')
191 @api_ims_inventory_v1.param(
192     'exclude_fields',
193     'Set fields to exclude showing, split by comma, "/" for parent and ' +
194     'children. Like "name,parent/children". This value will cover ' +
195     '"exclude_default".',
196     _in='query')
197 @api_ims_inventory_v1.param(
198     'exclude_default',
199     'Exclude showing all default fields, Set "true" to enable.',
200     _in='query')
201 @api_ims_inventory_v1.param(
202     'filter',
203     'Filter of the query.',
204     _in='query')
205 class ResourcePoolsListRouter(Resource):
206
207     model = ResourcePoolDTO.resource_pool_get
208
209     @api_ims_inventory_v1.marshal_list_with(model)
210     def get(self):
211         parser = reqparse.RequestParser()
212         parser.add_argument(PAGE_PARAM, location='args')
213         parser.add_argument('filter', location='args')
214         args = parser.parse_args()
215         kwargs = {}
216         if args.nextpage_opaque_marker is not None:
217             kwargs['page'] = args.nextpage_opaque_marker
218         kwargs['filter'] = args.filter if args.filter is not None else ''
219
220         ret = ocloud_view.resource_pools(bus.uow, **kwargs)
221         return link_header(request.full_path, ret)
222
223
224 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
225 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
226 @api_ims_inventory_v1.response(404, 'Resource pool not found')
227 @api_ims_inventory_v1.param(
228     'all_fields',
229     'Set any value for show all fields. This value will cover "fields" ' +
230     'and "all_fields".',
231     _in='query')
232 @api_ims_inventory_v1.param(
233     'fields',
234     'Set fields to show, split by comma, "/" for parent and children.' +
235     ' Like "name,parent/children". This value will cover "exculde_fields".',
236     _in='query')
237 @api_ims_inventory_v1.param(
238     'exclude_fields',
239     'Set fields to exclude showing, split by comma, "/" for parent and ' +
240     'children. Like "name,parent/children". This value will cover ' +
241     '"exclude_default".',
242     _in='query')
243 @api_ims_inventory_v1.param(
244     'exclude_default',
245     'Exclude showing all default fields, Set "true" to enable.',
246     _in='query')
247 class ResourcePoolGetRouter(Resource):
248
249     model = ResourcePoolDTO.resource_pool_get
250
251     @api_ims_inventory_v1.doc('Get resource pool')
252     @api_ims_inventory_v1.marshal_with(model)
253     def get(self, resourcePoolID):
254         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
255         if result is not None:
256             return result
257         raise NotFoundException("Resource pool {} doesn't exist".format(
258             resourcePoolID))
259
260
261 # ----------  Resources ---------- #
262 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
263 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
264 # @api_ims_inventory_v1.param('sort', 'sort by column name',
265 #                             _in='query')
266 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
267 #                             '(max 100). Default: 30',
268 #                             _in='query', default=30)
269 @api_ims_inventory_v1.param(PAGE_PARAM,
270                             'Page number of the results to fetch.' +
271                             ' Default: 1',
272                             _in='query', default=1)
273 @api_ims_inventory_v1.param(
274     'all_fields',
275     'Set any value for show all fields. This value will cover "fields" ' +
276     'and "all_fields".',
277     _in='query')
278 @api_ims_inventory_v1.param(
279     'fields',
280     'Set fields to show, split by comma, "/" for parent and children.' +
281     ' Like "name,parent/children". This value will cover "exculde_fields".',
282     _in='query')
283 @api_ims_inventory_v1.param(
284     'exclude_fields',
285     'Set fields to exclude showing, split by comma, "/" for parent and ' +
286     'children. Like "name,parent/children". This value will cover ' +
287     '"exclude_default".',
288     _in='query')
289 @api_ims_inventory_v1.param(
290     'exclude_default',
291     'Exclude showing all default fields, Set "true" to enable.',
292     _in='query')
293 @api_ims_inventory_v1.param(
294     'filter',
295     'Filter of the query.',
296     _in='query')
297 class ResourcesListRouter(Resource):
298
299     model = ResourceDTO.resource_list
300
301     @api_ims_inventory_v1.marshal_list_with(model)
302     def get(self, resourcePoolID):
303         parser = reqparse.RequestParser()
304         parser.add_argument(PAGE_PARAM, location='args')
305         parser.add_argument('filter', location='args')
306         args = parser.parse_args()
307         kwargs = {}
308         # if args.per_page is not None:
309         #     kwargs['per_page'] = args.per_page
310         #     base_url = base_url + 'per_page=' + args.per_page + '&'
311         if args.nextpage_opaque_marker is not None:
312             kwargs['page'] = args.nextpage_opaque_marker
313         kwargs['filter'] = args.filter if args.filter is not None else ''
314
315         ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
316         return link_header(request.full_path, ret)
317
318
319 @api_ims_inventory_v1.route(
320     "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
321 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
322 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
323 @api_ims_inventory_v1.response(404, 'Resource not found')
324 @api_ims_inventory_v1.param(
325     'all_fields',
326     'Set any value for show all fields. This value will cover "fields" ' +
327     'and "all_fields".',
328     _in='query')
329 @api_ims_inventory_v1.param(
330     'fields',
331     'Set fields to show, split by comma, "/" for parent and children.' +
332     ' Like "name,parent/children". This value will cover "exculde_fields".',
333     _in='query')
334 @api_ims_inventory_v1.param(
335     'exclude_fields',
336     'Set fields to exclude showing, split by comma, "/" for parent and ' +
337     'children. Like "name,parent/children". This value will cover ' +
338     '"exclude_default".',
339     _in='query')
340 @api_ims_inventory_v1.param(
341     'exclude_default',
342     'Exclude showing all default fields, Set "true" to enable.',
343     _in='query')
344 class ResourceGetRouter(Resource):
345
346     # dto = ResourceDTO()
347     # model = dto.get_resource_get()
348     model = ResourceDTO.recursive_resource_mapping()
349
350     @api_ims_inventory_v1.doc('Get resource')
351     @api_ims_inventory_v1.marshal_with(model)
352     def get(self, resourcePoolID, resourceID):
353         result = ocloud_view.resource_one(resourceID, bus.uow)
354         if result is not None:
355             return result
356         raise NotFoundException("Resource {} doesn't exist".format(
357             resourceID))
358
359
360 # ----------  DeploymentManagers ---------- #
361 @api_ims_inventory_v1.route("/v1/deploymentManagers")
362 @api_ims_inventory_v1.param(PAGE_PARAM,
363                             'Page number of the results to fetch.' +
364                             ' Default: 1',
365                             _in='query', default=1)
366 @api_ims_inventory_v1.param(
367     'all_fields',
368     'Set any value for show all fields. This value will cover "fields" ' +
369     'and "all_fields".',
370     _in='query')
371 @api_ims_inventory_v1.param(
372     'fields',
373     'Set fields to show, split by comma, "/" for parent and children.' +
374     ' Like "name,parent/children". This value will cover "exculde_fields".',
375     _in='query')
376 @api_ims_inventory_v1.param(
377     'exclude_fields',
378     'Set fields to exclude showing, split by comma, "/" for parent and ' +
379     'children. Like "name,parent/children". This value will cover ' +
380     '"exclude_default".',
381     _in='query')
382 @api_ims_inventory_v1.param(
383     'exclude_default',
384     'Exclude showing all default fields, Set "true" to enable.',
385     _in='query')
386 @api_ims_inventory_v1.param(
387     'filter',
388     'Filter of the query.',
389     _in='query')
390 class DeploymentManagersListRouter(Resource):
391
392     model = DeploymentManagerDTO.deployment_manager_list
393
394     @api_ims_inventory_v1.marshal_list_with(model)
395     def get(self):
396         parser = reqparse.RequestParser()
397         parser.add_argument(PAGE_PARAM, location='args')
398         parser.add_argument('filter', location='args')
399         args = parser.parse_args()
400         kwargs = {}
401         if args.nextpage_opaque_marker is not None:
402             kwargs['page'] = args.nextpage_opaque_marker
403         kwargs['filter'] = args.filter if args.filter is not None else ''
404
405         ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
406         return link_header(request.full_path, ret)
407
408
409 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
410 @api_ims_inventory_v1.param('deploymentManagerID',
411                             'ID of the deployment manager')
412 @api_ims_inventory_v1.param(
413     'profile', 'DMS profile: value supports "native_k8sapi"',
414     _in='query')
415 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
416 @api_ims_inventory_v1.param(
417     'all_fields',
418     'Set any value for show all fields. This value will cover "fields" ' +
419     'and "all_fields".',
420     _in='query')
421 @api_ims_inventory_v1.param(
422     'fields',
423     'Set fields to show, split by comma, "/" for parent and children.' +
424     ' Like "name,parent/children". This value will cover "exculde_fields".',
425     _in='query')
426 @api_ims_inventory_v1.param(
427     'exclude_fields',
428     'Set fields to exclude showing, split by comma, "/" for parent and ' +
429     'children. Like "name,parent/children". This value will cover ' +
430     '"exclude_default".',
431     _in='query')
432 @api_ims_inventory_v1.param(
433     'exclude_default',
434     'Exclude showing all default fields, Set "true" to enable.',
435     _in='query')
436 class DeploymentManagerGetRouter(Resource):
437
438     model = DeploymentManagerDTO.deployment_manager_get
439
440     @api_ims_inventory_v1.doc('Get deployment manager')
441     @api_ims_inventory_v1.marshal_with(model)
442     def get(self, deploymentManagerID):
443         parser = reqparse.RequestParser()
444         parser.add_argument('profile', location='args')
445         args = parser.parse_args()
446         profile = (
447             args.profile if args.profile is not None and args.profile != ''
448             else ocloud.DeploymentManagerProfileDefault)
449         result = ocloud_view.deployment_manager_one(
450             deploymentManagerID, bus.uow, profile)
451         if result is not None and result != "":
452             return result
453         elif result == "":
454             raise NotFoundException(
455                 "Profile {} doesn't support".format(
456                     args.profile))
457
458         raise NotFoundException("Deployment manager {} doesn't exist".format(
459             deploymentManagerID))
460
461
462 # ----------  Subscriptions ---------- #
463 @api_ims_inventory_v1.route("/v1/subscriptions")
464 class SubscriptionsListRouter(Resource):
465
466     model = SubscriptionDTO.subscription_get
467     expect = SubscriptionDTO.subscription_create
468
469     @api_ims_inventory_v1.doc('List subscriptions')
470     @api_ims_inventory_v1.marshal_list_with(model)
471     @api_ims_inventory_v1.param(
472         PAGE_PARAM,
473         'Page number of the results to fetch. Default: 1',
474         _in='query', default=1)
475     @api_ims_inventory_v1.param(
476         'all_fields',
477         'Set any value for show all fields. This value will cover "fields" ' +
478         'and "all_fields".',
479         _in='query')
480     @api_ims_inventory_v1.param(
481         'fields',
482         'Set fields to show, split by comma, "/" for parent and children.' +
483         ' Like "name,parent/children". This value will cover' +
484         ' "exculde_fields".',
485         _in='query')
486     @api_ims_inventory_v1.param(
487         'exclude_fields',
488         'Set fields to exclude showing, split by comma, "/" for parent and ' +
489         'children. Like "name,parent/children". This value will cover ' +
490         '"exclude_default".',
491         _in='query')
492     @api_ims_inventory_v1.param(
493         'exclude_default',
494         'Exclude showing all default fields, Set "true" to enable.',
495         _in='query')
496     @api_ims_inventory_v1.param(
497         'filter',
498         'Filter of the query.',
499         _in='query')
500     def get(self):
501         parser = reqparse.RequestParser()
502         parser.add_argument(PAGE_PARAM, location='args')
503         parser.add_argument('filter', location='args')
504         args = parser.parse_args()
505         kwargs = {}
506         if args.nextpage_opaque_marker is not None:
507             kwargs['page'] = args.nextpage_opaque_marker
508         kwargs['filter'] = args.filter if args.filter is not None else ''
509
510         ret = ocloud_view.subscriptions(bus.uow, **kwargs)
511         return link_header(request.full_path, ret)
512
513     @api_ims_inventory_v1.doc('Create a subscription')
514     @api_ims_inventory_v1.expect(expect)
515     @api_ims_inventory_v1.marshal_with(
516         model, code=201,
517         mask='{subscriptionId,callback,consumerSubscriptionId,filter}')
518     def post(self):
519         data = api_ims_inventory_v1.payload
520         result = ocloud_view.subscription_create(data, bus.uow)
521         return result, 201
522
523
524 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
525 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
526 @api_ims_inventory_v1.response(404, 'Subscription not found')
527 class SubscriptionGetDelRouter(Resource):
528
529     model = SubscriptionDTO.subscription_get
530
531     @api_ims_inventory_v1.doc('Get subscription by ID')
532     @api_ims_inventory_v1.marshal_with(model)
533     @api_ims_inventory_v1.param(
534         'all_fields',
535         'Set any value for show all fields. This value will cover "fields" ' +
536         'and "all_fields".',
537         _in='query')
538     @api_ims_inventory_v1.param(
539         'fields',
540         'Set fields to show, split by comma, "/" for parent and children.' +
541         ' Like "name,parent/children". This value will cover' +
542         ' "exculde_fields".',
543         _in='query')
544     @api_ims_inventory_v1.param(
545         'exclude_fields',
546         'Set fields to exclude showing, split by comma, "/" for parent and ' +
547         'children. Like "name,parent/children". This value will cover ' +
548         '"exclude_default".',
549         _in='query')
550     @api_ims_inventory_v1.param(
551         'exclude_default',
552         'Exclude showing all default fields, Set "true" to enable.',
553         _in='query')
554     def get(self, subscriptionID):
555         result = ocloud_view.subscription_one(
556             subscriptionID, bus.uow)
557         if result is not None:
558             return result
559         raise NotFoundException("Subscription {} doesn't exist".format(
560             subscriptionID))
561
562     @api_ims_inventory_v1.doc('Delete subscription by ID')
563     @api_ims_inventory_v1.response(200, 'Subscription deleted')
564     def delete(self, subscriptionID):
565         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
566         return result, 200