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.
16 from flask_restx import Resource
18 from o2ims.views import ocloud_view, api
19 from o2common.config import config
20 from o2ims.domain.ocloud import Subscription
21 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
22 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
25 apibase = config.get_o2ims_api_base()
28 # ---------- OClouds ---------- #
30 @api.response(404, 'oCloud not found')
31 class OcloudsListRouter(Resource):
32 """Ocloud get endpoint
33 O2 interface ocloud endpoint
36 ocloud_get = OcloudDTO.ocloud
38 @api.marshal_with(ocloud_get)
40 res = ocloud_view.oclouds(uow)
44 404, "oCloud doesn't exist")
47 # ---------- ResourceTypes ---------- #
48 @api.route("/resourceTypes")
49 class ResourceTypesListRouter(Resource):
51 model = ResourceTypeDTO.resource_type_get
53 @api.marshal_list_with(model)
55 return ocloud_view.resource_types(uow)
57 # @api.doc(response={405: 'Method Not Allowed'})
61 # @api.doc(response={405: 'Method Not Allowed'})
65 # @api.doc(response={405: 'Method Not Allowed'})
69 # @api.doc(response={405: 'Method Not Allowed'})
74 @api.route("/resourceTypes/<resourceTypeID>")
75 @api.param('resourceTypeID', 'ID of the resource type')
76 @api.response(404, 'Resource type not found')
77 class ResourceTypeGetRouter(Resource):
79 model = ResourceTypeDTO.resource_type_get
81 @api.doc('Get resource type')
82 @api.marshal_with(model)
83 def get(self, resourceTypeID):
84 result = ocloud_view.resource_type_one(resourceTypeID, uow)
85 if result is not None:
88 404, "Resource type {} doesn't exist".format(resourceTypeID))
91 # ---------- ResourcePools ---------- #
92 @api.route("/resourcePools")
93 class ResourcePoolsListRouter(Resource):
95 model = ResourcePoolDTO.resource_pool_get
97 @api.marshal_list_with(model)
99 return ocloud_view.resource_pools(uow)
102 @api.route("/resourcePools/<resourcePoolID>")
103 @api.param('resourcePoolID', 'ID of the resource pool')
104 @api.response(404, 'Resource pool not found')
105 class ResourcePoolGetRouter(Resource):
107 model = ResourcePoolDTO.resource_pool_get
109 @api.doc('Get resource pool')
110 @api.marshal_with(model)
111 def get(self, resourcePoolID):
112 result = ocloud_view.resource_pool_one(resourcePoolID, uow)
113 if result is not None:
116 404, "Resource pool {} doesn't exist".format(resourcePoolID))
119 # ---------- Resources ---------- #
120 @api.route("/resourcePools/<resourcePoolID>/resources")
121 @api.param('resourcePoolID', 'ID of the resource pool')
122 class ResourcesListRouter(Resource):
124 model = ResourceDTO.resource_list
126 @api.marshal_list_with(model)
127 def get(self, resourcePoolID):
128 return ocloud_view.resources(resourcePoolID, uow)
131 @api.route("/resourcePools/<resourcePoolID>/resources/<resourceID>")
132 @api.param('resourcePoolID', 'ID of the resource pool')
133 @api.param('resourceID', 'ID of the resource')
134 @api.response(404, 'Resource not found')
135 class ResourceGetRouter(Resource):
137 model = ResourceDTO.resource_get
139 @api.doc('Get resource')
140 @api.marshal_with(model)
141 def get(self, resourcePoolID, resourceID):
142 result = ocloud_view.resource_one(resourceID, uow)
143 if result is not None:
145 api.abort(404, "Resource {} doesn't exist".format(resourceID))
148 # ---------- DeploymentManagers ---------- #
149 @api.route("/deploymentManagers")
150 class DeploymentManagersListRouter(Resource):
152 model = DeploymentManagerDTO.deployment_manager_get
154 @api.marshal_list_with(model)
156 return ocloud_view.deployment_managers(uow)
159 @api.route("/deploymentManagers/<deploymentManagerID>")
160 @api.param('deploymentManagerID', 'ID of the deployment manager')
161 @api.response(404, 'Deployment manager not found')
162 class DeploymentManagerGetRouter(Resource):
164 model = DeploymentManagerDTO.deployment_manager_get
166 @api.doc('Get deployment manager')
167 @api.marshal_with(model)
168 def get(self, deploymentManagerID):
169 result = ocloud_view.deployment_manager_one(
170 deploymentManagerID, uow)
171 if result is not None:
173 api.abort(404, "Deployment manager {} doesn't exist".format(
174 deploymentManagerID))
177 # ---------- Subscriptions ---------- #
178 @api.route("/subscriptions")
179 class SubscriptionsListRouter(Resource):
181 model = SubscriptionDTO.subscription_get
182 expect = SubscriptionDTO.subscription
183 post_resp = SubscriptionDTO.subscription_post_resp
185 @api.doc('List subscriptions')
186 @api.marshal_list_with(model)
188 return ocloud_view.subscriptions(uow)
190 @api.doc('Create a subscription')
192 @api.marshal_with(post_resp, code=201)
195 sub_uuid = str(uuid.uuid4())
196 subscription = Subscription(
197 sub_uuid, data['callback'], data['consumerSubscriptionId'],
199 ocloud_view.subscription_create(subscription, uow)
200 return {"subscriptionId": sub_uuid}, 201
203 @api.route("/subscriptions/<subscriptionID>")
204 @api.param('subscriptionID', 'ID of the subscription')
205 @api.response(404, 'Subscription not found')
206 class SubscriptionGetDelRouter(Resource):
208 model = DeploymentManagerDTO.deployment_manager_get
210 @api.doc('Get subscription by ID')
211 @api.marshal_with(model)
212 def get(self, subscriptionID):
213 result = ocloud_view.subscription_one(
215 if result is not None:
217 api.abort(404, "Subscription {} doesn't exist".format(
220 @api.doc('Delete subscription by ID')
221 @api.response(204, 'Subscription deleted')
222 def delete(self, subscriptionID):
224 uow.subscriptions.delete(subscriptionID)
229 def configure_namespace(app, bus):
232 app.add_namespace(api_v1, path=apibase)