Fix: the resource's parameter 'parentId' is resourcepool ID
[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 from flask_restx import reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2ims.views import ocloud_view
20 from o2ims.views.api_ns import api_ims_inventory_v1
21 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
22     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
23
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
26
27
28 def configure_api_route():
29     # Set global bus for resource
30     global bus
31     bus = MessageBus.get_instance()
32
33
34 # ----------  OClouds ---------- #
35 @api_ims_inventory_v1.route("/")
36 @api_ims_inventory_v1.response(404, 'oCloud not found')
37 class OcloudsListRouter(Resource):
38     """Ocloud get endpoint
39     O2 interface ocloud endpoint
40     """
41
42     ocloud_get = OcloudDTO.ocloud
43
44     @api_ims_inventory_v1.marshal_with(ocloud_get)
45     def get(self):
46         res = ocloud_view.oclouds(bus.uow)
47         if len(res) > 0:
48             return res[0]
49         api_ims_inventory_v1.abort(
50             404, "oCloud doesn't exist")
51
52
53 # ----------  ResourceTypes ---------- #
54 @api_ims_inventory_v1.route("/resourceTypes")
55 class ResourceTypesListRouter(Resource):
56
57     model = ResourceTypeDTO.resource_type_get
58
59     @api_ims_inventory_v1.marshal_list_with(model)
60     def get(self):
61         return ocloud_view.resource_types(bus.uow)
62
63
64 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
65 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
66 @api_ims_inventory_v1.response(404, 'Resource type not found')
67 class ResourceTypeGetRouter(Resource):
68
69     model = ResourceTypeDTO.resource_type_get
70
71     @api_ims_inventory_v1.doc('Get resource type')
72     @api_ims_inventory_v1.marshal_with(model)
73     def get(self, resourceTypeID):
74         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
75         if result is not None:
76             return result
77         api_ims_inventory_v1.abort(
78             404, "Resource type {} doesn't exist".format(resourceTypeID))
79
80
81 # ----------  ResourcePools ---------- #
82 @api_ims_inventory_v1.route("/resourcePools")
83 class ResourcePoolsListRouter(Resource):
84
85     model = ResourcePoolDTO.resource_pool_get
86
87     @api_ims_inventory_v1.marshal_list_with(model)
88     def get(self):
89         return ocloud_view.resource_pools(bus.uow)
90
91
92 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
93 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
94 @api_ims_inventory_v1.response(404, 'Resource pool not found')
95 class ResourcePoolGetRouter(Resource):
96
97     model = ResourcePoolDTO.resource_pool_get
98
99     @api_ims_inventory_v1.doc('Get resource pool')
100     @api_ims_inventory_v1.marshal_with(model)
101     def get(self, resourcePoolID):
102         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
103         if result is not None:
104             return result
105         api_ims_inventory_v1.abort(
106             404, "Resource pool {} doesn't exist".format(resourcePoolID))
107
108
109 # ----------  Resources ---------- #
110 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
111 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
112 @api_ims_inventory_v1.param('resourceTypeName', 'filter resource type',
113                             location='args')
114 @api_ims_inventory_v1.param('parentId', 'filter parentId',
115                             location='args')
116 class ResourcesListRouter(Resource):
117
118     model = ResourceDTO.resource_list
119
120     @api_ims_inventory_v1.marshal_list_with(model)
121     def get(self, resourcePoolID):
122         parser = reqparse.RequestParser()
123         parser.add_argument('resourceTypeName', location='args')
124         parser.add_argument('parentId', location='args')
125         args = parser.parse_args()
126         kwargs = {}
127         if args.resourceTypeName is not None:
128             kwargs['resourceTypeName'] = args.resourceTypeName
129         if args.parentId is not None:
130             kwargs['parentId'] = args.parentId
131             if args.parentId.lower() == 'null':
132                 kwargs['parentId'] = None
133
134         return ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
135
136
137 @api_ims_inventory_v1.route(
138     "/resourcePools/<resourcePoolID>/resources/<resourceID>")
139 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
140 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
141 @api_ims_inventory_v1.response(404, 'Resource not found')
142 class ResourceGetRouter(Resource):
143
144     # dto = ResourceDTO()
145     # model = dto.get_resource_get()
146     model = ResourceDTO.recursive_resource_mapping()
147
148     @api_ims_inventory_v1.doc('Get resource')
149     @api_ims_inventory_v1.marshal_with(model)
150     def get(self, resourcePoolID, resourceID):
151         result = ocloud_view.resource_one(resourceID, bus.uow)
152         if result is not None:
153             return result
154         api_ims_inventory_v1.abort(
155             404, "Resource {} doesn't exist".format(resourceID))
156
157
158 # ----------  DeploymentManagers ---------- #
159 @api_ims_inventory_v1.route("/deploymentManagers")
160 class DeploymentManagersListRouter(Resource):
161
162     model = DeploymentManagerDTO.deployment_manager_get
163
164     @api_ims_inventory_v1.marshal_list_with(model)
165     def get(self):
166         return ocloud_view.deployment_managers(bus.uow)
167
168
169 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
170 @api_ims_inventory_v1.param('deploymentManagerID',
171                             'ID of the deployment manager')
172 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
173 class DeploymentManagerGetRouter(Resource):
174
175     model = DeploymentManagerDTO.deployment_manager_get
176
177     @api_ims_inventory_v1.doc('Get deployment manager')
178     @api_ims_inventory_v1.marshal_with(model)
179     def get(self, deploymentManagerID):
180         result = ocloud_view.deployment_manager_one(
181             deploymentManagerID, bus.uow)
182         if result is not None:
183             return result
184         api_ims_inventory_v1.abort(
185             404,
186             "Deployment manager {} doesn't exist".format(deploymentManagerID))
187
188
189 # ----------  Subscriptions ---------- #
190 @api_ims_inventory_v1.route("/subscriptions")
191 class SubscriptionsListRouter(Resource):
192
193     model = SubscriptionDTO.subscription_get
194     expect = SubscriptionDTO.subscription
195     post_resp = SubscriptionDTO.subscription_post_resp
196
197     @api_ims_inventory_v1.doc('List subscriptions')
198     @api_ims_inventory_v1.marshal_list_with(model)
199     def get(self):
200         return ocloud_view.subscriptions(bus.uow)
201
202     @api_ims_inventory_v1.doc('Create a subscription')
203     @api_ims_inventory_v1.expect(expect)
204     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
205     def post(self):
206         data = api_ims_inventory_v1.payload
207         result = ocloud_view.subscription_create(data, bus.uow)
208         return result, 201
209
210
211 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
212 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
213 @api_ims_inventory_v1.response(404, 'Subscription not found')
214 class SubscriptionGetDelRouter(Resource):
215
216     model = SubscriptionDTO.subscription_get
217
218     @api_ims_inventory_v1.doc('Get subscription by ID')
219     @api_ims_inventory_v1.marshal_with(model)
220     def get(self, subscriptionID):
221         result = ocloud_view.subscription_one(
222             subscriptionID, bus.uow)
223         if result is not None:
224             return result
225         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
226             subscriptionID))
227
228     @api_ims_inventory_v1.doc('Delete subscription by ID')
229     @api_ims_inventory_v1.response(204, 'Subscription deleted')
230     def delete(self, subscriptionID):
231         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
232         return result, 204