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, reqparse
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
23 from o2common.helper import o2logging
24 logger = o2logging.get_logger(__name__)
27 def configure_api_route():
28 # Set global bus for resource
30 bus = MessageBus.get_instance()
33 # ---------- OClouds ---------- #
34 @api_ims_inventory_v1.route("/")
35 @api_ims_inventory_v1.response(404, 'oCloud not found')
36 class OcloudsListRouter(Resource):
37 """Ocloud get endpoint
38 O2 interface ocloud endpoint
41 ocloud_get = OcloudDTO.ocloud
43 @api_ims_inventory_v1.marshal_with(ocloud_get)
45 res = ocloud_view.oclouds(bus.uow)
48 api_ims_inventory_v1.abort(
49 404, "oCloud doesn't exist")
52 # ---------- ResourceTypes ---------- #
53 @api_ims_inventory_v1.route("/resourceTypes")
54 class ResourceTypesListRouter(Resource):
56 model = ResourceTypeDTO.resource_type_get
58 @api_ims_inventory_v1.marshal_list_with(model)
60 return ocloud_view.resource_types(bus.uow)
63 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
64 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
65 @api_ims_inventory_v1.response(404, 'Resource type not found')
66 class ResourceTypeGetRouter(Resource):
68 model = ResourceTypeDTO.resource_type_get
70 @api_ims_inventory_v1.doc('Get resource type')
71 @api_ims_inventory_v1.marshal_with(model)
72 def get(self, resourceTypeID):
73 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
74 if result is not None:
76 api_ims_inventory_v1.abort(
77 404, "Resource type {} doesn't exist".format(resourceTypeID))
80 # ---------- ResourcePools ---------- #
81 @api_ims_inventory_v1.route("/resourcePools")
82 class ResourcePoolsListRouter(Resource):
84 model = ResourcePoolDTO.resource_pool_get
86 @api_ims_inventory_v1.marshal_list_with(model)
88 return ocloud_view.resource_pools(bus.uow)
91 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
92 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
93 @api_ims_inventory_v1.response(404, 'Resource pool not found')
94 class ResourcePoolGetRouter(Resource):
96 model = ResourcePoolDTO.resource_pool_get
98 @api_ims_inventory_v1.doc('Get resource pool')
99 @api_ims_inventory_v1.marshal_with(model)
100 def get(self, resourcePoolID):
101 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
102 if result is not None:
104 api_ims_inventory_v1.abort(
105 404, "Resource pool {} doesn't exist".format(resourcePoolID))
108 # ---------- Resources ---------- #
109 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
110 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
111 @api_ims_inventory_v1.param('resourceTypeName', 'filter resource type',
113 @api_ims_inventory_v1.param('parentId', 'filter parentId',
115 class ResourcesListRouter(Resource):
117 model = ResourceDTO.resource_list
119 @api_ims_inventory_v1.marshal_list_with(model)
120 def get(self, resourcePoolID):
121 parser = reqparse.RequestParser()
122 parser.add_argument('resourceTypeName', location='args')
123 parser.add_argument('parentId', location='args')
124 args = parser.parse_args()
126 if args.resourceTypeName is not None:
127 kwargs['resourceTypeName'] = args.resourceTypeName
128 if args.parentId is not None:
129 kwargs['parentId'] = args.parentId
130 if args.parentId.lower() == 'null':
131 kwargs['parentId'] = None
133 return ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
136 @api_ims_inventory_v1.route(
137 "/resourcePools/<resourcePoolID>/resources/<resourceID>")
138 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
139 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
140 @api_ims_inventory_v1.response(404, 'Resource not found')
141 class ResourceGetRouter(Resource):
143 # dto = ResourceDTO()
144 # model = dto.get_resource_get()
145 model = ResourceDTO.recursive_resource_mapping()
147 @api_ims_inventory_v1.doc('Get resource')
148 @api_ims_inventory_v1.marshal_with(model)
149 def get(self, resourcePoolID, resourceID):
150 result = ocloud_view.resource_one(resourceID, bus.uow)
151 if result is not None:
153 api_ims_inventory_v1.abort(
154 404, "Resource {} doesn't exist".format(resourceID))
157 # ---------- DeploymentManagers ---------- #
158 @api_ims_inventory_v1.route("/deploymentManagers")
159 class DeploymentManagersListRouter(Resource):
161 model = DeploymentManagerDTO.deployment_manager_list
163 @api_ims_inventory_v1.marshal_list_with(model)
165 return ocloud_view.deployment_managers(bus.uow)
168 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
169 @api_ims_inventory_v1.param('deploymentManagerID',
170 'ID of the deployment manager')
171 @api_ims_inventory_v1.param('profile', 'DMS profile',
173 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
174 class DeploymentManagerGetRouter(Resource):
176 model = DeploymentManagerDTO.deployment_manager_get
178 @api_ims_inventory_v1.doc('Get deployment manager')
179 @api_ims_inventory_v1.marshal_with(model)
180 def get(self, deploymentManagerID):
181 parser = reqparse.RequestParser()
182 parser.add_argument('profile', location='args')
183 args = parser.parse_args()
184 result = ocloud_view.deployment_manager_one(
185 deploymentManagerID, bus.uow, args.profile)
186 if result is not None:
188 api_ims_inventory_v1.abort(
190 "Deployment manager {} doesn't exist".format(deploymentManagerID))
193 # ---------- Subscriptions ---------- #
194 @api_ims_inventory_v1.route("/subscriptions")
195 class SubscriptionsListRouter(Resource):
197 model = SubscriptionDTO.subscription_get
198 expect = SubscriptionDTO.subscription
199 post_resp = SubscriptionDTO.subscription_post_resp
201 @api_ims_inventory_v1.doc('List subscriptions')
202 @api_ims_inventory_v1.marshal_list_with(model)
204 return ocloud_view.subscriptions(bus.uow)
206 @api_ims_inventory_v1.doc('Create a subscription')
207 @api_ims_inventory_v1.expect(expect)
208 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
210 data = api_ims_inventory_v1.payload
211 result = ocloud_view.subscription_create(data, bus.uow)
215 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
216 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
217 @api_ims_inventory_v1.response(404, 'Subscription not found')
218 class SubscriptionGetDelRouter(Resource):
220 model = SubscriptionDTO.subscription_get
222 @api_ims_inventory_v1.doc('Get subscription by ID')
223 @api_ims_inventory_v1.marshal_with(model)
224 def get(self, subscriptionID):
225 result = ocloud_view.subscription_one(
226 subscriptionID, bus.uow)
227 if result is not None:
229 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
232 @api_ims_inventory_v1.doc('Delete subscription by ID')
233 @api_ims_inventory_v1.response(204, 'Subscription deleted')
234 def delete(self, subscriptionID):
235 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)