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, \
29 from o2common.helper import o2logging
30 logger = o2logging.get_logger(__name__)
33 def configure_api_route():
34 # Set global bus for resource
36 bus = MessageBus.get_instance()
39 # ---------- API versions ---------- #
40 @api_ims_inventory_v1.route("/v1/api_versions")
41 class VersionRouter(Resource):
42 model = InventoryApiV1DTO.api_version_info_get
44 @api_ims_inventory_v1.doc('Get Inventory API version')
45 @api_ims_inventory_v1.marshal_with(model)
48 'uriPrefix': request.base_url.rsplit('/', 1)[0],
51 # 'isDeprecated': 'False',
52 # 'retirementDate': ''
57 # ---------- OClouds ---------- #
58 @api_ims_inventory_v1.route(*["/v1", "/v1/"])
59 @api_ims_inventory_v1.response(404, 'oCloud not found')
60 @api_ims_inventory_v1.param(
62 'Set any value for show all fields. This value will cover "fields" ' +
65 @api_ims_inventory_v1.param(
67 'Set fields to show, split by comma, "/" for parent and children.' +
68 ' Like "name,parent/children". This value will cover "exculde_fields".',
70 @api_ims_inventory_v1.param(
72 'Set fields to exclude showing, split by comma, "/" for parent and ' +
73 'children. Like "name,parent/children". This value will cover ' +
76 @api_ims_inventory_v1.param(
78 'Exclude showing all default fields, Set "true" to enable.',
80 class OcloudsListRouter(Resource):
81 """Ocloud get endpoint
82 O2 interface ocloud endpoint
85 ocloud_get = OcloudDTO.ocloud
87 @api_ims_inventory_v1.doc('Get Ocloud Information')
88 @api_ims_inventory_v1.marshal_with(ocloud_get)
90 res = ocloud_view.oclouds(bus.uow)
93 raise NotFoundException("oCloud doesn't exist")
96 # ---------- ResourceTypes ---------- #
97 @api_ims_inventory_v1.route("/v1/resourceTypes")
98 @api_ims_inventory_v1.param(PAGE_PARAM,
99 'Page number of the results to fetch.' +
101 _in='query', default=1)
102 @api_ims_inventory_v1.param(
104 'Set any value for show all fields. This value will cover "fields" ' +
107 @api_ims_inventory_v1.param(
109 'Set fields to show, split by comma, "/" for parent and children.' +
110 ' Like "name,parent/children". This value will cover "exculde_fields".',
112 @api_ims_inventory_v1.param(
114 'Set fields to exclude showing, split by comma, "/" for parent and ' +
115 'children. Like "name,parent/children". This value will cover ' +
116 '"exclude_default".',
118 @api_ims_inventory_v1.param(
120 'Exclude showing all default fields, Set "true" to enable.',
122 @api_ims_inventory_v1.param(
124 'Filter of the query.',
126 class ResourceTypesListRouter(Resource):
128 model = ResourceTypeDTO.resource_type_get
130 @api_ims_inventory_v1.doc('Get Resource Type List')
131 @api_ims_inventory_v1.marshal_list_with(model)
133 parser = reqparse.RequestParser()
134 parser.add_argument(PAGE_PARAM, location='args')
135 parser.add_argument('filter', location='args')
136 args = parser.parse_args()
138 if args.nextpage_opaque_marker is not None:
139 kwargs['page'] = args.nextpage_opaque_marker
140 kwargs['filter'] = args.filter if args.filter is not None else ''
142 ret = ocloud_view.resource_types(bus.uow, **kwargs)
143 return link_header(request.full_path, ret)
146 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
147 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
148 @api_ims_inventory_v1.response(404, 'Resource type not found')
149 @api_ims_inventory_v1.param(
151 'Set any value for show all fields. This value will cover "fields" ' +
154 @api_ims_inventory_v1.param(
156 'Set fields to show, split by comma, "/" for parent and children.' +
157 ' Like "name,parent/children". This value will cover "exculde_fields".',
159 @api_ims_inventory_v1.param(
161 'Set fields to exclude showing, split by comma, "/" for parent and ' +
162 'children. Like "name,parent/children". This value will cover ' +
163 '"exclude_default".',
165 @api_ims_inventory_v1.param(
167 'Exclude showing all default fields, Set "true" to enable.',
169 class ResourceTypeGetRouter(Resource):
171 model = ResourceTypeDTO.resource_type_get
173 @api_ims_inventory_v1.doc('Get Resource Type Information')
174 @api_ims_inventory_v1.marshal_with(model)
175 def get(self, resourceTypeID):
176 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
178 raise NotFoundException("Resource type {} doesn't exist".format(
183 # ---------- ResourcePools ---------- #
184 @api_ims_inventory_v1.route("/v1/resourcePools")
185 @api_ims_inventory_v1.param(PAGE_PARAM,
186 'Page number of the results to fetch.' +
188 _in='query', default=1)
189 @api_ims_inventory_v1.param(
191 'Set any value for show all fields. This value will cover "fields" ' +
194 @api_ims_inventory_v1.param(
196 'Set fields to show, split by comma, "/" for parent and children.' +
197 ' Like "name,parent/children". This value will cover "exculde_fields".',
199 @api_ims_inventory_v1.param(
201 'Set fields to exclude showing, split by comma, "/" for parent and ' +
202 'children. Like "name,parent/children". This value will cover ' +
203 '"exclude_default".',
205 @api_ims_inventory_v1.param(
207 'Exclude showing all default fields, Set "true" to enable.',
209 @api_ims_inventory_v1.param(
211 'Filter of the query.',
213 class ResourcePoolsListRouter(Resource):
215 model = ResourcePoolDTO.resource_pool_get
217 @api_ims_inventory_v1.doc('Get Resource Pool List')
218 @api_ims_inventory_v1.marshal_list_with(model)
220 parser = reqparse.RequestParser()
221 parser.add_argument(PAGE_PARAM, location='args')
222 parser.add_argument('filter', location='args')
223 args = parser.parse_args()
225 if args.nextpage_opaque_marker is not None:
226 kwargs['page'] = args.nextpage_opaque_marker
227 kwargs['filter'] = args.filter if args.filter is not None else ''
229 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
230 return link_header(request.full_path, ret)
233 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
234 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
235 @api_ims_inventory_v1.response(404, 'Resource pool not found')
236 @api_ims_inventory_v1.param(
238 'Set any value for show all fields. This value will cover "fields" ' +
241 @api_ims_inventory_v1.param(
243 'Set fields to show, split by comma, "/" for parent and children.' +
244 ' Like "name,parent/children". This value will cover "exculde_fields".',
246 @api_ims_inventory_v1.param(
248 'Set fields to exclude showing, split by comma, "/" for parent and ' +
249 'children. Like "name,parent/children". This value will cover ' +
250 '"exclude_default".',
252 @api_ims_inventory_v1.param(
254 'Exclude showing all default fields, Set "true" to enable.',
256 class ResourcePoolGetRouter(Resource):
258 model = ResourcePoolDTO.resource_pool_get
260 @api_ims_inventory_v1.doc('Get Resource Pool Information')
261 @api_ims_inventory_v1.marshal_with(model)
262 def get(self, resourcePoolID):
263 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
264 if result is not None:
266 raise NotFoundException("Resource pool {} doesn't exist".format(
270 # ---------- Resources ---------- #
271 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
272 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
273 @api_ims_inventory_v1.response(404, 'Resource pool not found')
274 # @api_ims_inventory_v1.param('sort', 'sort by column name',
276 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
277 # '(max 100). Default: 30',
278 # _in='query', default=30)
279 @api_ims_inventory_v1.param(PAGE_PARAM,
280 'Page number of the results to fetch.' +
282 _in='query', default=1)
283 @api_ims_inventory_v1.param(
285 'Set any value for show all fields. This value will cover "fields" ' +
288 @api_ims_inventory_v1.param(
290 'Set fields to show, split by comma, "/" for parent and children.' +
291 ' Like "name,parent/children". This value will cover "exculde_fields".',
293 @api_ims_inventory_v1.param(
295 'Set fields to exclude showing, split by comma, "/" for parent and ' +
296 'children. Like "name,parent/children". This value will cover ' +
297 '"exclude_default".',
299 @api_ims_inventory_v1.param(
301 'Exclude showing all default fields, Set "true" to enable.',
303 @api_ims_inventory_v1.param(
305 'Filter of the query.',
307 class ResourcesListRouter(Resource):
309 model = ResourceDTO.resource_list
311 @api_ims_inventory_v1.doc('Get Resource List')
312 @api_ims_inventory_v1.marshal_list_with(model)
313 def get(self, resourcePoolID):
314 parser = reqparse.RequestParser()
315 parser.add_argument(PAGE_PARAM, location='args')
316 parser.add_argument('filter', location='args')
317 args = parser.parse_args()
319 # if args.per_page is not None:
320 # kwargs['per_page'] = args.per_page
321 # base_url = base_url + 'per_page=' + args.per_page + '&'
322 if args.nextpage_opaque_marker is not None:
323 kwargs['page'] = args.nextpage_opaque_marker
324 kwargs['filter'] = args.filter if args.filter is not None else ''
325 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
327 raise NotFoundException("Resources under {} doesn't exist".format(
329 return link_header(request.full_path, ret)
332 @api_ims_inventory_v1.route(
333 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
334 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
335 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
336 @api_ims_inventory_v1.response(404, 'Resource not found')
337 @api_ims_inventory_v1.param(
339 'Set any value for show all fields. This value will cover "fields" ' +
342 @api_ims_inventory_v1.param(
344 'Set fields to show, split by comma, "/" for parent and children.' +
345 ' Like "name,parent/children". This value will cover "exculde_fields".',
347 @api_ims_inventory_v1.param(
349 'Set fields to exclude showing, split by comma, "/" for parent and ' +
350 'children. Like "name,parent/children". This value will cover ' +
351 '"exclude_default".',
353 @api_ims_inventory_v1.param(
355 'Exclude showing all default fields, Set "true" to enable.',
357 class ResourceGetRouter(Resource):
359 # dto = ResourceDTO()
360 # model = dto.get_resource_get()
361 model = ResourceDTO.recursive_resource_mapping()
363 @api_ims_inventory_v1.doc('Get Resource Information')
364 @api_ims_inventory_v1.marshal_with(model)
365 def get(self, resourcePoolID, resourceID):
366 result = ocloud_view.resource_one(resourceID, bus.uow, resourcePoolID)
368 raise NotFoundException("Resource {} doesn't exist".format(
373 # ---------- DeploymentManagers ---------- #
374 @api_ims_inventory_v1.route("/v1/deploymentManagers")
375 @api_ims_inventory_v1.param(PAGE_PARAM,
376 'Page number of the results to fetch.' +
378 _in='query', default=1)
379 @api_ims_inventory_v1.param(
381 'Set any value for show all fields. This value will cover "fields" ' +
384 @api_ims_inventory_v1.param(
386 'Set fields to show, split by comma, "/" for parent and children.' +
387 ' Like "name,parent/children". This value will cover "exculde_fields".',
389 @api_ims_inventory_v1.param(
391 'Set fields to exclude showing, split by comma, "/" for parent and ' +
392 'children. Like "name,parent/children". This value will cover ' +
393 '"exclude_default".',
395 @api_ims_inventory_v1.param(
397 'Exclude showing all default fields, Set "true" to enable.',
399 @api_ims_inventory_v1.param(
401 'Filter of the query.',
403 class DeploymentManagersListRouter(Resource):
405 model = DeploymentManagerDTO.deployment_manager_list
407 @api_ims_inventory_v1.doc('Get Deployment Manager List')
408 @api_ims_inventory_v1.marshal_list_with(model)
410 parser = reqparse.RequestParser()
411 parser.add_argument(PAGE_PARAM, location='args')
412 parser.add_argument('filter', location='args')
413 args = parser.parse_args()
415 if args.nextpage_opaque_marker is not None:
416 kwargs['page'] = args.nextpage_opaque_marker
417 kwargs['filter'] = args.filter if args.filter is not None else ''
419 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
420 return link_header(request.full_path, ret)
423 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
424 @api_ims_inventory_v1.param('deploymentManagerID',
425 'ID of the deployment manager')
426 @api_ims_inventory_v1.param(
427 'profile', 'DMS profile: value supports "native_k8sapi"',
429 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
430 @api_ims_inventory_v1.param(
432 'Set any value for show all fields. This value will cover "fields" ' +
435 @api_ims_inventory_v1.param(
437 'Set fields to show, split by comma, "/" for parent and children.' +
438 ' Like "name,parent/children". This value will cover "exculde_fields".',
440 @api_ims_inventory_v1.param(
442 'Set fields to exclude showing, split by comma, "/" for parent and ' +
443 'children. Like "name,parent/children". This value will cover ' +
444 '"exclude_default".',
446 @api_ims_inventory_v1.param(
448 'Exclude showing all default fields, Set "true" to enable.',
450 class DeploymentManagerGetRouter(Resource):
452 model = DeploymentManagerDTO.deployment_manager_get
454 @api_ims_inventory_v1.doc('Get Deployment Manager Information')
455 @api_ims_inventory_v1.marshal_with(model)
456 def get(self, deploymentManagerID):
457 parser = reqparse.RequestParser()
458 parser.add_argument('profile', location='args')
459 args = parser.parse_args()
461 args.profile if args.profile is not None and args.profile != ''
462 else ocloud.DeploymentManagerProfileDefault)
463 result = ocloud_view.deployment_manager_one(
464 deploymentManagerID, bus.uow, profile)
465 if result is not None and result != "":
468 raise NotFoundException(
469 "Profile {} doesn't support".format(
472 raise NotFoundException("Deployment manager {} doesn't exist".format(
473 deploymentManagerID))
476 # ---------- Subscriptions ---------- #
477 @api_ims_inventory_v1.route("/v1/subscriptions")
478 class SubscriptionsListRouter(Resource):
480 model = SubscriptionDTO.subscription_get
481 expect = SubscriptionDTO.subscription_create
483 @api_ims_inventory_v1.doc('Get Subscription List')
484 @api_ims_inventory_v1.marshal_list_with(model)
485 @api_ims_inventory_v1.param(
487 'Page number of the results to fetch. Default: 1',
488 _in='query', default=1)
489 @api_ims_inventory_v1.param(
491 'Set any value for show all fields. This value will cover "fields" ' +
494 @api_ims_inventory_v1.param(
496 'Set fields to show, split by comma, "/" for parent and children.' +
497 ' Like "name,parent/children". This value will cover' +
498 ' "exculde_fields".',
500 @api_ims_inventory_v1.param(
502 'Set fields to exclude showing, split by comma, "/" for parent and ' +
503 'children. Like "name,parent/children". This value will cover ' +
504 '"exclude_default".',
506 @api_ims_inventory_v1.param(
508 'Exclude showing all default fields, Set "true" to enable.',
510 @api_ims_inventory_v1.param(
512 'Filter of the query.',
515 parser = reqparse.RequestParser()
516 parser.add_argument(PAGE_PARAM, location='args')
517 parser.add_argument('filter', location='args')
518 args = parser.parse_args()
520 if args.nextpage_opaque_marker is not None:
521 kwargs['page'] = args.nextpage_opaque_marker
522 kwargs['filter'] = args.filter if args.filter is not None else ''
524 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
525 return link_header(request.full_path, ret)
527 @api_ims_inventory_v1.doc('Create a Subscription')
528 @api_ims_inventory_v1.expect(expect)
529 @api_ims_inventory_v1.marshal_with(
531 mask='{subscriptionId,callback,consumerSubscriptionId,filter}')
533 data = api_ims_inventory_v1.payload
534 callback = data.get('callback', None)
536 raise BadRequestException('The callback parameter is required')
538 result = ocloud_view.subscription_create(data, bus.uow)
542 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
543 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
544 @api_ims_inventory_v1.response(404, 'Subscription not found')
545 class SubscriptionGetDelRouter(Resource):
547 model = SubscriptionDTO.subscription_get
549 @api_ims_inventory_v1.doc('Get Subscription Information')
550 @api_ims_inventory_v1.marshal_with(model)
551 @api_ims_inventory_v1.param(
553 'Set any value for show all fields. This value will cover "fields" ' +
556 @api_ims_inventory_v1.param(
558 'Set fields to show, split by comma, "/" for parent and children.' +
559 ' Like "name,parent/children". This value will cover' +
560 ' "exculde_fields".',
562 @api_ims_inventory_v1.param(
564 'Set fields to exclude showing, split by comma, "/" for parent and ' +
565 'children. Like "name,parent/children". This value will cover ' +
566 '"exclude_default".',
568 @api_ims_inventory_v1.param(
570 'Exclude showing all default fields, Set "true" to enable.',
572 def get(self, subscriptionID):
573 result = ocloud_view.subscription_one(
574 subscriptionID, bus.uow)
575 if result is not None:
577 raise NotFoundException("Subscription {} doesn't exist".format(
580 @api_ims_inventory_v1.doc('Delete a Subscription')
581 @api_ims_inventory_v1.response(200, 'Subscription deleted')
582 def delete(self, subscriptionID):
583 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)