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