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 model = ResourceDTO.resource_get
122 @api_ims_inventory_v1.doc('Get resource')
123 @api_ims_inventory_v1.marshal_with(model)
124 def get(self, resourcePoolID, resourceID):
125 result = ocloud_view.resource_one(resourceID, bus.uow)
126 if result is not None:
128 api_ims_inventory_v1.abort(
129 404, "Resource {} doesn't exist".format(resourceID))
132 # ---------- DeploymentManagers ---------- #
133 @api_ims_inventory_v1.route("/deploymentManagers")
134 class DeploymentManagersListRouter(Resource):
136 model = DeploymentManagerDTO.deployment_manager_get
138 @api_ims_inventory_v1.marshal_list_with(model)
140 return ocloud_view.deployment_managers(bus.uow)
143 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
144 @api_ims_inventory_v1.param('deploymentManagerID',
145 'ID of the deployment manager')
146 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
147 class DeploymentManagerGetRouter(Resource):
149 model = DeploymentManagerDTO.deployment_manager_get
151 @api_ims_inventory_v1.doc('Get deployment manager')
152 @api_ims_inventory_v1.marshal_with(model)
153 def get(self, deploymentManagerID):
154 result = ocloud_view.deployment_manager_one(
155 deploymentManagerID, bus.uow)
156 if result is not None:
158 api_ims_inventory_v1.abort(
160 "Deployment manager {} doesn't exist".format(deploymentManagerID))
163 # ---------- Subscriptions ---------- #
164 @api_ims_inventory_v1.route("/subscriptions")
165 class SubscriptionsListRouter(Resource):
167 model = SubscriptionDTO.subscription_get
168 expect = SubscriptionDTO.subscription
169 post_resp = SubscriptionDTO.subscription_post_resp
171 @api_ims_inventory_v1.doc('List subscriptions')
172 @api_ims_inventory_v1.marshal_list_with(model)
174 return ocloud_view.subscriptions(bus.uow)
176 @api_ims_inventory_v1.doc('Create a subscription')
177 @api_ims_inventory_v1.expect(expect)
178 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
180 data = api_ims_inventory_v1.payload
181 result = ocloud_view.subscription_create(data, bus.uow)
185 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
186 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
187 @api_ims_inventory_v1.response(404, 'Subscription not found')
188 class SubscriptionGetDelRouter(Resource):
190 model = SubscriptionDTO.subscription_get
192 @api_ims_inventory_v1.doc('Get subscription by ID')
193 @api_ims_inventory_v1.marshal_with(model)
194 def get(self, subscriptionID):
195 result = ocloud_view.subscription_one(
196 subscriptionID, bus.uow)
197 if result is not None:
199 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
202 @api_ims_inventory_v1.doc('Delete subscription by ID')
203 @api_ims_inventory_v1.response(204, 'Subscription deleted')
204 def delete(self, subscriptionID):
205 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
209 def configure_namespace(app, bus_new):
211 # Set global bus for resource
215 app.add_namespace(api_ims_inventory_v1, path=apibase)