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