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.
16 from flask_restx import Resource
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
25 apibase = config.get_o2ims_api_base()
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
36 ocloud_get = OcloudDTO.ocloud
38 @api_ims_inventory_v1.marshal_with(ocloud_get)
40 res = ocloud_view.oclouds(bus.uow)
43 api_ims_inventory_v1.abort(
44 404, "oCloud doesn't exist")
47 # ---------- ResourceTypes ---------- #
48 @api_ims_inventory_v1.route("/resourceTypes")
49 class ResourceTypesListRouter(Resource):
51 model = ResourceTypeDTO.resource_type_get
53 @api_ims_inventory_v1.marshal_list_with(model)
55 return ocloud_view.resource_types(bus.uow)
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):
63 model = ResourceTypeDTO.resource_type_get
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:
71 api_ims_inventory_v1.abort(
72 404, "Resource type {} doesn't exist".format(resourceTypeID))
75 # ---------- ResourcePools ---------- #
76 @api_ims_inventory_v1.route("/resourcePools")
77 class ResourcePoolsListRouter(Resource):
79 model = ResourcePoolDTO.resource_pool_get
81 @api_ims_inventory_v1.marshal_list_with(model)
83 return ocloud_view.resource_pools(bus.uow)
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):
91 model = ResourcePoolDTO.resource_pool_get
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:
99 api_ims_inventory_v1.abort(
100 404, "Resource pool {} doesn't exist".format(resourcePoolID))
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):
108 model = ResourceDTO.resource_list
110 @api_ims_inventory_v1.marshal_list_with(model)
111 def get(self, resourcePoolID):
112 return ocloud_view.resources(resourcePoolID, bus.uow)
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):
122 model = ResourceDTO.resource_get
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:
130 api_ims_inventory_v1.abort(
131 404, "Resource {} doesn't exist".format(resourceID))
134 # ---------- DeploymentManagers ---------- #
135 @api_ims_inventory_v1.route("/deploymentManagers")
136 class DeploymentManagersListRouter(Resource):
138 model = DeploymentManagerDTO.deployment_manager_get
140 @api_ims_inventory_v1.marshal_list_with(model)
142 return ocloud_view.deployment_managers(bus.uow)
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):
151 model = DeploymentManagerDTO.deployment_manager_get
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:
160 api_ims_inventory_v1.abort(
162 "Deployment manager {} doesn't exist".format(deploymentManagerID))
165 # ---------- Subscriptions ---------- #
166 @api_ims_inventory_v1.route("/subscriptions")
167 class SubscriptionsListRouter(Resource):
169 model = SubscriptionDTO.subscription_get
170 expect = SubscriptionDTO.subscription
171 post_resp = SubscriptionDTO.subscription_post_resp
173 @api_ims_inventory_v1.doc('List subscriptions')
174 @api_ims_inventory_v1.marshal_list_with(model)
176 return ocloud_view.subscriptions(bus.uow)
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)
182 data = api_ims_inventory_v1.payload
183 sub_uuid = str(uuid.uuid4())
184 subscription = Subscription(
185 sub_uuid, data['callback'], data['consumerSubscriptionId'],
187 ocloud_view.subscription_create(subscription, bus.uow)
188 return {"subscriptionId": sub_uuid}, 201
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):
196 model = SubscriptionDTO.subscription_get
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:
205 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
208 @api_ims_inventory_v1.doc('Delete subscription by ID')
209 @api_ims_inventory_v1.response(204, 'Subscription deleted')
210 def delete(self, subscriptionID):
212 bus.uow.subscriptions.delete(subscriptionID)
217 def configure_namespace(app, bus_new):
219 # Set global bus for resource
223 app.add_namespace(api_ims_inventory_v1, path=apibase)