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