92cfd1caabbe355e659483dd67f7f8c50d3d91a1
[pti/o2.git] / o2ims / views / ocloud_route.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # from operator import sub
16 import uuid
17 # from re import sub
18 from flask_restx import Resource
19
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
25
26 apibase = config.get_o2ims_api_base()
27 # api = Namespace("O2IMS", description='IMS')
28
29
30 # ----------  OClouds ---------- #
31 api_ocloud = OcloudDTO.api
32
33
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
39     """
40
41     ocloud_get = OcloudDTO.ocloud
42
43     @api_ocloud.marshal_with(ocloud_get)
44     def get(self):
45         res = ocloud_view.oclouds(uow)
46         if len(res) > 0:
47             return res[0]
48         api_rt.abort(
49             404, "oCloud doesn't exist")
50
51
52 # ----------  ResourceTypes ---------- #
53 api_rt = ResourceTypeDTO.api
54
55
56 @api_rt.route("/resourceTypes")
57 class ResourceTypesListRouter(Resource):
58
59     model = ResourceTypeDTO.resource_type_get
60
61     @api_rt.marshal_list_with(model)
62     def get(self):
63         return ocloud_view.resource_types(uow)
64
65     # @api.doc(response={405: 'Method Not Allowed'})
66     # def post(self):
67     #     api.abort(405)
68
69     # @api.doc(response={405: 'Method Not Allowed'})
70     # def put(self):
71     #     api.abort(405)
72
73     # @api.doc(response={405: 'Method Not Allowed'})
74     # def patch(self):
75     #     api.abort(405)
76
77     # @api.doc(response={405: 'Method Not Allowed'})
78     # def delete(self):
79     #     api.abort(405)
80
81
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):
86
87     model = ResourceTypeDTO.resource_type_get
88
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:
94             return result
95         api_rt.abort(
96             404, "Resource type {} doesn't exist".format(resourceTypeID))
97
98
99 # ----------  ResourcePools ---------- #
100 api_rp = ResourcePoolDTO.api
101
102
103 @api_rp.route("/resourcePools")
104 class ResourcePoolsListRouter(Resource):
105
106     model = ResourcePoolDTO.resource_pool_get
107
108     @api_rp.marshal_list_with(model)
109     def get(self):
110         return ocloud_view.resource_pools(uow)
111
112
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):
117
118     model = ResourcePoolDTO.resource_pool_get
119
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:
125             return result
126         api_rp.abort(
127             404, "Resource pool {} doesn't exist".format(resourcePoolID))
128
129
130 # ----------  Resources ---------- #
131 api_res = ResourceDTO.api
132
133
134 @api_res.route("/resourcePools/<resourcePoolID>/resources")
135 @api_res.param('resourcePoolID', 'ID of the resource pool')
136 class ResourcesListRouter(Resource):
137
138     model = ResourceDTO.resource_list
139
140     @api_res.marshal_list_with(model)
141     def get(self, resourcePoolID):
142         return ocloud_view.resources(resourcePoolID, uow)
143
144
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):
150
151     model = ResourceDTO.resource_get
152
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:
158             return result
159         api_res.abort(404, "Resource {} doesn't exist".format(resourceID))
160
161
162 # ----------  DeploymentManagers ---------- #
163 api_dm = DeploymentManagerDTO.api
164
165
166 @api_dm.route("/deploymentManagers")
167 class DeploymentManagersListRouter(Resource):
168
169     model = DeploymentManagerDTO.deployment_manager_get
170
171     @api_dm.marshal_list_with(model)
172     def get(self):
173         return ocloud_view.deployment_managers(uow)
174
175
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):
180
181     model = DeploymentManagerDTO.deployment_manager_get
182
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:
189             return result
190         api_dm.abort(404, "Deployment manager {} doesn't exist".format(
191             deploymentManagerID))
192
193
194 # ----------  Subscriptions ---------- #
195 api_sub = SubscriptionDTO.api
196
197
198 @api_sub.route("/subscriptions")
199 class SubscriptionsListRouter(Resource):
200
201     model = SubscriptionDTO.subscription_get
202     expect = SubscriptionDTO.subscription
203     post_resp = SubscriptionDTO.subscription_post_resp
204
205     @api_sub.doc('List subscriptions')
206     @api_sub.marshal_list_with(model)
207     def get(self):
208         return ocloud_view.subscriptions(uow)
209
210     @api_sub.doc('Create a subscription')
211     @api_sub.expect(expect)
212     @api_sub.marshal_with(post_resp, code=201)
213     def post(self):
214         data = api_sub.payload
215         sub_uuid = str(uuid.uuid4())
216         subscription = Subscription(
217             sub_uuid, data['callback'], data['consumerSubscriptionId'],
218             data['filter'])
219         ocloud_view.subscription_create(subscription, uow)
220         return {"subscriptionId": sub_uuid}, 201
221
222
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):
227
228     model = DeploymentManagerDTO.deployment_manager_get
229
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(
234             subscriptionID, uow)
235         if result is not None:
236             return result
237         api_sub.abort(404, "Subscription {} doesn't exist".format(
238             subscriptionID))
239
240     @api_sub.doc('Delete subscription by ID')
241     @api_sub.response(204, 'Subscription deleted')
242     def delete(self, subscriptionID):
243         with uow:
244             uow.subscriptions.delete(subscriptionID)
245             uow.commit()
246         return '', 204
247
248
249 def configure_namespace(app, bus):
250
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)
257
258     # Set global uow
259     global uow
260     uow = bus.uow