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 import ProblemDetails
21 from o2ims.views import ocloud_view
22 from o2ims.views.api_ns import api_ims_inventory as api_ims_inventory_v1
23 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
24 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
30 def configure_api_route():
31 # Set global bus for resource
33 bus = MessageBus.get_instance()
36 # ---------- API versions ---------- #
37 @api_ims_inventory_v1.route("/v1/api_versions")
38 class VersionRouter(Resource):
41 'uriPrefix': request.base_url.rsplit('/', 1)[0],
44 # 'isDeprecated': 'False',
45 # 'retirementDate': ''
50 # ---------- OClouds ---------- #
51 @api_ims_inventory_v1.route("/v1/")
52 @api_ims_inventory_v1.response(404, 'oCloud not found')
53 @api_ims_inventory_v1.param(
55 'Set any value for show all fields. This value will cover "fields" ' +
58 @api_ims_inventory_v1.param(
60 'Set fields to show, split by comma, "/" for parent and children.' +
61 ' Like "name,parent/children". This value will cover "exculde_fields".',
63 @api_ims_inventory_v1.param(
65 'Set fields to exclude showing, split by comma, "/" for parent and ' +
66 'children. Like "name,parent/children". This value will cover ' +
69 @api_ims_inventory_v1.param(
71 'Exclude showing all default fields, Set "true" to enable.',
73 class OcloudsListRouter(Resource):
74 """Ocloud get endpoint
75 O2 interface ocloud endpoint
78 ocloud_get = OcloudDTO.ocloud
80 @api_ims_inventory_v1.marshal_with(ocloud_get)
82 res = ocloud_view.oclouds(bus.uow)
87 404, "oCloud doesn't exist").abort()
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)
170 if result is not None:
173 api_ims_inventory_v1,
174 404, "Resource type {} doesn't exist".format(
175 resourceTypeID)).abort()
178 # ---------- ResourcePools ---------- #
179 @api_ims_inventory_v1.route("/v1/resourcePools")
180 @api_ims_inventory_v1.param(PAGE_PARAM,
181 'Page number of the results to fetch.' +
183 _in='query', default=1)
184 @api_ims_inventory_v1.param(
186 'Set any value for show all fields. This value will cover "fields" ' +
189 @api_ims_inventory_v1.param(
191 'Set fields to show, split by comma, "/" for parent and children.' +
192 ' Like "name,parent/children". This value will cover "exculde_fields".',
194 @api_ims_inventory_v1.param(
196 'Set fields to exclude showing, split by comma, "/" for parent and ' +
197 'children. Like "name,parent/children". This value will cover ' +
198 '"exclude_default".',
200 @api_ims_inventory_v1.param(
202 'Exclude showing all default fields, Set "true" to enable.',
204 @api_ims_inventory_v1.param(
206 'Filter of the query.',
208 class ResourcePoolsListRouter(Resource):
210 model = ResourcePoolDTO.resource_pool_get
212 @api_ims_inventory_v1.marshal_list_with(model)
214 parser = reqparse.RequestParser()
215 parser.add_argument(PAGE_PARAM, location='args')
216 parser.add_argument('filter', location='args')
217 args = parser.parse_args()
219 if args.nextpage_opaque_marker is not None:
220 kwargs['page'] = args.nextpage_opaque_marker
221 kwargs['filter'] = args.filter if args.filter is not None else ''
223 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
224 return link_header(request.full_path, ret)
227 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
228 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
229 @api_ims_inventory_v1.response(404, 'Resource pool not found')
230 @api_ims_inventory_v1.param(
232 'Set any value for show all fields. This value will cover "fields" ' +
235 @api_ims_inventory_v1.param(
237 'Set fields to show, split by comma, "/" for parent and children.' +
238 ' Like "name,parent/children". This value will cover "exculde_fields".',
240 @api_ims_inventory_v1.param(
242 'Set fields to exclude showing, split by comma, "/" for parent and ' +
243 'children. Like "name,parent/children". This value will cover ' +
244 '"exclude_default".',
246 @api_ims_inventory_v1.param(
248 'Exclude showing all default fields, Set "true" to enable.',
250 class ResourcePoolGetRouter(Resource):
252 model = ResourcePoolDTO.resource_pool_get
254 @api_ims_inventory_v1.doc('Get resource pool')
255 @api_ims_inventory_v1.marshal_with(model)
256 def get(self, resourcePoolID):
257 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
258 if result is not None:
261 api_ims_inventory_v1,
262 404, "Resource pool {} doesn't exist".format(
263 resourcePoolID)).abort()
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.param('sort', 'sort by column name',
271 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
272 # '(max 100). Default: 30',
273 # _in='query', default=30)
274 @api_ims_inventory_v1.param(PAGE_PARAM,
275 'Page number of the results to fetch.' +
277 _in='query', default=1)
278 @api_ims_inventory_v1.param(
280 'Set any value for show all fields. This value will cover "fields" ' +
283 @api_ims_inventory_v1.param(
285 'Set fields to show, split by comma, "/" for parent and children.' +
286 ' Like "name,parent/children". This value will cover "exculde_fields".',
288 @api_ims_inventory_v1.param(
290 'Set fields to exclude showing, split by comma, "/" for parent and ' +
291 'children. Like "name,parent/children". This value will cover ' +
292 '"exclude_default".',
294 @api_ims_inventory_v1.param(
296 'Exclude showing all default fields, Set "true" to enable.',
298 @api_ims_inventory_v1.param(
300 'Filter of the query.',
302 class ResourcesListRouter(Resource):
304 model = ResourceDTO.resource_list
306 @api_ims_inventory_v1.marshal_list_with(model)
307 def get(self, resourcePoolID):
308 parser = reqparse.RequestParser()
309 parser.add_argument(PAGE_PARAM, location='args')
310 parser.add_argument('filter', location='args')
311 args = parser.parse_args()
313 # if args.per_page is not None:
314 # kwargs['per_page'] = args.per_page
315 # base_url = base_url + 'per_page=' + args.per_page + '&'
316 if args.nextpage_opaque_marker is not None:
317 kwargs['page'] = args.nextpage_opaque_marker
318 kwargs['filter'] = args.filter if args.filter is not None else ''
320 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
321 return link_header(request.full_path, ret)
324 @api_ims_inventory_v1.route(
325 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
326 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
327 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
328 @api_ims_inventory_v1.response(404, 'Resource not found')
329 @api_ims_inventory_v1.param(
331 'Set any value for show all fields. This value will cover "fields" ' +
334 @api_ims_inventory_v1.param(
336 'Set fields to show, split by comma, "/" for parent and children.' +
337 ' Like "name,parent/children". This value will cover "exculde_fields".',
339 @api_ims_inventory_v1.param(
341 'Set fields to exclude showing, split by comma, "/" for parent and ' +
342 'children. Like "name,parent/children". This value will cover ' +
343 '"exclude_default".',
345 @api_ims_inventory_v1.param(
347 'Exclude showing all default fields, Set "true" to enable.',
349 class ResourceGetRouter(Resource):
351 # dto = ResourceDTO()
352 # model = dto.get_resource_get()
353 model = ResourceDTO.recursive_resource_mapping()
355 @api_ims_inventory_v1.doc('Get resource')
356 @api_ims_inventory_v1.marshal_with(model)
357 def get(self, resourcePoolID, resourceID):
358 result = ocloud_view.resource_one(resourceID, bus.uow)
359 if result is not None:
362 api_ims_inventory_v1,
363 404, "Resource {} doesn't exist".format(
367 # ---------- DeploymentManagers ---------- #
368 @api_ims_inventory_v1.route("/v1/deploymentManagers")
369 @api_ims_inventory_v1.param(PAGE_PARAM,
370 'Page number of the results to fetch.' +
372 _in='query', default=1)
373 @api_ims_inventory_v1.param(
375 'Set any value for show all fields. This value will cover "fields" ' +
378 @api_ims_inventory_v1.param(
380 'Set fields to show, split by comma, "/" for parent and children.' +
381 ' Like "name,parent/children". This value will cover "exculde_fields".',
383 @api_ims_inventory_v1.param(
385 'Set fields to exclude showing, split by comma, "/" for parent and ' +
386 'children. Like "name,parent/children". This value will cover ' +
387 '"exclude_default".',
389 @api_ims_inventory_v1.param(
391 'Exclude showing all default fields, Set "true" to enable.',
393 @api_ims_inventory_v1.param(
395 'Filter of the query.',
397 class DeploymentManagersListRouter(Resource):
399 model = DeploymentManagerDTO.deployment_manager_list
401 @api_ims_inventory_v1.marshal_list_with(model)
403 parser = reqparse.RequestParser()
404 parser.add_argument(PAGE_PARAM, location='args')
405 parser.add_argument('filter', location='args')
406 args = parser.parse_args()
408 if args.nextpage_opaque_marker is not None:
409 kwargs['page'] = args.nextpage_opaque_marker
410 kwargs['filter'] = args.filter if args.filter is not None else ''
412 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
413 return link_header(request.full_path, ret)
416 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
417 @api_ims_inventory_v1.param('deploymentManagerID',
418 'ID of the deployment manager')
419 @api_ims_inventory_v1.param('profile', 'DMS profile: value supports "sol018"',
421 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
422 @api_ims_inventory_v1.param(
424 'Set any value for show all fields. This value will cover "fields" ' +
427 @api_ims_inventory_v1.param(
429 'Set fields to show, split by comma, "/" for parent and children.' +
430 ' Like "name,parent/children". This value will cover "exculde_fields".',
432 @api_ims_inventory_v1.param(
434 'Set fields to exclude showing, split by comma, "/" for parent and ' +
435 'children. Like "name,parent/children". This value will cover ' +
436 '"exclude_default".',
438 @api_ims_inventory_v1.param(
440 'Exclude showing all default fields, Set "true" to enable.',
442 class DeploymentManagerGetRouter(Resource):
444 model = DeploymentManagerDTO.deployment_manager_get
446 @api_ims_inventory_v1.doc('Get deployment manager')
447 @api_ims_inventory_v1.marshal_with(model)
448 def get(self, deploymentManagerID):
449 parser = reqparse.RequestParser()
450 parser.add_argument('profile', location='args')
451 args = parser.parse_args()
453 args.profile if args.profile is not None and args.profile != ''
455 result = ocloud_view.deployment_manager_one(
456 deploymentManagerID, bus.uow, profile)
457 if result is not None:
461 api_ims_inventory_v1,
462 404, "Deployment manager {} doesn't exist".format(
463 deploymentManagerID)).abort()
466 # ---------- Subscriptions ---------- #
467 @api_ims_inventory_v1.route("/v1/subscriptions")
468 class SubscriptionsListRouter(Resource):
470 model = SubscriptionDTO.subscription_get
471 expect = SubscriptionDTO.subscription
472 post_resp = SubscriptionDTO.subscription_post_resp
474 @api_ims_inventory_v1.doc('List subscriptions')
475 @api_ims_inventory_v1.marshal_list_with(model)
476 @api_ims_inventory_v1.param(
478 'Page number of the results to fetch. Default: 1',
479 _in='query', default=1)
480 @api_ims_inventory_v1.param(
482 'Set any value for show all fields. This value will cover "fields" ' +
485 @api_ims_inventory_v1.param(
487 'Set fields to show, split by comma, "/" for parent and children.' +
488 ' Like "name,parent/children". This value will cover' +
489 ' "exculde_fields".',
491 @api_ims_inventory_v1.param(
493 'Set fields to exclude showing, split by comma, "/" for parent and ' +
494 'children. Like "name,parent/children". This value will cover ' +
495 '"exclude_default".',
497 @api_ims_inventory_v1.param(
499 'Exclude showing all default fields, Set "true" to enable.',
501 @api_ims_inventory_v1.param(
503 'Filter of the query.',
506 parser = reqparse.RequestParser()
507 parser.add_argument(PAGE_PARAM, location='args')
508 parser.add_argument('filter', location='args')
509 args = parser.parse_args()
511 if args.nextpage_opaque_marker is not None:
512 kwargs['page'] = args.nextpage_opaque_marker
513 kwargs['filter'] = args.filter if args.filter is not None else ''
515 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
516 return link_header(request.full_path, ret)
518 @api_ims_inventory_v1.doc('Create a subscription')
519 @api_ims_inventory_v1.expect(expect)
520 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
522 data = api_ims_inventory_v1.payload
523 result = ocloud_view.subscription_create(data, bus.uow)
527 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
528 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
529 @api_ims_inventory_v1.response(404, 'Subscription not found')
530 class SubscriptionGetDelRouter(Resource):
532 model = SubscriptionDTO.subscription_get
534 @api_ims_inventory_v1.doc('Get subscription by ID')
535 @api_ims_inventory_v1.marshal_with(model)
536 @api_ims_inventory_v1.param(
538 'Set any value for show all fields. This value will cover "fields" ' +
541 @api_ims_inventory_v1.param(
543 'Set fields to show, split by comma, "/" for parent and children.' +
544 ' Like "name,parent/children". This value will cover' +
545 ' "exculde_fields".',
547 @api_ims_inventory_v1.param(
549 'Set fields to exclude showing, split by comma, "/" for parent and ' +
550 'children. Like "name,parent/children". This value will cover ' +
551 '"exclude_default".',
553 @api_ims_inventory_v1.param(
555 'Exclude showing all default fields, Set "true" to enable.',
557 def get(self, subscriptionID):
558 result = ocloud_view.subscription_one(
559 subscriptionID, bus.uow)
560 if result is not None:
563 api_ims_inventory_v1,
564 404, "Subscription {} doesn't exist".format(
565 subscriptionID)).abort()
567 @api_ims_inventory_v1.doc('Delete subscription by ID')
568 @api_ims_inventory_v1.response(204, 'Subscription deleted')
569 def delete(self, subscriptionID):
570 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)