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_restx import Resource
16 from flask_restx import reqparse
18 from o2common.service.messagebus import MessageBus
19 from o2ims.views import ocloud_view
20 from o2ims.views.api_ns import api_ims_inventory_v1
21 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
22 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
28 def configure_api_route():
29 # Set global bus for resource
31 bus = MessageBus.get_instance()
34 # ---------- OClouds ---------- #
35 @api_ims_inventory_v1.route("/")
36 @api_ims_inventory_v1.response(404, 'oCloud not found')
37 class OcloudsListRouter(Resource):
38 """Ocloud get endpoint
39 O2 interface ocloud endpoint
42 ocloud_get = OcloudDTO.ocloud
44 @api_ims_inventory_v1.marshal_with(ocloud_get)
46 res = ocloud_view.oclouds(bus.uow)
49 api_ims_inventory_v1.abort(
50 404, "oCloud doesn't exist")
53 # ---------- ResourceTypes ---------- #
54 @api_ims_inventory_v1.route("/resourceTypes")
55 class ResourceTypesListRouter(Resource):
57 model = ResourceTypeDTO.resource_type_get
59 @api_ims_inventory_v1.marshal_list_with(model)
61 return ocloud_view.resource_types(bus.uow)
64 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
65 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
66 @api_ims_inventory_v1.response(404, 'Resource type not found')
67 class ResourceTypeGetRouter(Resource):
69 model = ResourceTypeDTO.resource_type_get
71 @api_ims_inventory_v1.doc('Get resource type')
72 @api_ims_inventory_v1.marshal_with(model)
73 def get(self, resourceTypeID):
74 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
75 if result is not None:
77 api_ims_inventory_v1.abort(
78 404, "Resource type {} doesn't exist".format(resourceTypeID))
81 # ---------- ResourcePools ---------- #
82 @api_ims_inventory_v1.route("/resourcePools")
83 class ResourcePoolsListRouter(Resource):
85 model = ResourcePoolDTO.resource_pool_get
87 @api_ims_inventory_v1.marshal_list_with(model)
89 return ocloud_view.resource_pools(bus.uow)
92 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
93 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
94 @api_ims_inventory_v1.response(404, 'Resource pool not found')
95 class ResourcePoolGetRouter(Resource):
97 model = ResourcePoolDTO.resource_pool_get
99 @api_ims_inventory_v1.doc('Get resource pool')
100 @api_ims_inventory_v1.marshal_with(model)
101 def get(self, resourcePoolID):
102 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
103 if result is not None:
105 api_ims_inventory_v1.abort(
106 404, "Resource pool {} doesn't exist".format(resourcePoolID))
109 # ---------- Resources ---------- #
110 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
111 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
112 @api_ims_inventory_v1.param('resourceTypeName', 'filter resource type',
114 @api_ims_inventory_v1.param('parentId', 'filter parentId',
116 class ResourcesListRouter(Resource):
118 model = ResourceDTO.resource_list
120 @api_ims_inventory_v1.marshal_list_with(model)
121 def get(self, resourcePoolID):
122 parser = reqparse.RequestParser()
123 parser.add_argument('resourceTypeName', location='args')
124 parser.add_argument('parentId', location='args')
125 args = parser.parse_args()
127 if args.resourceTypeName is not None:
128 kwargs['resourceTypeName'] = args.resourceTypeName
129 if args.parentId is not None:
130 kwargs['parentId'] = args.parentId
131 return ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
134 @api_ims_inventory_v1.route(
135 "/resourcePools/<resourcePoolID>/resources/<resourceID>")
136 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
137 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
138 @api_ims_inventory_v1.response(404, 'Resource not found')
139 class ResourceGetRouter(Resource):
141 # dto = ResourceDTO()
142 # model = dto.get_resource_get()
143 model = ResourceDTO.recursive_resource_mapping()
145 @api_ims_inventory_v1.doc('Get resource')
146 @api_ims_inventory_v1.marshal_with(model)
147 def get(self, resourcePoolID, resourceID):
148 result = ocloud_view.resource_one(resourceID, bus.uow)
149 if result is not None:
151 api_ims_inventory_v1.abort(
152 404, "Resource {} doesn't exist".format(resourceID))
155 # ---------- DeploymentManagers ---------- #
156 @api_ims_inventory_v1.route("/deploymentManagers")
157 class DeploymentManagersListRouter(Resource):
159 model = DeploymentManagerDTO.deployment_manager_get
161 @api_ims_inventory_v1.marshal_list_with(model)
163 return ocloud_view.deployment_managers(bus.uow)
166 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
167 @api_ims_inventory_v1.param('deploymentManagerID',
168 'ID of the deployment manager')
169 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
170 class DeploymentManagerGetRouter(Resource):
172 model = DeploymentManagerDTO.deployment_manager_get
174 @api_ims_inventory_v1.doc('Get deployment manager')
175 @api_ims_inventory_v1.marshal_with(model)
176 def get(self, deploymentManagerID):
177 result = ocloud_view.deployment_manager_one(
178 deploymentManagerID, bus.uow)
179 if result is not None:
181 api_ims_inventory_v1.abort(
183 "Deployment manager {} doesn't exist".format(deploymentManagerID))
186 # ---------- Subscriptions ---------- #
187 @api_ims_inventory_v1.route("/subscriptions")
188 class SubscriptionsListRouter(Resource):
190 model = SubscriptionDTO.subscription_get
191 expect = SubscriptionDTO.subscription
192 post_resp = SubscriptionDTO.subscription_post_resp
194 @api_ims_inventory_v1.doc('List subscriptions')
195 @api_ims_inventory_v1.marshal_list_with(model)
197 return ocloud_view.subscriptions(bus.uow)
199 @api_ims_inventory_v1.doc('Create a subscription')
200 @api_ims_inventory_v1.expect(expect)
201 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
203 data = api_ims_inventory_v1.payload
204 result = ocloud_view.subscription_create(data, bus.uow)
208 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
209 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
210 @api_ims_inventory_v1.response(404, 'Subscription not found')
211 class SubscriptionGetDelRouter(Resource):
213 model = SubscriptionDTO.subscription_get
215 @api_ims_inventory_v1.doc('Get subscription by ID')
216 @api_ims_inventory_v1.marshal_with(model)
217 def get(self, subscriptionID):
218 result = ocloud_view.subscription_one(
219 subscriptionID, bus.uow)
220 if result is not None:
222 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
225 @api_ims_inventory_v1.doc('Delete subscription by ID')
226 @api_ims_inventory_v1.response(204, 'Subscription deleted')
227 def delete(self, subscriptionID):
228 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)