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 o2ims.views import ocloud_view
21 from o2ims.views.api_ns import api_ims_inventory as api_ims_inventory_v1
22 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
23 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
29 def configure_api_route():
30 # Set global bus for resource
32 bus = MessageBus.get_instance()
35 # ---------- API versions ---------- #
36 @api_ims_inventory_v1.route("/v1/api_versions")
37 class VersionRouter(Resource):
40 'uriPrefix': request.base_url.rsplit('/', 1)[0],
43 # 'isDeprecated': 'False',
44 # 'retirementDate': ''
49 # ---------- OClouds ---------- #
50 @api_ims_inventory_v1.route("/v1/")
51 @api_ims_inventory_v1.response(404, 'oCloud not found')
52 @api_ims_inventory_v1.param(
54 'Set any value for show all fields. This value will cover "fields" ' +
57 @api_ims_inventory_v1.param(
59 'Set fields to show, split by comma, "/" for parent and children.' +
60 ' Like "name,parent/children". This value will cover "exculde_fields".',
62 @api_ims_inventory_v1.param(
64 'Set fields to exclude showing, split by comma, "/" for parent and ' +
65 'children. Like "name,parent/children". This value will cover ' +
68 @api_ims_inventory_v1.param(
70 'Exclude showing all default fields, Set "true" to enable.',
72 class OcloudsListRouter(Resource):
73 """Ocloud get endpoint
74 O2 interface ocloud endpoint
77 ocloud_get = OcloudDTO.ocloud
79 @api_ims_inventory_v1.marshal_with(ocloud_get)
81 res = ocloud_view.oclouds(bus.uow)
84 api_ims_inventory_v1.abort(
85 404, "oCloud doesn't exist")
88 # ---------- ResourceTypes ---------- #
89 @api_ims_inventory_v1.route("/v1/resourceTypes")
90 @api_ims_inventory_v1.param(PAGE_PARAM,
91 'Page number of the results to fetch.' +
93 _in='query', default=1)
94 @api_ims_inventory_v1.param(
96 'Set any value for show all fields. This value will cover "fields" ' +
99 @api_ims_inventory_v1.param(
101 'Set fields to show, split by comma, "/" for parent and children.' +
102 ' Like "name,parent/children". This value will cover "exculde_fields".',
104 @api_ims_inventory_v1.param(
106 'Set fields to exclude showing, split by comma, "/" for parent and ' +
107 'children. Like "name,parent/children". This value will cover ' +
108 '"exclude_default".',
110 @api_ims_inventory_v1.param(
112 'Exclude showing all default fields, Set "true" to enable.',
114 @api_ims_inventory_v1.param(
116 'Filter of the query.',
118 class ResourceTypesListRouter(Resource):
120 model = ResourceTypeDTO.resource_type_get
122 @api_ims_inventory_v1.marshal_list_with(model)
124 parser = reqparse.RequestParser()
125 parser.add_argument(PAGE_PARAM, location='args')
126 parser.add_argument('filter', location='args')
127 args = parser.parse_args()
129 if args.nextpage_opaque_marker is not None:
130 kwargs['page'] = args.nextpage_opaque_marker
131 kwargs['filter'] = args.filter if args.filter is not None else ''
133 ret = ocloud_view.resource_types(bus.uow, **kwargs)
134 return link_header(request.full_path, ret)
137 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
138 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
139 @api_ims_inventory_v1.response(404, 'Resource type not found')
140 @api_ims_inventory_v1.param(
142 'Set any value for show all fields. This value will cover "fields" ' +
145 @api_ims_inventory_v1.param(
147 'Set fields to show, split by comma, "/" for parent and children.' +
148 ' Like "name,parent/children". This value will cover "exculde_fields".',
150 @api_ims_inventory_v1.param(
152 'Set fields to exclude showing, split by comma, "/" for parent and ' +
153 'children. Like "name,parent/children". This value will cover ' +
154 '"exclude_default".',
156 @api_ims_inventory_v1.param(
158 'Exclude showing all default fields, Set "true" to enable.',
160 class ResourceTypeGetRouter(Resource):
162 model = ResourceTypeDTO.resource_type_get
164 @api_ims_inventory_v1.doc('Get resource type')
165 @api_ims_inventory_v1.marshal_with(model)
166 def get(self, resourceTypeID):
167 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
168 if result is not None:
170 api_ims_inventory_v1.abort(
171 404, "Resource type {} doesn't exist".format(resourceTypeID))
174 # ---------- ResourcePools ---------- #
175 @api_ims_inventory_v1.route("/v1/resourcePools")
176 @api_ims_inventory_v1.param(PAGE_PARAM,
177 'Page number of the results to fetch.' +
179 _in='query', default=1)
180 @api_ims_inventory_v1.param(
182 'Set any value for show all fields. This value will cover "fields" ' +
185 @api_ims_inventory_v1.param(
187 'Set fields to show, split by comma, "/" for parent and children.' +
188 ' Like "name,parent/children". This value will cover "exculde_fields".',
190 @api_ims_inventory_v1.param(
192 'Set fields to exclude showing, split by comma, "/" for parent and ' +
193 'children. Like "name,parent/children". This value will cover ' +
194 '"exclude_default".',
196 @api_ims_inventory_v1.param(
198 'Exclude showing all default fields, Set "true" to enable.',
200 @api_ims_inventory_v1.param(
202 'Filter of the query.',
204 class ResourcePoolsListRouter(Resource):
206 model = ResourcePoolDTO.resource_pool_get
208 @api_ims_inventory_v1.marshal_list_with(model)
210 parser = reqparse.RequestParser()
211 parser.add_argument(PAGE_PARAM, location='args')
212 parser.add_argument('filter', location='args')
213 args = parser.parse_args()
215 if args.nextpage_opaque_marker is not None:
216 kwargs['page'] = args.nextpage_opaque_marker
217 kwargs['filter'] = args.filter if args.filter is not None else ''
219 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
220 return link_header(request.full_path, ret)
223 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
224 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
225 @api_ims_inventory_v1.response(404, 'Resource pool not found')
226 @api_ims_inventory_v1.param(
228 'Set any value for show all fields. This value will cover "fields" ' +
231 @api_ims_inventory_v1.param(
233 'Set fields to show, split by comma, "/" for parent and children.' +
234 ' Like "name,parent/children". This value will cover "exculde_fields".',
236 @api_ims_inventory_v1.param(
238 'Set fields to exclude showing, split by comma, "/" for parent and ' +
239 'children. Like "name,parent/children". This value will cover ' +
240 '"exclude_default".',
242 @api_ims_inventory_v1.param(
244 'Exclude showing all default fields, Set "true" to enable.',
246 class ResourcePoolGetRouter(Resource):
248 model = ResourcePoolDTO.resource_pool_get
250 @api_ims_inventory_v1.doc('Get resource pool')
251 @api_ims_inventory_v1.marshal_with(model)
252 def get(self, resourcePoolID):
253 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
254 if result is not None:
256 api_ims_inventory_v1.abort(
257 404, "Resource pool {} doesn't exist".format(resourcePoolID))
260 # ---------- Resources ---------- #
261 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
262 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
263 # @api_ims_inventory_v1.param('sort', 'sort by column name',
265 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
266 # '(max 100). Default: 30',
267 # _in='query', default=30)
268 @api_ims_inventory_v1.param(PAGE_PARAM,
269 'Page number of the results to fetch.' +
271 _in='query', default=1)
272 @api_ims_inventory_v1.param(
274 'Set any value for show all fields. This value will cover "fields" ' +
277 @api_ims_inventory_v1.param(
279 'Set fields to show, split by comma, "/" for parent and children.' +
280 ' Like "name,parent/children". This value will cover "exculde_fields".',
282 @api_ims_inventory_v1.param(
284 'Set fields to exclude showing, split by comma, "/" for parent and ' +
285 'children. Like "name,parent/children". This value will cover ' +
286 '"exclude_default".',
288 @api_ims_inventory_v1.param(
290 'Exclude showing all default fields, Set "true" to enable.',
292 @api_ims_inventory_v1.param(
294 'Filter of the query.',
296 class ResourcesListRouter(Resource):
298 model = ResourceDTO.resource_list
300 @api_ims_inventory_v1.marshal_list_with(model)
301 def get(self, resourcePoolID):
302 parser = reqparse.RequestParser()
303 parser.add_argument(PAGE_PARAM, location='args')
304 parser.add_argument('filter', location='args')
305 args = parser.parse_args()
307 # if args.per_page is not None:
308 # kwargs['per_page'] = args.per_page
309 # base_url = base_url + 'per_page=' + args.per_page + '&'
310 if args.nextpage_opaque_marker is not None:
311 kwargs['page'] = args.nextpage_opaque_marker
312 kwargs['filter'] = args.filter if args.filter is not None else ''
314 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
315 return link_header(request.full_path, ret)
318 @api_ims_inventory_v1.route(
319 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
320 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
321 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
322 @api_ims_inventory_v1.response(404, 'Resource not found')
323 @api_ims_inventory_v1.param(
325 'Set any value for show all fields. This value will cover "fields" ' +
328 @api_ims_inventory_v1.param(
330 'Set fields to show, split by comma, "/" for parent and children.' +
331 ' Like "name,parent/children". This value will cover "exculde_fields".',
333 @api_ims_inventory_v1.param(
335 'Set fields to exclude showing, split by comma, "/" for parent and ' +
336 'children. Like "name,parent/children". This value will cover ' +
337 '"exclude_default".',
339 @api_ims_inventory_v1.param(
341 'Exclude showing all default fields, Set "true" to enable.',
343 class ResourceGetRouter(Resource):
345 # dto = ResourceDTO()
346 # model = dto.get_resource_get()
347 model = ResourceDTO.recursive_resource_mapping()
349 @api_ims_inventory_v1.doc('Get resource')
350 @api_ims_inventory_v1.marshal_with(model)
351 def get(self, resourcePoolID, resourceID):
352 result = ocloud_view.resource_one(resourceID, bus.uow)
353 if result is not None:
355 api_ims_inventory_v1.abort(
356 404, "Resource {} doesn't exist".format(resourceID))
359 # ---------- DeploymentManagers ---------- #
360 @api_ims_inventory_v1.route("/v1/deploymentManagers")
361 @api_ims_inventory_v1.param(PAGE_PARAM,
362 'Page number of the results to fetch.' +
364 _in='query', default=1)
365 @api_ims_inventory_v1.param(
367 'Set any value for show all fields. This value will cover "fields" ' +
370 @api_ims_inventory_v1.param(
372 'Set fields to show, split by comma, "/" for parent and children.' +
373 ' Like "name,parent/children". This value will cover "exculde_fields".',
375 @api_ims_inventory_v1.param(
377 'Set fields to exclude showing, split by comma, "/" for parent and ' +
378 'children. Like "name,parent/children". This value will cover ' +
379 '"exclude_default".',
381 @api_ims_inventory_v1.param(
383 'Exclude showing all default fields, Set "true" to enable.',
385 @api_ims_inventory_v1.param(
387 'Filter of the query.',
389 class DeploymentManagersListRouter(Resource):
391 model = DeploymentManagerDTO.deployment_manager_list
393 @api_ims_inventory_v1.marshal_list_with(model)
395 parser = reqparse.RequestParser()
396 parser.add_argument(PAGE_PARAM, location='args')
397 parser.add_argument('filter', location='args')
398 args = parser.parse_args()
400 if args.nextpage_opaque_marker is not None:
401 kwargs['page'] = args.nextpage_opaque_marker
402 kwargs['filter'] = args.filter if args.filter is not None else ''
404 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
405 return link_header(request.full_path, ret)
408 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
409 @api_ims_inventory_v1.param('deploymentManagerID',
410 'ID of the deployment manager')
411 @api_ims_inventory_v1.param('profile', 'DMS profile: value supports "sol018"',
413 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
414 @api_ims_inventory_v1.param(
416 'Set any value for show all fields. This value will cover "fields" ' +
419 @api_ims_inventory_v1.param(
421 'Set fields to show, split by comma, "/" for parent and children.' +
422 ' Like "name,parent/children". This value will cover "exculde_fields".',
424 @api_ims_inventory_v1.param(
426 'Set fields to exclude showing, split by comma, "/" for parent and ' +
427 'children. Like "name,parent/children". This value will cover ' +
428 '"exclude_default".',
430 @api_ims_inventory_v1.param(
432 'Exclude showing all default fields, Set "true" to enable.',
434 class DeploymentManagerGetRouter(Resource):
436 model = DeploymentManagerDTO.deployment_manager_get
438 @api_ims_inventory_v1.doc('Get deployment manager')
439 @api_ims_inventory_v1.marshal_with(model)
440 def get(self, deploymentManagerID):
441 parser = reqparse.RequestParser()
442 parser.add_argument('profile', location='args')
443 args = parser.parse_args()
445 args.profile if args.profile is not None and args.profile != ''
447 result = ocloud_view.deployment_manager_one(
448 deploymentManagerID, bus.uow, profile)
449 if result is not None:
451 api_ims_inventory_v1.abort(
453 "Deployment manager {} doesn't exist".format(deploymentManagerID))
456 # ---------- Subscriptions ---------- #
457 @api_ims_inventory_v1.route("/v1/subscriptions")
458 class SubscriptionsListRouter(Resource):
460 model = SubscriptionDTO.subscription_get
461 expect = SubscriptionDTO.subscription
462 post_resp = SubscriptionDTO.subscription_post_resp
464 @api_ims_inventory_v1.doc('List subscriptions')
465 @api_ims_inventory_v1.marshal_list_with(model)
466 @api_ims_inventory_v1.param(
468 'Page number of the results to fetch. Default: 1',
469 _in='query', default=1)
470 @api_ims_inventory_v1.param(
472 'Set any value for show all fields. This value will cover "fields" ' +
475 @api_ims_inventory_v1.param(
477 'Set fields to show, split by comma, "/" for parent and children.' +
478 ' Like "name,parent/children". This value will cover' +
479 ' "exculde_fields".',
481 @api_ims_inventory_v1.param(
483 'Set fields to exclude showing, split by comma, "/" for parent and ' +
484 'children. Like "name,parent/children". This value will cover ' +
485 '"exclude_default".',
487 @api_ims_inventory_v1.param(
489 'Exclude showing all default fields, Set "true" to enable.',
491 @api_ims_inventory_v1.param(
493 'Filter of the query.',
496 parser = reqparse.RequestParser()
497 parser.add_argument(PAGE_PARAM, location='args')
498 parser.add_argument('filter', location='args')
499 args = parser.parse_args()
501 if args.nextpage_opaque_marker is not None:
502 kwargs['page'] = args.nextpage_opaque_marker
503 kwargs['filter'] = args.filter if args.filter is not None else ''
505 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
506 return link_header(request.full_path, ret)
508 @api_ims_inventory_v1.doc('Create a subscription')
509 @api_ims_inventory_v1.expect(expect)
510 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
512 data = api_ims_inventory_v1.payload
513 result = ocloud_view.subscription_create(data, bus.uow)
517 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
518 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
519 @api_ims_inventory_v1.response(404, 'Subscription not found')
520 class SubscriptionGetDelRouter(Resource):
522 model = SubscriptionDTO.subscription_get
524 @api_ims_inventory_v1.doc('Get subscription by ID')
525 @api_ims_inventory_v1.marshal_with(model)
526 @api_ims_inventory_v1.param(
528 'Set any value for show all fields. This value will cover "fields" ' +
531 @api_ims_inventory_v1.param(
533 'Set fields to show, split by comma, "/" for parent and children.' +
534 ' Like "name,parent/children". This value will cover' +
535 ' "exculde_fields".',
537 @api_ims_inventory_v1.param(
539 'Set fields to exclude showing, split by comma, "/" for parent and ' +
540 'children. Like "name,parent/children". This value will cover ' +
541 '"exclude_default".',
543 @api_ims_inventory_v1.param(
545 'Exclude showing all default fields, Set "true" to enable.',
547 def get(self, subscriptionID):
548 result = ocloud_view.subscription_one(
549 subscriptionID, bus.uow)
550 if result is not None:
552 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
555 @api_ims_inventory_v1.doc('Delete subscription by ID')
556 @api_ims_inventory_v1.response(204, 'Subscription deleted')
557 def delete(self, subscriptionID):
558 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)