00fe520c1dad08e8d20e7dfa6657f700fb3c9b3b
[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         return ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
132
133
134 @api_ims_inventory_v1.route(
135     "/resourcePools/<resourcePoolID>/resources/<resourceID>")
136 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
137 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
138 @api_ims_inventory_v1.response(404, 'Resource not found')
139 class ResourceGetRouter(Resource):
140
141     # dto = ResourceDTO()
142     # model = dto.get_resource_get()
143     model = ResourceDTO.recursive_resource_mapping()
144
145     @api_ims_inventory_v1.doc('Get resource')
146     @api_ims_inventory_v1.marshal_with(model)
147     def get(self, resourcePoolID, resourceID):
148         result = ocloud_view.resource_one(resourceID, bus.uow)
149         if result is not None:
150             return result
151         api_ims_inventory_v1.abort(
152             404, "Resource {} doesn't exist".format(resourceID))
153
154
155 # ----------  DeploymentManagers ---------- #
156 @api_ims_inventory_v1.route("/deploymentManagers")
157 class DeploymentManagersListRouter(Resource):
158
159     model = DeploymentManagerDTO.deployment_manager_get
160
161     @api_ims_inventory_v1.marshal_list_with(model)
162     def get(self):
163         return ocloud_view.deployment_managers(bus.uow)
164
165
166 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
167 @api_ims_inventory_v1.param('deploymentManagerID',
168                             'ID of the deployment manager')
169 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
170 class DeploymentManagerGetRouter(Resource):
171
172     model = DeploymentManagerDTO.deployment_manager_get
173
174     @api_ims_inventory_v1.doc('Get deployment manager')
175     @api_ims_inventory_v1.marshal_with(model)
176     def get(self, deploymentManagerID):
177         result = ocloud_view.deployment_manager_one(
178             deploymentManagerID, bus.uow)
179         if result is not None:
180             return result
181         api_ims_inventory_v1.abort(
182             404,
183             "Deployment manager {} doesn't exist".format(deploymentManagerID))
184
185
186 # ----------  Subscriptions ---------- #
187 @api_ims_inventory_v1.route("/subscriptions")
188 class SubscriptionsListRouter(Resource):
189
190     model = SubscriptionDTO.subscription_get
191     expect = SubscriptionDTO.subscription
192     post_resp = SubscriptionDTO.subscription_post_resp
193
194     @api_ims_inventory_v1.doc('List subscriptions')
195     @api_ims_inventory_v1.marshal_list_with(model)
196     def get(self):
197         return ocloud_view.subscriptions(bus.uow)
198
199     @api_ims_inventory_v1.doc('Create a subscription')
200     @api_ims_inventory_v1.expect(expect)
201     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
202     def post(self):
203         data = api_ims_inventory_v1.payload
204         result = ocloud_view.subscription_create(data, bus.uow)
205         return result, 201
206
207
208 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
209 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
210 @api_ims_inventory_v1.response(404, 'Subscription not found')
211 class SubscriptionGetDelRouter(Resource):
212
213     model = SubscriptionDTO.subscription_get
214
215     @api_ims_inventory_v1.doc('Get subscription by ID')
216     @api_ims_inventory_v1.marshal_with(model)
217     def get(self, subscriptionID):
218         result = ocloud_view.subscription_one(
219             subscriptionID, bus.uow)
220         if result is not None:
221             return result
222         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
223             subscriptionID))
224
225     @api_ims_inventory_v1.doc('Delete subscription by ID')
226     @api_ims_inventory_v1.response(204, 'Subscription deleted')
227     def delete(self, subscriptionID):
228         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
229         return result, 204