Pagination in request and response; Fix alarm client issue
[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 import request
16 from flask_restx import Resource, reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2common.views.pagination_route import link_header, PAGE_PARAM
20 from o2ims.views import ocloud_view
21 from o2ims.views.api_ns import api_ims_inventory_v1
22 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
23     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
24
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28
29 def configure_api_route():
30     # Set global bus for resource
31     global bus
32     bus = MessageBus.get_instance()
33
34
35 # ----------  OClouds ---------- #
36 @api_ims_inventory_v1.route("/")
37 @api_ims_inventory_v1.response(404, 'oCloud not found')
38 class OcloudsListRouter(Resource):
39     """Ocloud get endpoint
40     O2 interface ocloud endpoint
41     """
42
43     ocloud_get = OcloudDTO.ocloud
44
45     @api_ims_inventory_v1.marshal_with(ocloud_get)
46     def get(self):
47         res = ocloud_view.oclouds(bus.uow)
48         if len(res) > 0:
49             return res[0]
50         api_ims_inventory_v1.abort(
51             404, "oCloud doesn't exist")
52
53
54 # ----------  ResourceTypes ---------- #
55 @api_ims_inventory_v1.route("/resourceTypes")
56 @api_ims_inventory_v1.param(PAGE_PARAM,
57                             'Page number of the results to fetch.' +
58                             ' Default: 1',
59                             _in='query', default=1)
60 class ResourceTypesListRouter(Resource):
61
62     model = ResourceTypeDTO.resource_type_get
63
64     @api_ims_inventory_v1.marshal_list_with(model)
65     def get(self):
66         parser = reqparse.RequestParser()
67         parser.add_argument(PAGE_PARAM, location='args')
68         args = parser.parse_args()
69         kwargs = {}
70         if args.nextpage_opaque_marker is not None:
71             kwargs['page'] = args.nextpage_opaque_marker
72
73         ret = ocloud_view.resource_types(bus.uow, **kwargs)
74         return link_header(request.full_path, ret)
75
76
77 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
78 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
79 @api_ims_inventory_v1.response(404, 'Resource type not found')
80 class ResourceTypeGetRouter(Resource):
81
82     model = ResourceTypeDTO.resource_type_get
83
84     @api_ims_inventory_v1.doc('Get resource type')
85     @api_ims_inventory_v1.marshal_with(model)
86     def get(self, resourceTypeID):
87         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
88         if result is not None:
89             return result
90         api_ims_inventory_v1.abort(
91             404, "Resource type {} doesn't exist".format(resourceTypeID))
92
93
94 # ----------  ResourcePools ---------- #
95 @api_ims_inventory_v1.route("/resourcePools")
96 @api_ims_inventory_v1.param(PAGE_PARAM,
97                             'Page number of the results to fetch.' +
98                             ' Default: 1',
99                             _in='query', default=1)
100 class ResourcePoolsListRouter(Resource):
101
102     model = ResourcePoolDTO.resource_pool_get
103
104     @api_ims_inventory_v1.marshal_list_with(model)
105     def get(self):
106         parser = reqparse.RequestParser()
107         parser.add_argument(PAGE_PARAM, location='args')
108         args = parser.parse_args()
109         kwargs = {}
110         if args.nextpage_opaque_marker is not None:
111             kwargs['page'] = args.nextpage_opaque_marker
112
113         ret = ocloud_view.resource_pools(bus.uow, **kwargs)
114         return link_header(request.full_path, ret)
115
116
117 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
118 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
119 @api_ims_inventory_v1.response(404, 'Resource pool not found')
120 class ResourcePoolGetRouter(Resource):
121
122     model = ResourcePoolDTO.resource_pool_get
123
124     @api_ims_inventory_v1.doc('Get resource pool')
125     @api_ims_inventory_v1.marshal_with(model)
126     def get(self, resourcePoolID):
127         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
128         if result is not None:
129             return result
130         api_ims_inventory_v1.abort(
131             404, "Resource pool {} doesn't exist".format(resourcePoolID))
132
133
134 # ----------  Resources ---------- #
135 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
136 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
137 @api_ims_inventory_v1.param('resourceTypeName', 'filter resource type',
138                             _in='query')
139 @api_ims_inventory_v1.param('parentId', 'filter parentId',
140                             _in='query')
141 # @api_ims_inventory_v1.param('sort', 'sort by column name',
142 #                             _in='query')
143 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
144 #                             '(max 100). Default: 30',
145 #                             _in='query', default=30)
146 @api_ims_inventory_v1.param(PAGE_PARAM,
147                             'Page number of the results to fetch.' +
148                             ' Default: 1',
149                             _in='query', default=1)
150 class ResourcesListRouter(Resource):
151
152     model = ResourceDTO.resource_list
153
154     @api_ims_inventory_v1.marshal_list_with(model)
155     def get(self, resourcePoolID):
156         parser = reqparse.RequestParser()
157         parser.add_argument('resourceTypeName', location='args')
158         parser.add_argument('parentId', location='args')
159         # parser.add_argument('sort', location='args')
160         # parser.add_argument('per_page', location='args')
161         parser.add_argument(PAGE_PARAM, location='args')
162         args = parser.parse_args()
163         kwargs = {}
164         if args.resourceTypeName is not None:
165             kwargs['resourceTypeName'] = args.resourceTypeName
166         if args.parentId is not None:
167             kwargs['parentId'] = args.parentId
168             if args.parentId.lower() == 'null':
169                 kwargs['parentId'] = None
170         # if args.per_page is not None:
171         #     kwargs['per_page'] = args.per_page
172         #     base_url = base_url + 'per_page=' + args.per_page + '&'
173         if args.nextpage_opaque_marker is not None:
174             kwargs['page'] = args.nextpage_opaque_marker
175
176         ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
177
178         return link_header(request.full_path, ret)
179
180
181 @api_ims_inventory_v1.route(
182     "/resourcePools/<resourcePoolID>/resources/<resourceID>")
183 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
184 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
185 @api_ims_inventory_v1.response(404, 'Resource not found')
186 class ResourceGetRouter(Resource):
187
188     # dto = ResourceDTO()
189     # model = dto.get_resource_get()
190     model = ResourceDTO.recursive_resource_mapping()
191
192     @api_ims_inventory_v1.doc('Get resource')
193     @api_ims_inventory_v1.marshal_with(model)
194     def get(self, resourcePoolID, resourceID):
195         result = ocloud_view.resource_one(resourceID, bus.uow)
196         if result is not None:
197             return result
198         api_ims_inventory_v1.abort(
199             404, "Resource {} doesn't exist".format(resourceID))
200
201
202 # ----------  DeploymentManagers ---------- #
203 @api_ims_inventory_v1.route("/deploymentManagers")
204 @api_ims_inventory_v1.param(PAGE_PARAM,
205                             'Page number of the results to fetch.' +
206                             ' Default: 1',
207                             _in='query', default=1)
208 class DeploymentManagersListRouter(Resource):
209
210     model = DeploymentManagerDTO.deployment_manager_list
211
212     @api_ims_inventory_v1.marshal_list_with(model)
213     def get(self):
214         parser = reqparse.RequestParser()
215         parser.add_argument(PAGE_PARAM, location='args')
216         args = parser.parse_args()
217         kwargs = {}
218         if args.nextpage_opaque_marker is not None:
219             kwargs['page'] = args.nextpage_opaque_marker
220
221         ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
222         return link_header(request.full_path, ret)
223
224
225 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
226 @api_ims_inventory_v1.param('deploymentManagerID',
227                             'ID of the deployment manager')
228 @api_ims_inventory_v1.param('profile', 'DMS profile: value supports "sol018"',
229                             _in='query')
230 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
231 class DeploymentManagerGetRouter(Resource):
232
233     model = DeploymentManagerDTO.deployment_manager_get
234
235     @api_ims_inventory_v1.doc('Get deployment manager')
236     @api_ims_inventory_v1.marshal_with(model)
237     def get(self, deploymentManagerID):
238         parser = reqparse.RequestParser()
239         parser.add_argument('profile', location='args')
240         args = parser.parse_args()
241         profile = (
242             args.profile if args.profile is not None and args.profile != ''
243             else 'default')
244         result = ocloud_view.deployment_manager_one(
245             deploymentManagerID, bus.uow, profile)
246         if result is not None:
247             return result
248         api_ims_inventory_v1.abort(
249             404,
250             "Deployment manager {} doesn't exist".format(deploymentManagerID))
251
252
253 # ----------  Subscriptions ---------- #
254 @api_ims_inventory_v1.route("/subscriptions")
255 class SubscriptionsListRouter(Resource):
256
257     model = SubscriptionDTO.subscription_get
258     expect = SubscriptionDTO.subscription
259     post_resp = SubscriptionDTO.subscription_post_resp
260
261     @api_ims_inventory_v1.doc('List subscriptions')
262     @api_ims_inventory_v1.marshal_list_with(model)
263     def get(self):
264         parser = reqparse.RequestParser()
265         parser.add_argument(PAGE_PARAM, location='args')
266         args = parser.parse_args()
267         kwargs = {}
268         if args.nextpage_opaque_marker is not None:
269             kwargs['page'] = args.nextpage_opaque_marker
270
271         ret = ocloud_view.subscriptions(bus.uow, **kwargs)
272         return link_header(request.full_path, ret)
273
274     @api_ims_inventory_v1.doc('Create a subscription')
275     @api_ims_inventory_v1.expect(expect)
276     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
277     def post(self):
278         data = api_ims_inventory_v1.payload
279         result = ocloud_view.subscription_create(data, bus.uow)
280         return result, 201
281
282
283 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
284 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
285 @api_ims_inventory_v1.response(404, 'Subscription not found')
286 class SubscriptionGetDelRouter(Resource):
287
288     model = SubscriptionDTO.subscription_get
289
290     @api_ims_inventory_v1.doc('Get subscription by ID')
291     @api_ims_inventory_v1.marshal_with(model)
292     def get(self, subscriptionID):
293         result = ocloud_view.subscription_one(
294             subscriptionID, bus.uow)
295         if result is not None:
296             return result
297         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
298             subscriptionID))
299
300     @api_ims_inventory_v1.doc('Delete subscription by ID')
301     @api_ims_inventory_v1.response(204, 'Subscription deleted')
302     def delete(self, subscriptionID):
303         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
304         return result, 204