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