a01021efc0c00c3d03ca7809f58054ba263b1532
[pti/o2.git] / o2ims / views / ocloud_route.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from flask_restx import Resource
16
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
21
22
23 apibase = config.get_o2ims_api_base()
24
25
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
32     """
33
34     ocloud_get = OcloudDTO.ocloud
35
36     @api_ims_inventory_v1.marshal_with(ocloud_get)
37     def get(self):
38         res = ocloud_view.oclouds(bus.uow)
39         if len(res) > 0:
40             return res[0]
41         api_ims_inventory_v1.abort(
42             404, "oCloud doesn't exist")
43
44
45 # ----------  ResourceTypes ---------- #
46 @api_ims_inventory_v1.route("/resourceTypes")
47 class ResourceTypesListRouter(Resource):
48
49     model = ResourceTypeDTO.resource_type_get
50
51     @api_ims_inventory_v1.marshal_list_with(model)
52     def get(self):
53         return ocloud_view.resource_types(bus.uow)
54
55
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):
60
61     model = ResourceTypeDTO.resource_type_get
62
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:
68             return result
69         api_ims_inventory_v1.abort(
70             404, "Resource type {} doesn't exist".format(resourceTypeID))
71
72
73 # ----------  ResourcePools ---------- #
74 @api_ims_inventory_v1.route("/resourcePools")
75 class ResourcePoolsListRouter(Resource):
76
77     model = ResourcePoolDTO.resource_pool_get
78
79     @api_ims_inventory_v1.marshal_list_with(model)
80     def get(self):
81         return ocloud_view.resource_pools(bus.uow)
82
83
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):
88
89     model = ResourcePoolDTO.resource_pool_get
90
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:
96             return result
97         api_ims_inventory_v1.abort(
98             404, "Resource pool {} doesn't exist".format(resourcePoolID))
99
100
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):
105
106     model = ResourceDTO.resource_list
107
108     @api_ims_inventory_v1.marshal_list_with(model)
109     def get(self, resourcePoolID):
110         return ocloud_view.resources(resourcePoolID, bus.uow)
111
112
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):
119
120     # dto = ResourceDTO()
121     # model = dto.get_resource_get()
122     model = ResourceDTO.recursive_resource_mapping()
123
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:
129             return result
130         api_ims_inventory_v1.abort(
131             404, "Resource {} doesn't exist".format(resourceID))
132
133
134 # ----------  DeploymentManagers ---------- #
135 @api_ims_inventory_v1.route("/deploymentManagers")
136 class DeploymentManagersListRouter(Resource):
137
138     model = DeploymentManagerDTO.deployment_manager_get
139
140     @api_ims_inventory_v1.marshal_list_with(model)
141     def get(self):
142         return ocloud_view.deployment_managers(bus.uow)
143
144
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):
150
151     model = DeploymentManagerDTO.deployment_manager_get
152
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:
159             return result
160         api_ims_inventory_v1.abort(
161             404,
162             "Deployment manager {} doesn't exist".format(deploymentManagerID))
163
164
165 # ----------  Subscriptions ---------- #
166 @api_ims_inventory_v1.route("/subscriptions")
167 class SubscriptionsListRouter(Resource):
168
169     model = SubscriptionDTO.subscription_get
170     expect = SubscriptionDTO.subscription
171     post_resp = SubscriptionDTO.subscription_post_resp
172
173     @api_ims_inventory_v1.doc('List subscriptions')
174     @api_ims_inventory_v1.marshal_list_with(model)
175     def get(self):
176         return ocloud_view.subscriptions(bus.uow)
177
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)
181     def post(self):
182         data = api_ims_inventory_v1.payload
183         result = ocloud_view.subscription_create(data, bus.uow)
184         return result, 201
185
186
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):
191
192     model = SubscriptionDTO.subscription_get
193
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:
200             return result
201         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
202             subscriptionID))
203
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)
208         return result, 204
209
210
211 def configure_namespace(app, bus_new):
212
213     # Set global bus for resource
214     global bus
215     bus = bus_new
216
217     app.add_namespace(api_ims_inventory_v1, path=apibase)