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