1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from flask import request
16 from flask_restx import Resource, reqparse
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, \
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
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 def configure_api_route():
33 # Set global bus for resource
35 bus = MessageBus.get_instance()
38 # ---------- API versions ---------- #
39 @api_ims_inventory_v1.route("/v1/api_versions")
40 class VersionRouter(Resource):
41 @api_ims_inventory_v1.doc('Get Inventory API version')
44 'uriPrefix': request.base_url.rsplit('/', 1)[0],
47 # 'isDeprecated': 'False',
48 # 'retirementDate': ''
53 # ---------- OClouds ---------- #
54 @api_ims_inventory_v1.route(*["/v1", "/v1/"])
55 @api_ims_inventory_v1.response(404, 'oCloud not found')
56 @api_ims_inventory_v1.param(
58 'Set any value for show all fields. This value will cover "fields" ' +
61 @api_ims_inventory_v1.param(
63 'Set fields to show, split by comma, "/" for parent and children.' +
64 ' Like "name,parent/children". This value will cover "exculde_fields".',
66 @api_ims_inventory_v1.param(
68 'Set fields to exclude showing, split by comma, "/" for parent and ' +
69 'children. Like "name,parent/children". This value will cover ' +
72 @api_ims_inventory_v1.param(
74 'Exclude showing all default fields, Set "true" to enable.',
76 class OcloudsListRouter(Resource):
77 """Ocloud get endpoint
78 O2 interface ocloud endpoint
81 ocloud_get = OcloudDTO.ocloud
83 @api_ims_inventory_v1.doc('Get Ocloud Information')
84 @api_ims_inventory_v1.marshal_with(ocloud_get)
86 res = ocloud_view.oclouds(bus.uow)
89 raise NotFoundException("oCloud doesn't exist")
92 # ---------- ResourceTypes ---------- #
93 @api_ims_inventory_v1.route("/v1/resourceTypes")
94 @api_ims_inventory_v1.param(PAGE_PARAM,
95 'Page number of the results to fetch.' +
97 _in='query', default=1)
98 @api_ims_inventory_v1.param(
100 'Set any value for show all fields. This value will cover "fields" ' +
103 @api_ims_inventory_v1.param(
105 'Set fields to show, split by comma, "/" for parent and children.' +
106 ' Like "name,parent/children". This value will cover "exculde_fields".',
108 @api_ims_inventory_v1.param(
110 'Set fields to exclude showing, split by comma, "/" for parent and ' +
111 'children. Like "name,parent/children". This value will cover ' +
112 '"exclude_default".',
114 @api_ims_inventory_v1.param(
116 'Exclude showing all default fields, Set "true" to enable.',
118 @api_ims_inventory_v1.param(
120 'Filter of the query.',
122 class ResourceTypesListRouter(Resource):
124 model = ResourceTypeDTO.resource_type_get
126 @api_ims_inventory_v1.doc('Get Resource Type List')
127 @api_ims_inventory_v1.marshal_list_with(model)
129 parser = reqparse.RequestParser()
130 parser.add_argument(PAGE_PARAM, location='args')
131 parser.add_argument('filter', location='args')
132 args = parser.parse_args()
134 if args.nextpage_opaque_marker is not None:
135 kwargs['page'] = args.nextpage_opaque_marker
136 kwargs['filter'] = args.filter if args.filter is not None else ''
138 ret = ocloud_view.resource_types(bus.uow, **kwargs)
139 return link_header(request.full_path, ret)
142 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
143 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
144 @api_ims_inventory_v1.response(404, 'Resource type not found')
145 @api_ims_inventory_v1.param(
147 'Set any value for show all fields. This value will cover "fields" ' +
150 @api_ims_inventory_v1.param(
152 'Set fields to show, split by comma, "/" for parent and children.' +
153 ' Like "name,parent/children". This value will cover "exculde_fields".',
155 @api_ims_inventory_v1.param(
157 'Set fields to exclude showing, split by comma, "/" for parent and ' +
158 'children. Like "name,parent/children". This value will cover ' +
159 '"exclude_default".',
161 @api_ims_inventory_v1.param(
163 'Exclude showing all default fields, Set "true" to enable.',
165 class ResourceTypeGetRouter(Resource):
167 model = ResourceTypeDTO.resource_type_get
169 @api_ims_inventory_v1.doc('Get Resource Type Information')
170 @api_ims_inventory_v1.marshal_with(model)
171 def get(self, resourceTypeID):
172 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
174 raise NotFoundException("Resource type {} doesn't exist".format(
179 # ---------- ResourcePools ---------- #
180 @api_ims_inventory_v1.route("/v1/resourcePools")
181 @api_ims_inventory_v1.param(PAGE_PARAM,
182 'Page number of the results to fetch.' +
184 _in='query', default=1)
185 @api_ims_inventory_v1.param(
187 'Set any value for show all fields. This value will cover "fields" ' +
190 @api_ims_inventory_v1.param(
192 'Set fields to show, split by comma, "/" for parent and children.' +
193 ' Like "name,parent/children". This value will cover "exculde_fields".',
195 @api_ims_inventory_v1.param(
197 'Set fields to exclude showing, split by comma, "/" for parent and ' +
198 'children. Like "name,parent/children". This value will cover ' +
199 '"exclude_default".',
201 @api_ims_inventory_v1.param(
203 'Exclude showing all default fields, Set "true" to enable.',
205 @api_ims_inventory_v1.param(
207 'Filter of the query.',
209 class ResourcePoolsListRouter(Resource):
211 model = ResourcePoolDTO.resource_pool_get
213 @api_ims_inventory_v1.doc('Get Resource Pool List')
214 @api_ims_inventory_v1.marshal_list_with(model)
216 parser = reqparse.RequestParser()
217 parser.add_argument(PAGE_PARAM, location='args')
218 parser.add_argument('filter', location='args')
219 args = parser.parse_args()
221 if args.nextpage_opaque_marker is not None:
222 kwargs['page'] = args.nextpage_opaque_marker
223 kwargs['filter'] = args.filter if args.filter is not None else ''
225 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
226 return link_header(request.full_path, ret)
229 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
230 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
231 @api_ims_inventory_v1.response(404, 'Resource pool not found')
232 @api_ims_inventory_v1.param(
234 'Set any value for show all fields. This value will cover "fields" ' +
237 @api_ims_inventory_v1.param(
239 'Set fields to show, split by comma, "/" for parent and children.' +
240 ' Like "name,parent/children". This value will cover "exculde_fields".',
242 @api_ims_inventory_v1.param(
244 'Set fields to exclude showing, split by comma, "/" for parent and ' +
245 'children. Like "name,parent/children". This value will cover ' +
246 '"exclude_default".',
248 @api_ims_inventory_v1.param(
250 'Exclude showing all default fields, Set "true" to enable.',
252 class ResourcePoolGetRouter(Resource):
254 model = ResourcePoolDTO.resource_pool_get
256 @api_ims_inventory_v1.doc('Get Resource Pool Information')
257 @api_ims_inventory_v1.marshal_with(model)
258 def get(self, resourcePoolID):
259 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
260 if result is not None:
262 raise NotFoundException("Resource pool {} doesn't exist".format(
266 # ---------- Resources ---------- #
267 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
268 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
269 @api_ims_inventory_v1.response(404, 'Resource pool not found')
270 # @api_ims_inventory_v1.param('sort', 'sort by column name',
272 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
273 # '(max 100). Default: 30',
274 # _in='query', default=30)
275 @api_ims_inventory_v1.param(PAGE_PARAM,
276 'Page number of the results to fetch.' +
278 _in='query', default=1)
279 @api_ims_inventory_v1.param(
281 'Set any value for show all fields. This value will cover "fields" ' +
284 @api_ims_inventory_v1.param(
286 'Set fields to show, split by comma, "/" for parent and children.' +
287 ' Like "name,parent/children". This value will cover "exculde_fields".',
289 @api_ims_inventory_v1.param(
291 'Set fields to exclude showing, split by comma, "/" for parent and ' +
292 'children. Like "name,parent/children". This value will cover ' +
293 '"exclude_default".',
295 @api_ims_inventory_v1.param(
297 'Exclude showing all default fields, Set "true" to enable.',
299 @api_ims_inventory_v1.param(
301 'Filter of the query.',
303 class ResourcesListRouter(Resource):
305 model = ResourceDTO.resource_list
307 @api_ims_inventory_v1.doc('Get Resource List')
308 @api_ims_inventory_v1.marshal_list_with(model)
309 def get(self, resourcePoolID):
310 parser = reqparse.RequestParser()
311 parser.add_argument(PAGE_PARAM, location='args')
312 parser.add_argument('filter', location='args')
313 args = parser.parse_args()
315 # if args.per_page is not None:
316 # kwargs['per_page'] = args.per_page
317 # base_url = base_url + 'per_page=' + args.per_page + '&'
318 if args.nextpage_opaque_marker is not None:
319 kwargs['page'] = args.nextpage_opaque_marker
320 kwargs['filter'] = args.filter if args.filter is not None else ''
321 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
323 raise NotFoundException("Resources under {} doesn't exist".format(
325 return link_header(request.full_path, ret)
328 @api_ims_inventory_v1.route(
329 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
330 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
331 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
332 @api_ims_inventory_v1.response(404, 'Resource not found')
333 @api_ims_inventory_v1.param(
335 'Set any value for show all fields. This value will cover "fields" ' +
338 @api_ims_inventory_v1.param(
340 'Set fields to show, split by comma, "/" for parent and children.' +
341 ' Like "name,parent/children". This value will cover "exculde_fields".',
343 @api_ims_inventory_v1.param(
345 'Set fields to exclude showing, split by comma, "/" for parent and ' +
346 'children. Like "name,parent/children". This value will cover ' +
347 '"exclude_default".',
349 @api_ims_inventory_v1.param(
351 'Exclude showing all default fields, Set "true" to enable.',
353 class ResourceGetRouter(Resource):
355 # dto = ResourceDTO()
356 # model = dto.get_resource_get()
357 model = ResourceDTO.recursive_resource_mapping()
359 @api_ims_inventory_v1.doc('Get Resource Information')
360 @api_ims_inventory_v1.marshal_with(model)
361 def get(self, resourcePoolID, resourceID):
362 result = ocloud_view.resource_one(resourceID, bus.uow, resourcePoolID)
364 raise NotFoundException("Resource {} doesn't exist".format(
369 # ---------- DeploymentManagers ---------- #
370 @api_ims_inventory_v1.route("/v1/deploymentManagers")
371 @api_ims_inventory_v1.param(PAGE_PARAM,
372 'Page number of the results to fetch.' +
374 _in='query', default=1)
375 @api_ims_inventory_v1.param(
377 'Set any value for show all fields. This value will cover "fields" ' +
380 @api_ims_inventory_v1.param(
382 'Set fields to show, split by comma, "/" for parent and children.' +
383 ' Like "name,parent/children". This value will cover "exculde_fields".',
385 @api_ims_inventory_v1.param(
387 'Set fields to exclude showing, split by comma, "/" for parent and ' +
388 'children. Like "name,parent/children". This value will cover ' +
389 '"exclude_default".',
391 @api_ims_inventory_v1.param(
393 'Exclude showing all default fields, Set "true" to enable.',
395 @api_ims_inventory_v1.param(
397 'Filter of the query.',
399 class DeploymentManagersListRouter(Resource):
401 model = DeploymentManagerDTO.deployment_manager_list
403 @api_ims_inventory_v1.doc('Get Deployment Manager List')
404 @api_ims_inventory_v1.marshal_list_with(model)
406 parser = reqparse.RequestParser()
407 parser.add_argument(PAGE_PARAM, location='args')
408 parser.add_argument('filter', location='args')
409 args = parser.parse_args()
411 if args.nextpage_opaque_marker is not None:
412 kwargs['page'] = args.nextpage_opaque_marker
413 kwargs['filter'] = args.filter if args.filter is not None else ''
415 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
416 return link_header(request.full_path, ret)
419 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
420 @api_ims_inventory_v1.param('deploymentManagerID',
421 'ID of the deployment manager')
422 @api_ims_inventory_v1.param(
423 'profile', 'DMS profile: value supports "native_k8sapi"',
425 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
426 @api_ims_inventory_v1.param(
428 'Set any value for show all fields. This value will cover "fields" ' +
431 @api_ims_inventory_v1.param(
433 'Set fields to show, split by comma, "/" for parent and children.' +
434 ' Like "name,parent/children". This value will cover "exculde_fields".',
436 @api_ims_inventory_v1.param(
438 'Set fields to exclude showing, split by comma, "/" for parent and ' +
439 'children. Like "name,parent/children". This value will cover ' +
440 '"exclude_default".',
442 @api_ims_inventory_v1.param(
444 'Exclude showing all default fields, Set "true" to enable.',
446 class DeploymentManagerGetRouter(Resource):
448 model = DeploymentManagerDTO.deployment_manager_get
450 @api_ims_inventory_v1.doc('Get Deployment Manager Information')
451 @api_ims_inventory_v1.marshal_with(model)
452 def get(self, deploymentManagerID):
453 parser = reqparse.RequestParser()
454 parser.add_argument('profile', location='args')
455 args = parser.parse_args()
457 args.profile if args.profile is not None and args.profile != ''
458 else ocloud.DeploymentManagerProfileDefault)
459 result = ocloud_view.deployment_manager_one(
460 deploymentManagerID, bus.uow, profile)
461 if result is not None and result != "":
464 raise NotFoundException(
465 "Profile {} doesn't support".format(
468 raise NotFoundException("Deployment manager {} doesn't exist".format(
469 deploymentManagerID))
472 # ---------- Subscriptions ---------- #
473 @api_ims_inventory_v1.route("/v1/subscriptions")
474 class SubscriptionsListRouter(Resource):
476 model = SubscriptionDTO.subscription_get
477 expect = SubscriptionDTO.subscription_create
479 @api_ims_inventory_v1.doc('Get Subscription List')
480 @api_ims_inventory_v1.marshal_list_with(model)
481 @api_ims_inventory_v1.param(
483 'Page number of the results to fetch. Default: 1',
484 _in='query', default=1)
485 @api_ims_inventory_v1.param(
487 'Set any value for show all fields. This value will cover "fields" ' +
490 @api_ims_inventory_v1.param(
492 'Set fields to show, split by comma, "/" for parent and children.' +
493 ' Like "name,parent/children". This value will cover' +
494 ' "exculde_fields".',
496 @api_ims_inventory_v1.param(
498 'Set fields to exclude showing, split by comma, "/" for parent and ' +
499 'children. Like "name,parent/children". This value will cover ' +
500 '"exclude_default".',
502 @api_ims_inventory_v1.param(
504 'Exclude showing all default fields, Set "true" to enable.',
506 @api_ims_inventory_v1.param(
508 'Filter of the query.',
511 parser = reqparse.RequestParser()
512 parser.add_argument(PAGE_PARAM, location='args')
513 parser.add_argument('filter', location='args')
514 args = parser.parse_args()
516 if args.nextpage_opaque_marker is not None:
517 kwargs['page'] = args.nextpage_opaque_marker
518 kwargs['filter'] = args.filter if args.filter is not None else ''
520 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
521 return link_header(request.full_path, ret)
523 @api_ims_inventory_v1.doc('Create a Subscription')
524 @api_ims_inventory_v1.expect(expect)
525 @api_ims_inventory_v1.marshal_with(
527 mask='{subscriptionId,callback,consumerSubscriptionId,filter}')
529 data = api_ims_inventory_v1.payload
530 callback = data.get('callback', None)
532 raise BadRequestException('The callback parameter is required')
534 result = ocloud_view.subscription_create(data, bus.uow)
538 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
539 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
540 @api_ims_inventory_v1.response(404, 'Subscription not found')
541 class SubscriptionGetDelRouter(Resource):
543 model = SubscriptionDTO.subscription_get
545 @api_ims_inventory_v1.doc('Get Subscription Information')
546 @api_ims_inventory_v1.marshal_with(model)
547 @api_ims_inventory_v1.param(
549 'Set any value for show all fields. This value will cover "fields" ' +
552 @api_ims_inventory_v1.param(
554 'Set fields to show, split by comma, "/" for parent and children.' +
555 ' Like "name,parent/children". This value will cover' +
556 ' "exculde_fields".',
558 @api_ims_inventory_v1.param(
560 'Set fields to exclude showing, split by comma, "/" for parent and ' +
561 'children. Like "name,parent/children". This value will cover ' +
562 '"exclude_default".',
564 @api_ims_inventory_v1.param(
566 'Exclude showing all default fields, Set "true" to enable.',
568 def get(self, subscriptionID):
569 result = ocloud_view.subscription_one(
570 subscriptionID, bus.uow)
571 if result is not None:
573 raise NotFoundException("Subscription {} doesn't exist".format(
576 @api_ims_inventory_v1.doc('Delete a Subscription')
577 @api_ims_inventory_v1.response(200, 'Subscription deleted')
578 def delete(self, subscriptionID):
579 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)