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_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 # ---------- OClouds ---------- #
36 @api_ims_inventory_v1.route("/")
37 @api_ims_inventory_v1.response(404, 'oCloud not found')
38 @api_ims_inventory_v1.param(
40 'Set any value for show all fields. This value will cover "fields" ' +
43 @api_ims_inventory_v1.param(
45 'Set fields to show, split by comma, "/" for parent and children.' +
46 ' Like "name,parent/children". This value will cover "exculde_fields".',
48 @api_ims_inventory_v1.param(
50 'Set fields to exclude showing, split by comma, "/" for parent and ' +
51 'children. Like "name,parent/children". This value will cover ' +
54 @api_ims_inventory_v1.param(
56 'Exclude showing all default fields, Set "true" to enable.',
58 class OcloudsListRouter(Resource):
59 """Ocloud get endpoint
60 O2 interface ocloud endpoint
63 ocloud_get = OcloudDTO.ocloud
65 @api_ims_inventory_v1.marshal_with(ocloud_get)
67 res = ocloud_view.oclouds(bus.uow)
70 api_ims_inventory_v1.abort(
71 404, "oCloud doesn't exist")
74 # ---------- ResourceTypes ---------- #
75 @api_ims_inventory_v1.route("/resourceTypes")
76 @api_ims_inventory_v1.param(PAGE_PARAM,
77 'Page number of the results to fetch.' +
79 _in='query', default=1)
80 @api_ims_inventory_v1.param(
82 'Set any value for show all fields. This value will cover "fields" ' +
85 @api_ims_inventory_v1.param(
87 'Set fields to show, split by comma, "/" for parent and children.' +
88 ' Like "name,parent/children". This value will cover "exculde_fields".',
90 @api_ims_inventory_v1.param(
92 'Set fields to exclude showing, split by comma, "/" for parent and ' +
93 'children. Like "name,parent/children". This value will cover ' +
96 @api_ims_inventory_v1.param(
98 'Exclude showing all default fields, Set "true" to enable.',
100 @api_ims_inventory_v1.param(
102 'Filter of the query.',
104 class ResourceTypesListRouter(Resource):
106 model = ResourceTypeDTO.resource_type_get
108 @api_ims_inventory_v1.marshal_list_with(model)
110 parser = reqparse.RequestParser()
111 parser.add_argument(PAGE_PARAM, location='args')
112 parser.add_argument('filter', location='args')
113 args = parser.parse_args()
115 if args.nextpage_opaque_marker is not None:
116 kwargs['page'] = args.nextpage_opaque_marker
117 kwargs['filter'] = args.filter if args.filter is not None else ''
119 ret = ocloud_view.resource_types(bus.uow, **kwargs)
120 return link_header(request.full_path, ret)
123 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
124 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
125 @api_ims_inventory_v1.response(404, 'Resource type not found')
126 @api_ims_inventory_v1.param(
128 'Set any value for show all fields. This value will cover "fields" ' +
131 @api_ims_inventory_v1.param(
133 'Set fields to show, split by comma, "/" for parent and children.' +
134 ' Like "name,parent/children". This value will cover "exculde_fields".',
136 @api_ims_inventory_v1.param(
138 'Set fields to exclude showing, split by comma, "/" for parent and ' +
139 'children. Like "name,parent/children". This value will cover ' +
140 '"exclude_default".',
142 @api_ims_inventory_v1.param(
144 'Exclude showing all default fields, Set "true" to enable.',
146 class ResourceTypeGetRouter(Resource):
148 model = ResourceTypeDTO.resource_type_get
150 @api_ims_inventory_v1.doc('Get resource type')
151 @api_ims_inventory_v1.marshal_with(model)
152 def get(self, resourceTypeID):
153 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
154 if result is not None:
156 api_ims_inventory_v1.abort(
157 404, "Resource type {} doesn't exist".format(resourceTypeID))
160 # ---------- ResourcePools ---------- #
161 @api_ims_inventory_v1.route("/resourcePools")
162 @api_ims_inventory_v1.param(PAGE_PARAM,
163 'Page number of the results to fetch.' +
165 _in='query', default=1)
166 @api_ims_inventory_v1.param(
168 'Set any value for show all fields. This value will cover "fields" ' +
171 @api_ims_inventory_v1.param(
173 'Set fields to show, split by comma, "/" for parent and children.' +
174 ' Like "name,parent/children". This value will cover "exculde_fields".',
176 @api_ims_inventory_v1.param(
178 'Set fields to exclude showing, split by comma, "/" for parent and ' +
179 'children. Like "name,parent/children". This value will cover ' +
180 '"exclude_default".',
182 @api_ims_inventory_v1.param(
184 'Exclude showing all default fields, Set "true" to enable.',
186 @api_ims_inventory_v1.param(
188 'Filter of the query.',
190 class ResourcePoolsListRouter(Resource):
192 model = ResourcePoolDTO.resource_pool_get
194 @api_ims_inventory_v1.marshal_list_with(model)
196 parser = reqparse.RequestParser()
197 parser.add_argument(PAGE_PARAM, location='args')
198 parser.add_argument('filter', location='args')
199 args = parser.parse_args()
201 if args.nextpage_opaque_marker is not None:
202 kwargs['page'] = args.nextpage_opaque_marker
203 kwargs['filter'] = args.filter if args.filter is not None else ''
205 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
206 return link_header(request.full_path, ret)
209 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
210 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
211 @api_ims_inventory_v1.response(404, 'Resource pool not found')
212 @api_ims_inventory_v1.param(
214 'Set any value for show all fields. This value will cover "fields" ' +
217 @api_ims_inventory_v1.param(
219 'Set fields to show, split by comma, "/" for parent and children.' +
220 ' Like "name,parent/children". This value will cover "exculde_fields".',
222 @api_ims_inventory_v1.param(
224 'Set fields to exclude showing, split by comma, "/" for parent and ' +
225 'children. Like "name,parent/children". This value will cover ' +
226 '"exclude_default".',
228 @api_ims_inventory_v1.param(
230 'Exclude showing all default fields, Set "true" to enable.',
232 class ResourcePoolGetRouter(Resource):
234 model = ResourcePoolDTO.resource_pool_get
236 @api_ims_inventory_v1.doc('Get resource pool')
237 @api_ims_inventory_v1.marshal_with(model)
238 def get(self, resourcePoolID):
239 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
240 if result is not None:
242 api_ims_inventory_v1.abort(
243 404, "Resource pool {} doesn't exist".format(resourcePoolID))
246 # ---------- Resources ---------- #
247 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
248 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
249 # @api_ims_inventory_v1.param('sort', 'sort by column name',
251 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
252 # '(max 100). Default: 30',
253 # _in='query', default=30)
254 @api_ims_inventory_v1.param(PAGE_PARAM,
255 'Page number of the results to fetch.' +
257 _in='query', default=1)
258 @api_ims_inventory_v1.param(
260 'Set any value for show all fields. This value will cover "fields" ' +
263 @api_ims_inventory_v1.param(
265 'Set fields to show, split by comma, "/" for parent and children.' +
266 ' Like "name,parent/children". This value will cover "exculde_fields".',
268 @api_ims_inventory_v1.param(
270 'Set fields to exclude showing, split by comma, "/" for parent and ' +
271 'children. Like "name,parent/children". This value will cover ' +
272 '"exclude_default".',
274 @api_ims_inventory_v1.param(
276 'Exclude showing all default fields, Set "true" to enable.',
278 @api_ims_inventory_v1.param(
280 'Filter of the query.',
282 class ResourcesListRouter(Resource):
284 model = ResourceDTO.resource_list
286 @api_ims_inventory_v1.marshal_list_with(model)
287 def get(self, resourcePoolID):
288 parser = reqparse.RequestParser()
289 parser.add_argument(PAGE_PARAM, location='args')
290 parser.add_argument('filter', location='args')
291 args = parser.parse_args()
293 # if args.per_page is not None:
294 # kwargs['per_page'] = args.per_page
295 # base_url = base_url + 'per_page=' + args.per_page + '&'
296 if args.nextpage_opaque_marker is not None:
297 kwargs['page'] = args.nextpage_opaque_marker
298 kwargs['filter'] = args.filter if args.filter is not None else ''
300 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
301 return link_header(request.full_path, ret)
304 @api_ims_inventory_v1.route(
305 "/resourcePools/<resourcePoolID>/resources/<resourceID>")
306 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
307 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
308 @api_ims_inventory_v1.response(404, 'Resource not found')
309 @api_ims_inventory_v1.param(
311 'Set any value for show all fields. This value will cover "fields" ' +
314 @api_ims_inventory_v1.param(
316 'Set fields to show, split by comma, "/" for parent and children.' +
317 ' Like "name,parent/children". This value will cover "exculde_fields".',
319 @api_ims_inventory_v1.param(
321 'Set fields to exclude showing, split by comma, "/" for parent and ' +
322 'children. Like "name,parent/children". This value will cover ' +
323 '"exclude_default".',
325 @api_ims_inventory_v1.param(
327 'Exclude showing all default fields, Set "true" to enable.',
329 class ResourceGetRouter(Resource):
331 # dto = ResourceDTO()
332 # model = dto.get_resource_get()
333 model = ResourceDTO.recursive_resource_mapping()
335 @api_ims_inventory_v1.doc('Get resource')
336 @api_ims_inventory_v1.marshal_with(model)
337 def get(self, resourcePoolID, resourceID):
338 result = ocloud_view.resource_one(resourceID, bus.uow)
339 if result is not None:
341 api_ims_inventory_v1.abort(
342 404, "Resource {} doesn't exist".format(resourceID))
345 # ---------- DeploymentManagers ---------- #
346 @api_ims_inventory_v1.route("/deploymentManagers")
347 @api_ims_inventory_v1.param(PAGE_PARAM,
348 'Page number of the results to fetch.' +
350 _in='query', default=1)
351 @api_ims_inventory_v1.param(
353 'Set any value for show all fields. This value will cover "fields" ' +
356 @api_ims_inventory_v1.param(
358 'Set fields to show, split by comma, "/" for parent and children.' +
359 ' Like "name,parent/children". This value will cover "exculde_fields".',
361 @api_ims_inventory_v1.param(
363 'Set fields to exclude showing, split by comma, "/" for parent and ' +
364 'children. Like "name,parent/children". This value will cover ' +
365 '"exclude_default".',
367 @api_ims_inventory_v1.param(
369 'Exclude showing all default fields, Set "true" to enable.',
371 @api_ims_inventory_v1.param(
373 'Filter of the query.',
375 class DeploymentManagersListRouter(Resource):
377 model = DeploymentManagerDTO.deployment_manager_list
379 @api_ims_inventory_v1.marshal_list_with(model)
381 parser = reqparse.RequestParser()
382 parser.add_argument(PAGE_PARAM, location='args')
383 parser.add_argument('filter', location='args')
384 args = parser.parse_args()
386 if args.nextpage_opaque_marker is not None:
387 kwargs['page'] = args.nextpage_opaque_marker
388 kwargs['filter'] = args.filter if args.filter is not None else ''
390 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
391 return link_header(request.full_path, ret)
394 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
395 @api_ims_inventory_v1.param('deploymentManagerID',
396 'ID of the deployment manager')
397 @api_ims_inventory_v1.param('profile', 'DMS profile: value supports "sol018"',
399 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
400 @api_ims_inventory_v1.param(
402 'Set any value for show all fields. This value will cover "fields" ' +
405 @api_ims_inventory_v1.param(
407 'Set fields to show, split by comma, "/" for parent and children.' +
408 ' Like "name,parent/children". This value will cover "exculde_fields".',
410 @api_ims_inventory_v1.param(
412 'Set fields to exclude showing, split by comma, "/" for parent and ' +
413 'children. Like "name,parent/children". This value will cover ' +
414 '"exclude_default".',
416 @api_ims_inventory_v1.param(
418 'Exclude showing all default fields, Set "true" to enable.',
420 class DeploymentManagerGetRouter(Resource):
422 model = DeploymentManagerDTO.deployment_manager_get
424 @api_ims_inventory_v1.doc('Get deployment manager')
425 @api_ims_inventory_v1.marshal_with(model)
426 def get(self, deploymentManagerID):
427 parser = reqparse.RequestParser()
428 parser.add_argument('profile', location='args')
429 args = parser.parse_args()
431 args.profile if args.profile is not None and args.profile != ''
433 result = ocloud_view.deployment_manager_one(
434 deploymentManagerID, bus.uow, profile)
435 if result is not None:
437 api_ims_inventory_v1.abort(
439 "Deployment manager {} doesn't exist".format(deploymentManagerID))
442 # ---------- Subscriptions ---------- #
443 @api_ims_inventory_v1.route("/subscriptions")
444 class SubscriptionsListRouter(Resource):
446 model = SubscriptionDTO.subscription_get
447 expect = SubscriptionDTO.subscription
448 post_resp = SubscriptionDTO.subscription_post_resp
450 @api_ims_inventory_v1.doc('List subscriptions')
451 @api_ims_inventory_v1.marshal_list_with(model)
452 @api_ims_inventory_v1.param(
454 'Page number of the results to fetch. Default: 1',
455 _in='query', default=1)
456 @api_ims_inventory_v1.param(
458 'Set any value for show all fields. This value will cover "fields" ' +
461 @api_ims_inventory_v1.param(
463 'Set fields to show, split by comma, "/" for parent and children.' +
464 ' Like "name,parent/children". This value will cover' +
465 ' "exculde_fields".',
467 @api_ims_inventory_v1.param(
469 'Set fields to exclude showing, split by comma, "/" for parent and ' +
470 'children. Like "name,parent/children". This value will cover ' +
471 '"exclude_default".',
473 @api_ims_inventory_v1.param(
475 'Exclude showing all default fields, Set "true" to enable.',
477 @api_ims_inventory_v1.param(
479 'Filter of the query.',
482 parser = reqparse.RequestParser()
483 parser.add_argument(PAGE_PARAM, location='args')
484 parser.add_argument('filter', location='args')
485 args = parser.parse_args()
487 if args.nextpage_opaque_marker is not None:
488 kwargs['page'] = args.nextpage_opaque_marker
489 kwargs['filter'] = args.filter if args.filter is not None else ''
491 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
492 return link_header(request.full_path, ret)
494 @api_ims_inventory_v1.doc('Create a subscription')
495 @api_ims_inventory_v1.expect(expect)
496 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
498 data = api_ims_inventory_v1.payload
499 result = ocloud_view.subscription_create(data, bus.uow)
503 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
504 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
505 @api_ims_inventory_v1.response(404, 'Subscription not found')
506 class SubscriptionGetDelRouter(Resource):
508 model = SubscriptionDTO.subscription_get
510 @api_ims_inventory_v1.doc('Get subscription by ID')
511 @api_ims_inventory_v1.marshal_with(model)
512 @api_ims_inventory_v1.param(
514 'Set any value for show all fields. This value will cover "fields" ' +
517 @api_ims_inventory_v1.param(
519 'Set fields to show, split by comma, "/" for parent and children.' +
520 ' Like "name,parent/children". This value will cover' +
521 ' "exculde_fields".',
523 @api_ims_inventory_v1.param(
525 'Set fields to exclude showing, split by comma, "/" for parent and ' +
526 'children. Like "name,parent/children". This value will cover ' +
527 '"exclude_default".',
529 @api_ims_inventory_v1.param(
531 'Exclude showing all default fields, Set "true" to enable.',
533 def get(self, subscriptionID):
534 result = ocloud_view.subscription_one(
535 subscriptionID, bus.uow)
536 if result is not None:
538 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
541 @api_ims_inventory_v1.doc('Delete subscription by ID')
542 @api_ims_inventory_v1.response(204, 'Subscription deleted')
543 def delete(self, subscriptionID):
544 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)