1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from flask_restx import Resource
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,\
24 apibase = config.get_o2ims_api_base()
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
35 ocloud_get = OcloudDTO.ocloud
37 @api_ims_inventory_v1.marshal_with(ocloud_get)
39 res = ocloud_view.oclouds(bus.uow)
42 api_ims_inventory_v1.abort(
43 404, "oCloud doesn't exist")
46 # ---------- ResourceTypes ---------- #
47 @api_ims_inventory_v1.route("/resourceTypes")
48 class ResourceTypesListRouter(Resource):
50 model = ResourceTypeDTO.resource_type_get
52 @api_ims_inventory_v1.marshal_list_with(model)
54 return ocloud_view.resource_types(bus.uow)
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):
62 model = ResourceTypeDTO.resource_type_get
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:
70 api_ims_inventory_v1.abort(
71 404, "Resource type {} doesn't exist".format(resourceTypeID))
74 # ---------- ResourcePools ---------- #
75 @api_ims_inventory_v1.route("/resourcePools")
76 class ResourcePoolsListRouter(Resource):
78 model = ResourcePoolDTO.resource_pool_get
80 @api_ims_inventory_v1.marshal_list_with(model)
82 return ocloud_view.resource_pools(bus.uow)
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):
90 model = ResourcePoolDTO.resource_pool_get
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:
98 api_ims_inventory_v1.abort(
99 404, "Resource pool {} doesn't exist".format(resourcePoolID))
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):
107 model = ResourceDTO.resource_list
109 @api_ims_inventory_v1.marshal_list_with(model)
110 def get(self, resourcePoolID):
111 return ocloud_view.resources(resourcePoolID, bus.uow)
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):
121 # dto = ResourceDTO()
122 # model = dto.get_resource_get()
123 model = ResourceDTO.recursive_resource_mapping()
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:
131 api_ims_inventory_v1.abort(
132 404, "Resource {} doesn't exist".format(resourceID))
135 # ---------- DeploymentManagers ---------- #
136 @api_ims_inventory_v1.route("/deploymentManagers")
137 class DeploymentManagersListRouter(Resource):
139 model = DeploymentManagerDTO.deployment_manager_get
141 @api_ims_inventory_v1.marshal_list_with(model)
143 return ocloud_view.deployment_managers(bus.uow)
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):
152 model = DeploymentManagerDTO.deployment_manager_get
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:
161 api_ims_inventory_v1.abort(
163 "Deployment manager {} doesn't exist".format(deploymentManagerID))
166 # ---------- Subscriptions ---------- #
167 @api_ims_inventory_v1.route("/subscriptions")
168 class SubscriptionsListRouter(Resource):
170 model = SubscriptionDTO.subscription_get
171 expect = SubscriptionDTO.subscription
172 post_resp = SubscriptionDTO.subscription_post_resp
174 @api_ims_inventory_v1.doc('List subscriptions')
175 @api_ims_inventory_v1.marshal_list_with(model)
177 return ocloud_view.subscriptions(bus.uow)
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)
183 data = api_ims_inventory_v1.payload
184 result = ocloud_view.subscription_create(data, bus.uow)
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):
193 model = SubscriptionDTO.subscription_get
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:
202 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
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)
212 # ---------- Registration ---------- #
213 @api_ims_inventory_v1.route("/registrations")
214 class RegistrationListRouter(Resource):
216 model = RegistrationDTO.registration_get
217 expect = RegistrationDTO.registration
218 post_resp = RegistrationDTO.registration_post_resp
220 @api_ims_inventory_v1.doc('List registrations')
221 @api_ims_inventory_v1.marshal_list_with(model)
223 return ocloud_view.registrations(bus.uow)
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)
229 data = api_ims_inventory_v1.payload
230 result = ocloud_view.registration_create(data, bus)
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):
239 model = RegistrationDTO.registration_get
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:
248 api_ims_inventory_v1.abort(404, "Registration {} doesn't exist".format(
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)
258 def configure_namespace(app, bus_new):
260 # Set global bus for resource
264 app.add_namespace(api_ims_inventory_v1, path=apibase)