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
23 apibase = config.get_o2ims_api_base()
26 # ---------- OClouds ---------- #
27 @api_ims_inventory_v1.route("/")
28 @api_ims_inventory_v1.response(404, 'oCloud not found')
29 class OcloudsListRouter(Resource):
30 """Ocloud get endpoint
31 O2 interface ocloud endpoint
34 ocloud_get = OcloudDTO.ocloud
36 @api_ims_inventory_v1.marshal_with(ocloud_get)
38 res = ocloud_view.oclouds(bus.uow)
41 api_ims_inventory_v1.abort(
42 404, "oCloud doesn't exist")
45 # ---------- ResourceTypes ---------- #
46 @api_ims_inventory_v1.route("/resourceTypes")
47 class ResourceTypesListRouter(Resource):
49 model = ResourceTypeDTO.resource_type_get
51 @api_ims_inventory_v1.marshal_list_with(model)
53 return ocloud_view.resource_types(bus.uow)
56 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
57 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
58 @api_ims_inventory_v1.response(404, 'Resource type not found')
59 class ResourceTypeGetRouter(Resource):
61 model = ResourceTypeDTO.resource_type_get
63 @api_ims_inventory_v1.doc('Get resource type')
64 @api_ims_inventory_v1.marshal_with(model)
65 def get(self, resourceTypeID):
66 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
67 if result is not None:
69 api_ims_inventory_v1.abort(
70 404, "Resource type {} doesn't exist".format(resourceTypeID))
73 # ---------- ResourcePools ---------- #
74 @api_ims_inventory_v1.route("/resourcePools")
75 class ResourcePoolsListRouter(Resource):
77 model = ResourcePoolDTO.resource_pool_get
79 @api_ims_inventory_v1.marshal_list_with(model)
81 return ocloud_view.resource_pools(bus.uow)
84 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
85 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
86 @api_ims_inventory_v1.response(404, 'Resource pool not found')
87 class ResourcePoolGetRouter(Resource):
89 model = ResourcePoolDTO.resource_pool_get
91 @api_ims_inventory_v1.doc('Get resource pool')
92 @api_ims_inventory_v1.marshal_with(model)
93 def get(self, resourcePoolID):
94 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
95 if result is not None:
97 api_ims_inventory_v1.abort(
98 404, "Resource pool {} doesn't exist".format(resourcePoolID))
101 # ---------- Resources ---------- #
102 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
103 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
104 class ResourcesListRouter(Resource):
106 model = ResourceDTO.resource_list
108 @api_ims_inventory_v1.marshal_list_with(model)
109 def get(self, resourcePoolID):
110 return ocloud_view.resources(resourcePoolID, bus.uow)
113 @api_ims_inventory_v1.route(
114 "/resourcePools/<resourcePoolID>/resources/<resourceID>")
115 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
116 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
117 @api_ims_inventory_v1.response(404, 'Resource not found')
118 class ResourceGetRouter(Resource):
120 # dto = ResourceDTO()
121 # model = dto.get_resource_get()
122 model = ResourceDTO.recursive_resource_mapping()
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 result = ocloud_view.subscription_create(data, bus.uow)
187 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
188 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
189 @api_ims_inventory_v1.response(404, 'Subscription not found')
190 class SubscriptionGetDelRouter(Resource):
192 model = SubscriptionDTO.subscription_get
194 @api_ims_inventory_v1.doc('Get subscription by ID')
195 @api_ims_inventory_v1.marshal_with(model)
196 def get(self, subscriptionID):
197 result = ocloud_view.subscription_one(
198 subscriptionID, bus.uow)
199 if result is not None:
201 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
204 @api_ims_inventory_v1.doc('Delete subscription by ID')
205 @api_ims_inventory_v1.response(204, 'Subscription deleted')
206 def delete(self, subscriptionID):
207 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
211 def configure_namespace(app, bus_new):
213 # Set global bus for resource
217 app.add_namespace(api_ims_inventory_v1, path=apibase)