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
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 ---------- #
30 @api.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.marshal_with(ocloud_get)
40 res = ocloud_view.oclouds(bus.uow)
44 404, "oCloud doesn't exist")
47 # ---------- ResourceTypes ---------- #
48 @api.route("/resourceTypes")
49 class ResourceTypesListRouter(Resource):
51 model = ResourceTypeDTO.resource_type_get
53 @api.marshal_list_with(model)
55 return ocloud_view.resource_types(bus.uow)
58 @api.route("/resourceTypes/<resourceTypeID>")
59 @api.param('resourceTypeID', 'ID of the resource type')
60 @api.response(404, 'Resource type not found')
61 class ResourceTypeGetRouter(Resource):
63 model = ResourceTypeDTO.resource_type_get
65 @api.doc('Get resource type')
66 @api.marshal_with(model)
67 def get(self, resourceTypeID):
68 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
69 if result is not None:
72 404, "Resource type {} doesn't exist".format(resourceTypeID))
75 # ---------- ResourcePools ---------- #
76 @api.route("/resourcePools")
77 class ResourcePoolsListRouter(Resource):
79 model = ResourcePoolDTO.resource_pool_get
81 @api.marshal_list_with(model)
83 return ocloud_view.resource_pools(bus.uow)
86 @api.route("/resourcePools/<resourcePoolID>")
87 @api.param('resourcePoolID', 'ID of the resource pool')
88 @api.response(404, 'Resource pool not found')
89 class ResourcePoolGetRouter(Resource):
91 model = ResourcePoolDTO.resource_pool_get
93 @api.doc('Get resource pool')
94 @api.marshal_with(model)
95 def get(self, resourcePoolID):
96 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
97 if result is not None:
100 404, "Resource pool {} doesn't exist".format(resourcePoolID))
103 # ---------- Resources ---------- #
104 @api.route("/resourcePools/<resourcePoolID>/resources")
105 @api.param('resourcePoolID', 'ID of the resource pool')
106 class ResourcesListRouter(Resource):
108 model = ResourceDTO.resource_list
110 @api.marshal_list_with(model)
111 def get(self, resourcePoolID):
112 return ocloud_view.resources(resourcePoolID, bus.uow)
115 @api.route("/resourcePools/<resourcePoolID>/resources/<resourceID>")
116 @api.param('resourcePoolID', 'ID of the resource pool')
117 @api.param('resourceID', 'ID of the resource')
118 @api.response(404, 'Resource not found')
119 class ResourceGetRouter(Resource):
121 model = ResourceDTO.resource_get
123 @api.doc('Get resource')
124 @api.marshal_with(model)
125 def get(self, resourcePoolID, resourceID):
126 result = ocloud_view.resource_one(resourceID, bus.uow)
127 if result is not None:
129 api.abort(404, "Resource {} doesn't exist".format(resourceID))
132 # ---------- DeploymentManagers ---------- #
133 @api.route("/deploymentManagers")
134 class DeploymentManagersListRouter(Resource):
136 model = DeploymentManagerDTO.deployment_manager_get
138 @api.marshal_list_with(model)
140 return ocloud_view.deployment_managers(bus.uow)
143 @api.route("/deploymentManagers/<deploymentManagerID>")
144 @api.param('deploymentManagerID', 'ID of the deployment manager')
145 @api.response(404, 'Deployment manager not found')
146 class DeploymentManagerGetRouter(Resource):
148 model = DeploymentManagerDTO.deployment_manager_get
150 @api.doc('Get deployment manager')
151 @api.marshal_with(model)
152 def get(self, deploymentManagerID):
153 result = ocloud_view.deployment_manager_one(
154 deploymentManagerID, bus.uow)
155 if result is not None:
157 api.abort(404, "Deployment manager {} doesn't exist".format(
158 deploymentManagerID))
161 # ---------- Subscriptions ---------- #
162 @api.route("/subscriptions")
163 class SubscriptionsListRouter(Resource):
165 model = SubscriptionDTO.subscription_get
166 expect = SubscriptionDTO.subscription
167 post_resp = SubscriptionDTO.subscription_post_resp
169 @api.doc('List subscriptions')
170 @api.marshal_list_with(model)
172 return ocloud_view.subscriptions(bus.uow)
174 @api.doc('Create a subscription')
176 @api.marshal_with(post_resp, code=201)
179 sub_uuid = str(uuid.uuid4())
180 subscription = Subscription(
181 sub_uuid, data['callback'], data['consumerSubscriptionId'],
183 ocloud_view.subscription_create(subscription, bus.uow)
184 return {"subscriptionId": sub_uuid}, 201
187 @api.route("/subscriptions/<subscriptionID>")
188 @api.param('subscriptionID', 'ID of the subscription')
189 @api.response(404, 'Subscription not found')
190 class SubscriptionGetDelRouter(Resource):
192 model = SubscriptionDTO.subscription_get
194 @api.doc('Get subscription by ID')
195 @api.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.abort(404, "Subscription {} doesn't exist".format(
204 @api.doc('Delete subscription by ID')
205 @api.response(204, 'Subscription deleted')
206 def delete(self, subscriptionID):
208 bus.uow.subscriptions.delete(subscriptionID)
213 def configure_namespace(app, bus_new):
215 # Set global bus for resource
220 app.add_namespace(api_v1, path=apibase)