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 operator import sub
18 from flask_restx import Resource
20 from o2ims import config
21 from o2ims.views import ocloud_view
22 from o2ims.domain.ocloud import Subscription
23 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
24 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
26 apibase = config.get_o2ims_api_base()
27 # api = Namespace("O2IMS", description='IMS')
30 # ---------- OClouds ---------- #
31 api_ocloud = OcloudDTO.api
34 @api_ocloud.route("/")
35 @api_ocloud.response(404, 'oCloud not found')
36 class OcloudsListRouter(Resource):
37 """Ocloud get endpoint
38 O2 interface ocloud endpoint
41 ocloud_get = OcloudDTO.ocloud
43 @api_ocloud.marshal_with(ocloud_get)
45 res = ocloud_view.oclouds(uow)
49 404, "oCloud doesn't exist")
52 # ---------- ResourceTypes ---------- #
53 api_rt = ResourceTypeDTO.api
56 @api_rt.route("/resourceTypes")
57 class ResourceTypesListRouter(Resource):
59 model = ResourceTypeDTO.resource_type_get
61 @api_rt.marshal_list_with(model)
63 return ocloud_view.resource_types(uow)
65 # @api.doc(response={405: 'Method Not Allowed'})
69 # @api.doc(response={405: 'Method Not Allowed'})
73 # @api.doc(response={405: 'Method Not Allowed'})
77 # @api.doc(response={405: 'Method Not Allowed'})
82 @api_rt.route("/resourceTypes/<resourceTypeID>")
83 @api_rt.param('resourceTypeID', 'ID of the resource type')
84 @api_rt.response(404, 'Resource type not found')
85 class ResourceTypeGetRouter(Resource):
87 model = ResourceTypeDTO.resource_type_get
89 @api_rt.doc('Get resource type')
90 @api_rt.marshal_with(model)
91 def get(self, resourceTypeID):
92 result = ocloud_view.resource_type_one(resourceTypeID, uow)
93 if result is not None:
96 404, "Resource type {} doesn't exist".format(resourceTypeID))
99 # ---------- ResourcePools ---------- #
100 api_rp = ResourcePoolDTO.api
103 @api_rp.route("/resourcePools")
104 class ResourcePoolsListRouter(Resource):
106 model = ResourcePoolDTO.resource_pool_get
108 @api_rp.marshal_list_with(model)
110 return ocloud_view.resource_pools(uow)
113 @api_rp.route("/resourcePools/<resourcePoolID>")
114 @api_rp.param('resourcePoolID', 'ID of the resource pool')
115 @api_rp.response(404, 'Resource pool not found')
116 class ResourcePoolGetRouter(Resource):
118 model = ResourcePoolDTO.resource_pool_get
120 @api_rp.doc('Get resource pool')
121 @api_rp.marshal_with(model)
122 def get(self, resourcePoolID):
123 result = ocloud_view.resource_pool_one(resourcePoolID, uow)
124 if result is not None:
127 404, "Resource pool {} doesn't exist".format(resourcePoolID))
130 # ---------- Resources ---------- #
131 api_res = ResourceDTO.api
134 @api_res.route("/resourcePools/<resourcePoolID>/resources")
135 @api_res.param('resourcePoolID', 'ID of the resource pool')
136 class ResourcesListRouter(Resource):
138 model = ResourceDTO.resource_list
140 @api_res.marshal_list_with(model)
141 def get(self, resourcePoolID):
142 return ocloud_view.resources(resourcePoolID, uow)
145 @api_res.route("/resourcePools/<resourcePoolID>/resources/<resourceID>")
146 @api_res.param('resourcePoolID', 'ID of the resource pool')
147 @api_res.param('resourceID', 'ID of the resource')
148 @api_res.response(404, 'Resource not found')
149 class ResourceGetRouter(Resource):
151 model = ResourceDTO.resource_get
153 @api_res.doc('Get resource')
154 @api_res.marshal_with(model)
155 def get(self, resourcePoolID, resourceID):
156 result = ocloud_view.resource_one(resourceID, uow)
157 if result is not None:
159 api_res.abort(404, "Resource {} doesn't exist".format(resourceID))
162 # ---------- DeploymentManagers ---------- #
163 api_dm = DeploymentManagerDTO.api
166 @api_dm.route("/deploymentManagers")
167 class DeploymentManagersListRouter(Resource):
169 model = DeploymentManagerDTO.deployment_manager_get
171 @api_dm.marshal_list_with(model)
173 return ocloud_view.deployment_managers(uow)
176 @api_dm.route("/deploymentManagers/<deploymentManagerID>")
177 @api_dm.param('deploymentManagerID', 'ID of the deployment manager')
178 @api_dm.response(404, 'Deployment manager not found')
179 class DeploymentManagerGetRouter(Resource):
181 model = DeploymentManagerDTO.deployment_manager_get
183 @api_dm.doc('Get deployment manager')
184 @api_dm.marshal_with(model)
185 def get(self, deploymentManagerID):
186 result = ocloud_view.deployment_manager_one(
187 deploymentManagerID, uow)
188 if result is not None:
190 api_dm.abort(404, "Deployment manager {} doesn't exist".format(
191 deploymentManagerID))
194 # ---------- Subscriptions ---------- #
195 api_sub = SubscriptionDTO.api
198 @api_sub.route("/subscriptions")
199 class SubscriptionsListRouter(Resource):
201 model = SubscriptionDTO.subscription_get
202 expect = SubscriptionDTO.subscription
203 post_resp = SubscriptionDTO.subscription_post_resp
205 @api_sub.doc('List subscriptions')
206 @api_sub.marshal_list_with(model)
208 return ocloud_view.subscriptions(uow)
210 @api_sub.doc('Create a subscription')
211 @api_sub.expect(expect)
212 @api_sub.marshal_with(post_resp, code=201)
214 data = api_sub.payload
215 sub_uuid = str(uuid.uuid4())
216 subscription = Subscription(
217 sub_uuid, data['callback'], data['consumerSubscriptionId'],
219 ocloud_view.subscription_create(subscription, uow)
220 return {"subscriptionId": sub_uuid}, 201
223 @api_sub.route("/subscriptions/<subscriptionID>")
224 @api_sub.param('subscriptionID', 'ID of the subscription')
225 @api_sub.response(404, 'Subscription not found')
226 class SubscriptionGetDelRouter(Resource):
228 model = DeploymentManagerDTO.deployment_manager_get
230 @api_sub.doc('Get subscription by ID')
231 @api_sub.marshal_with(model)
232 def get(self, subscriptionID):
233 result = ocloud_view.subscription_one(
235 if result is not None:
237 api_sub.abort(404, "Subscription {} doesn't exist".format(
240 @api_sub.doc('Delete subscription by ID')
241 @api_sub.response(204, 'Subscription deleted')
242 def delete(self, subscriptionID):
244 uow.subscriptions.delete(subscriptionID)
249 def configure_namespace(app, bus):
251 app.add_namespace(api_ocloud, path=apibase)
252 app.add_namespace(api_rt, path=apibase)
253 app.add_namespace(api_rp, path=apibase)
254 app.add_namespace(api_res, path=apibase)
255 app.add_namespace(api_dm, path=apibase)
256 app.add_namespace(api_sub, path=apibase)