Add registration the SMO's CRUD interface in IMS
[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 flask_restx import Resource
16
17 from o2ims.views import ocloud_view, api_ims_inventory_v1
18 from o2common.config import config
19 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
20     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO,\
21     RegistrationDTO
22
23
24 apibase = config.get_o2ims_api_base()
25
26
27 # ----------  OClouds ---------- #
28 @api_ims_inventory_v1.route("/")
29 @api_ims_inventory_v1.response(404, 'oCloud not found')
30 class OcloudsListRouter(Resource):
31     """Ocloud get endpoint
32     O2 interface ocloud endpoint
33     """
34
35     ocloud_get = OcloudDTO.ocloud
36
37     @api_ims_inventory_v1.marshal_with(ocloud_get)
38     def get(self):
39         res = ocloud_view.oclouds(bus.uow)
40         if len(res) > 0:
41             return res[0]
42         api_ims_inventory_v1.abort(
43             404, "oCloud doesn't exist")
44
45
46 # ----------  ResourceTypes ---------- #
47 @api_ims_inventory_v1.route("/resourceTypes")
48 class ResourceTypesListRouter(Resource):
49
50     model = ResourceTypeDTO.resource_type_get
51
52     @api_ims_inventory_v1.marshal_list_with(model)
53     def get(self):
54         return ocloud_view.resource_types(bus.uow)
55
56
57 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
58 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
59 @api_ims_inventory_v1.response(404, 'Resource type not found')
60 class ResourceTypeGetRouter(Resource):
61
62     model = ResourceTypeDTO.resource_type_get
63
64     @api_ims_inventory_v1.doc('Get resource type')
65     @api_ims_inventory_v1.marshal_with(model)
66     def get(self, resourceTypeID):
67         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
68         if result is not None:
69             return result
70         api_ims_inventory_v1.abort(
71             404, "Resource type {} doesn't exist".format(resourceTypeID))
72
73
74 # ----------  ResourcePools ---------- #
75 @api_ims_inventory_v1.route("/resourcePools")
76 class ResourcePoolsListRouter(Resource):
77
78     model = ResourcePoolDTO.resource_pool_get
79
80     @api_ims_inventory_v1.marshal_list_with(model)
81     def get(self):
82         return ocloud_view.resource_pools(bus.uow)
83
84
85 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
86 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
87 @api_ims_inventory_v1.response(404, 'Resource pool not found')
88 class ResourcePoolGetRouter(Resource):
89
90     model = ResourcePoolDTO.resource_pool_get
91
92     @api_ims_inventory_v1.doc('Get resource pool')
93     @api_ims_inventory_v1.marshal_with(model)
94     def get(self, resourcePoolID):
95         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
96         if result is not None:
97             return result
98         api_ims_inventory_v1.abort(
99             404, "Resource pool {} doesn't exist".format(resourcePoolID))
100
101
102 # ----------  Resources ---------- #
103 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
104 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
105 class ResourcesListRouter(Resource):
106
107     model = ResourceDTO.resource_list
108
109     @api_ims_inventory_v1.marshal_list_with(model)
110     def get(self, resourcePoolID):
111         return ocloud_view.resources(resourcePoolID, bus.uow)
112
113
114 @api_ims_inventory_v1.route(
115     "/resourcePools/<resourcePoolID>/resources/<resourceID>")
116 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
117 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
118 @api_ims_inventory_v1.response(404, 'Resource not found')
119 class ResourceGetRouter(Resource):
120
121     # dto = ResourceDTO()
122     # model = dto.get_resource_get()
123     model = ResourceDTO.recursive_resource_mapping()
124
125     @api_ims_inventory_v1.doc('Get resource')
126     @api_ims_inventory_v1.marshal_with(model)
127     def get(self, resourcePoolID, resourceID):
128         result = ocloud_view.resource_one(resourceID, bus.uow)
129         if result is not None:
130             return result
131         api_ims_inventory_v1.abort(
132             404, "Resource {} doesn't exist".format(resourceID))
133
134
135 # ----------  DeploymentManagers ---------- #
136 @api_ims_inventory_v1.route("/deploymentManagers")
137 class DeploymentManagersListRouter(Resource):
138
139     model = DeploymentManagerDTO.deployment_manager_get
140
141     @api_ims_inventory_v1.marshal_list_with(model)
142     def get(self):
143         return ocloud_view.deployment_managers(bus.uow)
144
145
146 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
147 @api_ims_inventory_v1.param('deploymentManagerID',
148                             'ID of the deployment manager')
149 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
150 class DeploymentManagerGetRouter(Resource):
151
152     model = DeploymentManagerDTO.deployment_manager_get
153
154     @api_ims_inventory_v1.doc('Get deployment manager')
155     @api_ims_inventory_v1.marshal_with(model)
156     def get(self, deploymentManagerID):
157         result = ocloud_view.deployment_manager_one(
158             deploymentManagerID, bus.uow)
159         if result is not None:
160             return result
161         api_ims_inventory_v1.abort(
162             404,
163             "Deployment manager {} doesn't exist".format(deploymentManagerID))
164
165
166 # ----------  Subscriptions ---------- #
167 @api_ims_inventory_v1.route("/subscriptions")
168 class SubscriptionsListRouter(Resource):
169
170     model = SubscriptionDTO.subscription_get
171     expect = SubscriptionDTO.subscription
172     post_resp = SubscriptionDTO.subscription_post_resp
173
174     @api_ims_inventory_v1.doc('List subscriptions')
175     @api_ims_inventory_v1.marshal_list_with(model)
176     def get(self):
177         return ocloud_view.subscriptions(bus.uow)
178
179     @api_ims_inventory_v1.doc('Create a subscription')
180     @api_ims_inventory_v1.expect(expect)
181     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
182     def post(self):
183         data = api_ims_inventory_v1.payload
184         result = ocloud_view.subscription_create(data, bus.uow)
185         return result, 201
186
187
188 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
189 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
190 @api_ims_inventory_v1.response(404, 'Subscription not found')
191 class SubscriptionGetDelRouter(Resource):
192
193     model = SubscriptionDTO.subscription_get
194
195     @api_ims_inventory_v1.doc('Get subscription by ID')
196     @api_ims_inventory_v1.marshal_with(model)
197     def get(self, subscriptionID):
198         result = ocloud_view.subscription_one(
199             subscriptionID, bus.uow)
200         if result is not None:
201             return result
202         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
203             subscriptionID))
204
205     @api_ims_inventory_v1.doc('Delete subscription by ID')
206     @api_ims_inventory_v1.response(204, 'Subscription deleted')
207     def delete(self, subscriptionID):
208         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
209         return result, 204
210
211
212 # ----------  Registration ---------- #
213 @api_ims_inventory_v1.route("/registrations")
214 class RegistrationListRouter(Resource):
215
216     model = RegistrationDTO.registration_get
217     expect = RegistrationDTO.registration
218     post_resp = RegistrationDTO.registration_post_resp
219
220     @api_ims_inventory_v1.doc('List registrations')
221     @api_ims_inventory_v1.marshal_list_with(model)
222     def get(self):
223         return ocloud_view.registrations(bus.uow)
224
225     @api_ims_inventory_v1.doc('Create a registration')
226     @api_ims_inventory_v1.expect(expect)
227     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
228     def post(self):
229         data = api_ims_inventory_v1.payload
230         result = ocloud_view.registration_create(data, bus.uow)
231         return result, 201
232
233
234 @api_ims_inventory_v1.route("/registrations/<registrationID>")
235 @api_ims_inventory_v1.param('registrationID', 'ID of the registration')
236 @api_ims_inventory_v1.response(404, 'Registration not found')
237 class RegistrationGetDelRouter(Resource):
238
239     model = RegistrationDTO.registration_get
240
241     @api_ims_inventory_v1.doc('Get registration by ID')
242     @api_ims_inventory_v1.marshal_with(model)
243     def get(self, registrationID):
244         result = ocloud_view.registration_one(
245             registrationID, bus.uow)
246         if result is not None:
247             return result
248         api_ims_inventory_v1.abort(404, "Registration {} doesn't exist".format(
249             registrationID))
250
251     @api_ims_inventory_v1.doc('Delete registration by ID')
252     @api_ims_inventory_v1.response(204, 'Registration deleted')
253     def delete(self, registrationID):
254         result = ocloud_view.registration_delete(registrationID, bus.uow)
255         return result, 204
256
257
258 def configure_namespace(app, bus_new):
259
260     # Set global bus for resource
261     global bus
262     bus = bus_new
263
264     app.add_namespace(api_ims_inventory_v1, path=apibase)