a6f18471a169c2488c370f3416796b475a7e8520
[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 import uuid
16 from flask_restx import Resource
17
18 from o2ims.views import ocloud_view, api
19 from o2ims import config
20 from o2ims.domain.ocloud import Subscription
21 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
22     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
23
24
25 apibase = config.get_o2ims_api_base()
26
27
28 # ----------  OClouds ---------- #
29 @api.route("/")
30 @api.response(404, 'oCloud not found')
31 class OcloudsListRouter(Resource):
32     """Ocloud get endpoint
33     O2 interface ocloud endpoint
34     """
35
36     ocloud_get = OcloudDTO.ocloud
37
38     @api.marshal_with(ocloud_get)
39     def get(self):
40         res = ocloud_view.oclouds(uow)
41         if len(res) > 0:
42             return res[0]
43         api.abort(
44             404, "oCloud doesn't exist")
45
46
47 # ----------  ResourceTypes ---------- #
48 @api.route("/resourceTypes")
49 class ResourceTypesListRouter(Resource):
50
51     model = ResourceTypeDTO.resource_type_get
52
53     @api.marshal_list_with(model)
54     def get(self):
55         return ocloud_view.resource_types(uow)
56
57     # @api.doc(response={405: 'Method Not Allowed'})
58     # def post(self):
59     #     api.abort(405)
60
61     # @api.doc(response={405: 'Method Not Allowed'})
62     # def put(self):
63     #     api.abort(405)
64
65     # @api.doc(response={405: 'Method Not Allowed'})
66     # def patch(self):
67     #     api.abort(405)
68
69     # @api.doc(response={405: 'Method Not Allowed'})
70     # def delete(self):
71     #     api.abort(405)
72
73
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):
78
79     model = ResourceTypeDTO.resource_type_get
80
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:
86             return result
87         api.abort(
88             404, "Resource type {} doesn't exist".format(resourceTypeID))
89
90
91 # ----------  ResourcePools ---------- #
92 @api.route("/resourcePools")
93 class ResourcePoolsListRouter(Resource):
94
95     model = ResourcePoolDTO.resource_pool_get
96
97     @api.marshal_list_with(model)
98     def get(self):
99         return ocloud_view.resource_pools(uow)
100
101
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):
106
107     model = ResourcePoolDTO.resource_pool_get
108
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:
114             return result
115         api.abort(
116             404, "Resource pool {} doesn't exist".format(resourcePoolID))
117
118
119 # ----------  Resources ---------- #
120 @api.route("/resourcePools/<resourcePoolID>/resources")
121 @api.param('resourcePoolID', 'ID of the resource pool')
122 class ResourcesListRouter(Resource):
123
124     model = ResourceDTO.resource_list
125
126     @api.marshal_list_with(model)
127     def get(self, resourcePoolID):
128         return ocloud_view.resources(resourcePoolID, uow)
129
130
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):
136
137     model = ResourceDTO.resource_get
138
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:
144             return result
145         api.abort(404, "Resource {} doesn't exist".format(resourceID))
146
147
148 # ----------  DeploymentManagers ---------- #
149 @api.route("/deploymentManagers")
150 class DeploymentManagersListRouter(Resource):
151
152     model = DeploymentManagerDTO.deployment_manager_get
153
154     @api.marshal_list_with(model)
155     def get(self):
156         return ocloud_view.deployment_managers(uow)
157
158
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):
163
164     model = DeploymentManagerDTO.deployment_manager_get
165
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:
172             return result
173         api.abort(404, "Deployment manager {} doesn't exist".format(
174             deploymentManagerID))
175
176
177 # ----------  Subscriptions ---------- #
178 @api.route("/subscriptions")
179 class SubscriptionsListRouter(Resource):
180
181     model = SubscriptionDTO.subscription_get
182     expect = SubscriptionDTO.subscription
183     post_resp = SubscriptionDTO.subscription_post_resp
184
185     @api.doc('List subscriptions')
186     @api.marshal_list_with(model)
187     def get(self):
188         return ocloud_view.subscriptions(uow)
189
190     @api.doc('Create a subscription')
191     @api.expect(expect)
192     @api.marshal_with(post_resp, code=201)
193     def post(self):
194         data = api.payload
195         sub_uuid = str(uuid.uuid4())
196         subscription = Subscription(
197             sub_uuid, data['callback'], data['consumerSubscriptionId'],
198             data['filter'])
199         ocloud_view.subscription_create(subscription, uow)
200         return {"subscriptionId": sub_uuid}, 201
201
202
203 @api.route("/subscriptions/<subscriptionID>")
204 @api.param('subscriptionID', 'ID of the subscription')
205 @api.response(404, 'Subscription not found')
206 class SubscriptionGetDelRouter(Resource):
207
208     model = DeploymentManagerDTO.deployment_manager_get
209
210     @api.doc('Get subscription by ID')
211     @api.marshal_with(model)
212     def get(self, subscriptionID):
213         result = ocloud_view.subscription_one(
214             subscriptionID, uow)
215         if result is not None:
216             return result
217         api.abort(404, "Subscription {} doesn't exist".format(
218             subscriptionID))
219
220     @api.doc('Delete subscription by ID')
221     @api.response(204, 'Subscription deleted')
222     def delete(self, subscriptionID):
223         with uow:
224             uow.subscriptions.delete(subscriptionID)
225             uow.commit()
226         return '', 204
227
228
229 def configure_namespace(app, bus):
230
231     api_v1 = api
232     app.add_namespace(api_v1, path=apibase)
233
234     # Set global uow
235     global uow
236     uow = bus.uow