Fix subscription get response
[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 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
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(bus.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(bus.uow)
56
57
58 @api.route("/resourceTypes/<resourceTypeID>")
59 @api.param('resourceTypeID', 'ID of the resource type')
60 @api.response(404, 'Resource type not found')
61 class ResourceTypeGetRouter(Resource):
62
63     model = ResourceTypeDTO.resource_type_get
64
65     @api.doc('Get resource type')
66     @api.marshal_with(model)
67     def get(self, resourceTypeID):
68         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
69         if result is not None:
70             return result
71         api.abort(
72             404, "Resource type {} doesn't exist".format(resourceTypeID))
73
74
75 # ----------  ResourcePools ---------- #
76 @api.route("/resourcePools")
77 class ResourcePoolsListRouter(Resource):
78
79     model = ResourcePoolDTO.resource_pool_get
80
81     @api.marshal_list_with(model)
82     def get(self):
83         return ocloud_view.resource_pools(bus.uow)
84
85
86 @api.route("/resourcePools/<resourcePoolID>")
87 @api.param('resourcePoolID', 'ID of the resource pool')
88 @api.response(404, 'Resource pool not found')
89 class ResourcePoolGetRouter(Resource):
90
91     model = ResourcePoolDTO.resource_pool_get
92
93     @api.doc('Get resource pool')
94     @api.marshal_with(model)
95     def get(self, resourcePoolID):
96         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
97         if result is not None:
98             return result
99         api.abort(
100             404, "Resource pool {} doesn't exist".format(resourcePoolID))
101
102
103 # ----------  Resources ---------- #
104 @api.route("/resourcePools/<resourcePoolID>/resources")
105 @api.param('resourcePoolID', 'ID of the resource pool')
106 class ResourcesListRouter(Resource):
107
108     model = ResourceDTO.resource_list
109
110     @api.marshal_list_with(model)
111     def get(self, resourcePoolID):
112         return ocloud_view.resources(resourcePoolID, bus.uow)
113
114
115 @api.route("/resourcePools/<resourcePoolID>/resources/<resourceID>")
116 @api.param('resourcePoolID', 'ID of the resource pool')
117 @api.param('resourceID', 'ID of the resource')
118 @api.response(404, 'Resource not found')
119 class ResourceGetRouter(Resource):
120
121     model = ResourceDTO.resource_get
122
123     @api.doc('Get resource')
124     @api.marshal_with(model)
125     def get(self, resourcePoolID, resourceID):
126         result = ocloud_view.resource_one(resourceID, bus.uow)
127         if result is not None:
128             return result
129         api.abort(404, "Resource {} doesn't exist".format(resourceID))
130
131
132 # ----------  DeploymentManagers ---------- #
133 @api.route("/deploymentManagers")
134 class DeploymentManagersListRouter(Resource):
135
136     model = DeploymentManagerDTO.deployment_manager_get
137
138     @api.marshal_list_with(model)
139     def get(self):
140         return ocloud_view.deployment_managers(bus.uow)
141
142
143 @api.route("/deploymentManagers/<deploymentManagerID>")
144 @api.param('deploymentManagerID', 'ID of the deployment manager')
145 @api.response(404, 'Deployment manager not found')
146 class DeploymentManagerGetRouter(Resource):
147
148     model = DeploymentManagerDTO.deployment_manager_get
149
150     @api.doc('Get deployment manager')
151     @api.marshal_with(model)
152     def get(self, deploymentManagerID):
153         result = ocloud_view.deployment_manager_one(
154             deploymentManagerID, bus.uow)
155         if result is not None:
156             return result
157         api.abort(404, "Deployment manager {} doesn't exist".format(
158             deploymentManagerID))
159
160
161 # ----------  Subscriptions ---------- #
162 @api.route("/subscriptions")
163 class SubscriptionsListRouter(Resource):
164
165     model = SubscriptionDTO.subscription_get
166     expect = SubscriptionDTO.subscription
167     post_resp = SubscriptionDTO.subscription_post_resp
168
169     @api.doc('List subscriptions')
170     @api.marshal_list_with(model)
171     def get(self):
172         return ocloud_view.subscriptions(bus.uow)
173
174     @api.doc('Create a subscription')
175     @api.expect(expect)
176     @api.marshal_with(post_resp, code=201)
177     def post(self):
178         data = api.payload
179         sub_uuid = str(uuid.uuid4())
180         subscription = Subscription(
181             sub_uuid, data['callback'], data['consumerSubscriptionId'],
182             data['filter'])
183         ocloud_view.subscription_create(subscription, bus.uow)
184         return {"subscriptionId": sub_uuid}, 201
185
186
187 @api.route("/subscriptions/<subscriptionID>")
188 @api.param('subscriptionID', 'ID of the subscription')
189 @api.response(404, 'Subscription not found')
190 class SubscriptionGetDelRouter(Resource):
191
192     model = SubscriptionDTO.subscription_get
193
194     @api.doc('Get subscription by ID')
195     @api.marshal_with(model)
196     def get(self, subscriptionID):
197         result = ocloud_view.subscription_one(
198             subscriptionID, bus.uow)
199         if result is not None:
200             return result
201         api.abort(404, "Subscription {} doesn't exist".format(
202             subscriptionID))
203
204     @api.doc('Delete subscription by ID')
205     @api.response(204, 'Subscription deleted')
206     def delete(self, subscriptionID):
207         with bus.uow:
208             bus.uow.subscriptions.delete(subscriptionID)
209             bus.uow.commit()
210         return '', 204
211
212
213 def configure_namespace(app, bus_new):
214
215     # Set global bus for resource
216     global bus
217     bus = bus_new
218
219     api_v1 = api
220     app.add_namespace(api_v1, path=apibase)