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):
43 'uriPrefix': request.base_url.rsplit('/', 1)[0],
46 # 'isDeprecated': 'False',
47 # 'retirementDate': ''
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(
57 'Set any value for show all fields. This value will cover "fields" ' +
60 @api_ims_inventory_v1.param(
62 'Set fields to show, split by comma, "/" for parent and children.' +
63 ' Like "name,parent/children". This value will cover "exculde_fields".',
65 @api_ims_inventory_v1.param(
67 'Set fields to exclude showing, split by comma, "/" for parent and ' +
68 'children. Like "name,parent/children". This value will cover ' +
71 @api_ims_inventory_v1.param(
73 'Exclude showing all default fields, Set "true" to enable.',
75 class OcloudsListRouter(Resource):
76 """Ocloud get endpoint
77 O2 interface ocloud endpoint
80 ocloud_get = OcloudDTO.ocloud
82 @api_ims_inventory_v1.marshal_with(ocloud_get)
84 res = ocloud_view.oclouds(bus.uow)
87 raise NotFoundException("oCloud doesn't exist")
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.' +
95 _in='query', default=1)
96 @api_ims_inventory_v1.param(
98 'Set any value for show all fields. This value will cover "fields" ' +
101 @api_ims_inventory_v1.param(
103 'Set fields to show, split by comma, "/" for parent and children.' +
104 ' Like "name,parent/children". This value will cover "exculde_fields".',
106 @api_ims_inventory_v1.param(
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".',
112 @api_ims_inventory_v1.param(
114 'Exclude showing all default fields, Set "true" to enable.',
116 @api_ims_inventory_v1.param(
118 'Filter of the query.',
120 class ResourceTypesListRouter(Resource):
122 model = ResourceTypeDTO.resource_type_get
124 @api_ims_inventory_v1.marshal_list_with(model)
126 parser = reqparse.RequestParser()
127 parser.add_argument(PAGE_PARAM, location='args')
128 parser.add_argument('filter', location='args')
129 args = parser.parse_args()
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 ''
135 ret = ocloud_view.resource_types(bus.uow, **kwargs)
136 return link_header(request.full_path, ret)
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(
144 'Set any value for show all fields. This value will cover "fields" ' +
147 @api_ims_inventory_v1.param(
149 'Set fields to show, split by comma, "/" for parent and children.' +
150 ' Like "name,parent/children". This value will cover "exculde_fields".',
152 @api_ims_inventory_v1.param(
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".',
158 @api_ims_inventory_v1.param(
160 'Exclude showing all default fields, Set "true" to enable.',
162 class ResourceTypeGetRouter(Resource):
164 model = ResourceTypeDTO.resource_type_get
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)
171 raise NotFoundException("Resource type {} doesn't exist".format(
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.' +
181 _in='query', default=1)
182 @api_ims_inventory_v1.param(
184 'Set any value for show all fields. This value will cover "fields" ' +
187 @api_ims_inventory_v1.param(
189 'Set fields to show, split by comma, "/" for parent and children.' +
190 ' Like "name,parent/children". This value will cover "exculde_fields".',
192 @api_ims_inventory_v1.param(
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".',
198 @api_ims_inventory_v1.param(
200 'Exclude showing all default fields, Set "true" to enable.',
202 @api_ims_inventory_v1.param(
204 'Filter of the query.',
206 class ResourcePoolsListRouter(Resource):
208 model = ResourcePoolDTO.resource_pool_get
210 @api_ims_inventory_v1.marshal_list_with(model)
212 parser = reqparse.RequestParser()
213 parser.add_argument(PAGE_PARAM, location='args')
214 parser.add_argument('filter', location='args')
215 args = parser.parse_args()
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 ''
221 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
222 return link_header(request.full_path, ret)
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(
230 'Set any value for show all fields. This value will cover "fields" ' +
233 @api_ims_inventory_v1.param(
235 'Set fields to show, split by comma, "/" for parent and children.' +
236 ' Like "name,parent/children". This value will cover "exculde_fields".',
238 @api_ims_inventory_v1.param(
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".',
244 @api_ims_inventory_v1.param(
246 'Exclude showing all default fields, Set "true" to enable.',
248 class ResourcePoolGetRouter(Resource):
250 model = ResourcePoolDTO.resource_pool_get
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:
258 raise NotFoundException("Resource pool {} doesn't exist".format(
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.response(404, 'Resource pool not found')
266 # @api_ims_inventory_v1.param('sort', 'sort by column name',
268 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
269 # '(max 100). Default: 30',
270 # _in='query', default=30)
271 @api_ims_inventory_v1.param(PAGE_PARAM,
272 'Page number of the results to fetch.' +
274 _in='query', default=1)
275 @api_ims_inventory_v1.param(
277 'Set any value for show all fields. This value will cover "fields" ' +
280 @api_ims_inventory_v1.param(
282 'Set fields to show, split by comma, "/" for parent and children.' +
283 ' Like "name,parent/children". This value will cover "exculde_fields".',
285 @api_ims_inventory_v1.param(
287 'Set fields to exclude showing, split by comma, "/" for parent and ' +
288 'children. Like "name,parent/children". This value will cover ' +
289 '"exclude_default".',
291 @api_ims_inventory_v1.param(
293 'Exclude showing all default fields, Set "true" to enable.',
295 @api_ims_inventory_v1.param(
297 'Filter of the query.',
299 class ResourcesListRouter(Resource):
301 model = ResourceDTO.resource_list
303 @api_ims_inventory_v1.marshal_list_with(model)
304 def get(self, resourcePoolID):
305 parser = reqparse.RequestParser()
306 parser.add_argument(PAGE_PARAM, location='args')
307 parser.add_argument('filter', location='args')
308 args = parser.parse_args()
310 # if args.per_page is not None:
311 # kwargs['per_page'] = args.per_page
312 # base_url = base_url + 'per_page=' + args.per_page + '&'
313 if args.nextpage_opaque_marker is not None:
314 kwargs['page'] = args.nextpage_opaque_marker
315 kwargs['filter'] = args.filter if args.filter is not None else ''
316 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
318 raise NotFoundException("Resources under {} doesn't exist".format(
320 return link_header(request.full_path, ret)
323 @api_ims_inventory_v1.route(
324 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
325 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
326 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
327 @api_ims_inventory_v1.response(404, 'Resource not found')
328 @api_ims_inventory_v1.param(
330 'Set any value for show all fields. This value will cover "fields" ' +
333 @api_ims_inventory_v1.param(
335 'Set fields to show, split by comma, "/" for parent and children.' +
336 ' Like "name,parent/children". This value will cover "exculde_fields".',
338 @api_ims_inventory_v1.param(
340 'Set fields to exclude showing, split by comma, "/" for parent and ' +
341 'children. Like "name,parent/children". This value will cover ' +
342 '"exclude_default".',
344 @api_ims_inventory_v1.param(
346 'Exclude showing all default fields, Set "true" to enable.',
348 class ResourceGetRouter(Resource):
350 # dto = ResourceDTO()
351 # model = dto.get_resource_get()
352 model = ResourceDTO.recursive_resource_mapping()
354 @api_ims_inventory_v1.doc('Get resource')
355 @api_ims_inventory_v1.marshal_with(model)
356 def get(self, resourcePoolID, resourceID):
357 result = ocloud_view.resource_one(resourceID, bus.uow, resourcePoolID)
359 raise NotFoundException("Resource {} doesn't exist".format(
364 # ---------- DeploymentManagers ---------- #
365 @api_ims_inventory_v1.route("/v1/deploymentManagers")
366 @api_ims_inventory_v1.param(PAGE_PARAM,
367 'Page number of the results to fetch.' +
369 _in='query', default=1)
370 @api_ims_inventory_v1.param(
372 'Set any value for show all fields. This value will cover "fields" ' +
375 @api_ims_inventory_v1.param(
377 'Set fields to show, split by comma, "/" for parent and children.' +
378 ' Like "name,parent/children". This value will cover "exculde_fields".',
380 @api_ims_inventory_v1.param(
382 'Set fields to exclude showing, split by comma, "/" for parent and ' +
383 'children. Like "name,parent/children". This value will cover ' +
384 '"exclude_default".',
386 @api_ims_inventory_v1.param(
388 'Exclude showing all default fields, Set "true" to enable.',
390 @api_ims_inventory_v1.param(
392 'Filter of the query.',
394 class DeploymentManagersListRouter(Resource):
396 model = DeploymentManagerDTO.deployment_manager_list
398 @api_ims_inventory_v1.marshal_list_with(model)
400 parser = reqparse.RequestParser()
401 parser.add_argument(PAGE_PARAM, location='args')
402 parser.add_argument('filter', location='args')
403 args = parser.parse_args()
405 if args.nextpage_opaque_marker is not None:
406 kwargs['page'] = args.nextpage_opaque_marker
407 kwargs['filter'] = args.filter if args.filter is not None else ''
409 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
410 return link_header(request.full_path, ret)
413 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
414 @api_ims_inventory_v1.param('deploymentManagerID',
415 'ID of the deployment manager')
416 @api_ims_inventory_v1.param(
417 'profile', 'DMS profile: value supports "native_k8sapi"',
419 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
420 @api_ims_inventory_v1.param(
422 'Set any value for show all fields. This value will cover "fields" ' +
425 @api_ims_inventory_v1.param(
427 'Set fields to show, split by comma, "/" for parent and children.' +
428 ' Like "name,parent/children". This value will cover "exculde_fields".',
430 @api_ims_inventory_v1.param(
432 'Set fields to exclude showing, split by comma, "/" for parent and ' +
433 'children. Like "name,parent/children". This value will cover ' +
434 '"exclude_default".',
436 @api_ims_inventory_v1.param(
438 'Exclude showing all default fields, Set "true" to enable.',
440 class DeploymentManagerGetRouter(Resource):
442 model = DeploymentManagerDTO.deployment_manager_get
444 @api_ims_inventory_v1.doc('Get deployment manager')
445 @api_ims_inventory_v1.marshal_with(model)
446 def get(self, deploymentManagerID):
447 parser = reqparse.RequestParser()
448 parser.add_argument('profile', location='args')
449 args = parser.parse_args()
451 args.profile if args.profile is not None and args.profile != ''
452 else ocloud.DeploymentManagerProfileDefault)
453 result = ocloud_view.deployment_manager_one(
454 deploymentManagerID, bus.uow, profile)
455 if result is not None and result != "":
458 raise NotFoundException(
459 "Profile {} doesn't support".format(
462 raise NotFoundException("Deployment manager {} doesn't exist".format(
463 deploymentManagerID))
466 # ---------- Subscriptions ---------- #
467 @api_ims_inventory_v1.route("/v1/subscriptions")
468 class SubscriptionsListRouter(Resource):
470 model = SubscriptionDTO.subscription_get
471 expect = SubscriptionDTO.subscription_create
473 @api_ims_inventory_v1.doc('List subscriptions')
474 @api_ims_inventory_v1.marshal_list_with(model)
475 @api_ims_inventory_v1.param(
477 'Page number of the results to fetch. Default: 1',
478 _in='query', default=1)
479 @api_ims_inventory_v1.param(
481 'Set any value for show all fields. This value will cover "fields" ' +
484 @api_ims_inventory_v1.param(
486 'Set fields to show, split by comma, "/" for parent and children.' +
487 ' Like "name,parent/children". This value will cover' +
488 ' "exculde_fields".',
490 @api_ims_inventory_v1.param(
492 'Set fields to exclude showing, split by comma, "/" for parent and ' +
493 'children. Like "name,parent/children". This value will cover ' +
494 '"exclude_default".',
496 @api_ims_inventory_v1.param(
498 'Exclude showing all default fields, Set "true" to enable.',
500 @api_ims_inventory_v1.param(
502 'Filter of the query.',
505 parser = reqparse.RequestParser()
506 parser.add_argument(PAGE_PARAM, location='args')
507 parser.add_argument('filter', location='args')
508 args = parser.parse_args()
510 if args.nextpage_opaque_marker is not None:
511 kwargs['page'] = args.nextpage_opaque_marker
512 kwargs['filter'] = args.filter if args.filter is not None else ''
514 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
515 return link_header(request.full_path, ret)
517 @api_ims_inventory_v1.doc('Create a subscription')
518 @api_ims_inventory_v1.expect(expect)
519 @api_ims_inventory_v1.marshal_with(
521 mask='{subscriptionId,callback,consumerSubscriptionId,filter}')
523 data = api_ims_inventory_v1.payload
524 callback = data.get('callback', None)
526 raise BadRequestException('The callback parameter is required')
528 result = ocloud_view.subscription_create(data, bus.uow)
532 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
533 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
534 @api_ims_inventory_v1.response(404, 'Subscription not found')
535 class SubscriptionGetDelRouter(Resource):
537 model = SubscriptionDTO.subscription_get
539 @api_ims_inventory_v1.doc('Get subscription by ID')
540 @api_ims_inventory_v1.marshal_with(model)
541 @api_ims_inventory_v1.param(
543 'Set any value for show all fields. This value will cover "fields" ' +
546 @api_ims_inventory_v1.param(
548 'Set fields to show, split by comma, "/" for parent and children.' +
549 ' Like "name,parent/children". This value will cover' +
550 ' "exculde_fields".',
552 @api_ims_inventory_v1.param(
554 'Set fields to exclude showing, split by comma, "/" for parent and ' +
555 'children. Like "name,parent/children". This value will cover ' +
556 '"exclude_default".',
558 @api_ims_inventory_v1.param(
560 'Exclude showing all default fields, Set "true" to enable.',
562 def get(self, subscriptionID):
563 result = ocloud_view.subscription_one(
564 subscriptionID, bus.uow)
565 if result is not None:
567 raise NotFoundException("Subscription {} doesn't exist".format(
570 @api_ims_inventory_v1.doc('Delete subscription by ID')
571 @api_ims_inventory_v1.response(200, 'Subscription deleted')
572 def delete(self, subscriptionID):
573 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)