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