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 o2common.service.messagebus import MessageBus
18 from o2ims.views import ocloud_view
19 from o2ims.views.api_ns import api_ims_inventory_v1
20 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
21 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
24 def configure_api_route():
25 # Set global bus for resource
27 bus = MessageBus.get_instance()
30 # ---------- OClouds ---------- #
31 @api_ims_inventory_v1.route("/")
32 @api_ims_inventory_v1.response(404, 'oCloud not found')
33 class OcloudsListRouter(Resource):
34 """Ocloud get endpoint
35 O2 interface ocloud endpoint
38 ocloud_get = OcloudDTO.ocloud
40 @api_ims_inventory_v1.marshal_with(ocloud_get)
42 res = ocloud_view.oclouds(bus.uow)
45 api_ims_inventory_v1.abort(
46 404, "oCloud doesn't exist")
49 # ---------- ResourceTypes ---------- #
50 @api_ims_inventory_v1.route("/resourceTypes")
51 class ResourceTypesListRouter(Resource):
53 model = ResourceTypeDTO.resource_type_get
55 @api_ims_inventory_v1.marshal_list_with(model)
57 return ocloud_view.resource_types(bus.uow)
60 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
61 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
62 @api_ims_inventory_v1.response(404, 'Resource type not found')
63 class ResourceTypeGetRouter(Resource):
65 model = ResourceTypeDTO.resource_type_get
67 @api_ims_inventory_v1.doc('Get resource type')
68 @api_ims_inventory_v1.marshal_with(model)
69 def get(self, resourceTypeID):
70 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
71 if result is not None:
73 api_ims_inventory_v1.abort(
74 404, "Resource type {} doesn't exist".format(resourceTypeID))
77 # ---------- ResourcePools ---------- #
78 @api_ims_inventory_v1.route("/resourcePools")
79 class ResourcePoolsListRouter(Resource):
81 model = ResourcePoolDTO.resource_pool_get
83 @api_ims_inventory_v1.marshal_list_with(model)
85 return ocloud_view.resource_pools(bus.uow)
88 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
89 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
90 @api_ims_inventory_v1.response(404, 'Resource pool not found')
91 class ResourcePoolGetRouter(Resource):
93 model = ResourcePoolDTO.resource_pool_get
95 @api_ims_inventory_v1.doc('Get resource pool')
96 @api_ims_inventory_v1.marshal_with(model)
97 def get(self, resourcePoolID):
98 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
99 if result is not None:
101 api_ims_inventory_v1.abort(
102 404, "Resource pool {} doesn't exist".format(resourcePoolID))
105 # ---------- Resources ---------- #
106 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
107 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
108 class ResourcesListRouter(Resource):
110 model = ResourceDTO.resource_list
112 @api_ims_inventory_v1.marshal_list_with(model)
113 def get(self, resourcePoolID):
114 return ocloud_view.resources(resourcePoolID, bus.uow)
117 @api_ims_inventory_v1.route(
118 "/resourcePools/<resourcePoolID>/resources/<resourceID>")
119 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
120 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
121 @api_ims_inventory_v1.response(404, 'Resource not found')
122 class ResourceGetRouter(Resource):
124 # dto = ResourceDTO()
125 # model = dto.get_resource_get()
126 model = ResourceDTO.recursive_resource_mapping()
128 @api_ims_inventory_v1.doc('Get resource')
129 @api_ims_inventory_v1.marshal_with(model)
130 def get(self, resourcePoolID, resourceID):
131 result = ocloud_view.resource_one(resourceID, bus.uow)
132 if result is not None:
134 api_ims_inventory_v1.abort(
135 404, "Resource {} doesn't exist".format(resourceID))
138 # ---------- DeploymentManagers ---------- #
139 @api_ims_inventory_v1.route("/deploymentManagers")
140 class DeploymentManagersListRouter(Resource):
142 model = DeploymentManagerDTO.deployment_manager_get
144 @api_ims_inventory_v1.marshal_list_with(model)
146 return ocloud_view.deployment_managers(bus.uow)
149 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
150 @api_ims_inventory_v1.param('deploymentManagerID',
151 'ID of the deployment manager')
152 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
153 class DeploymentManagerGetRouter(Resource):
155 model = DeploymentManagerDTO.deployment_manager_get
157 @api_ims_inventory_v1.doc('Get deployment manager')
158 @api_ims_inventory_v1.marshal_with(model)
159 def get(self, deploymentManagerID):
160 result = ocloud_view.deployment_manager_one(
161 deploymentManagerID, bus.uow)
162 if result is not None:
164 api_ims_inventory_v1.abort(
166 "Deployment manager {} doesn't exist".format(deploymentManagerID))
169 # ---------- Subscriptions ---------- #
170 @api_ims_inventory_v1.route("/subscriptions")
171 class SubscriptionsListRouter(Resource):
173 model = SubscriptionDTO.subscription_get
174 expect = SubscriptionDTO.subscription
175 post_resp = SubscriptionDTO.subscription_post_resp
177 @api_ims_inventory_v1.doc('List subscriptions')
178 @api_ims_inventory_v1.marshal_list_with(model)
180 return ocloud_view.subscriptions(bus.uow)
182 @api_ims_inventory_v1.doc('Create a subscription')
183 @api_ims_inventory_v1.expect(expect)
184 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
186 data = api_ims_inventory_v1.payload
187 result = ocloud_view.subscription_create(data, bus.uow)
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):
211 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)