0fc6e692847336fb09e0c696da729b05e4535e99
[pti/o2.git] / o2dms / views / dms_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 import jsonify
16 from flask_restx import Resource
17
18 from o2common.config import config
19 from o2dms.views.dms_dto import DmsDTO, DmsLcmNfDeploymentDescriptorDTO
20 from o2dms.views.dms_dto import DmsLcmNfDeploymentDTO
21 from o2dms.views.dms_dto import DmsLcmNfOCloudVResourceDTO
22 from o2dms.views import dms_lcm_view, api_dms_lcm_v1
23
24 apibase = config.get_o2dms_api_base()
25
26
27 # ----------  DeploymentManagers ---------- #
28 @api_dms_lcm_v1.route("/<deploymentManagerID>")
29 @api_dms_lcm_v1.param('deploymentManagerID', 'ID of the deployment manager')
30 @api_dms_lcm_v1.response(404, 'Deployment manager not found')
31 class DmsGetRouter(Resource):
32
33     model = DmsDTO.dms_get
34
35     @api_dms_lcm_v1.doc('Get deployment manager')
36     @api_dms_lcm_v1.marshal_with(model)
37     def get(self, deploymentManagerID):
38         result = dms_lcm_view.deployment_manager_one(
39             deploymentManagerID, bus.uow)
40         if result is not None:
41             return result
42         api_dms_lcm_v1.abort(404, "Deployment manager {} doesn't exist".format(
43             deploymentManagerID))
44
45
46 # LCM services #
47 @api_dms_lcm_v1\
48     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
49            "NfDeploymentDescriptor")
50 @api_dms_lcm_v1\
51     .param('deploymentManagerID', 'ID of the deployment manager')
52 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
53 class DmsLcmNfDeploymentDescListRouter(Resource):
54
55     model = DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_get
56
57     createdto = DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_create
58     post_resp = DmsLcmNfDeploymentDescriptorDTO.\
59         NfDeploymentDescriptor_create_post_resp
60
61     @api_dms_lcm_v1.doc('Get a list of NfDeploymentDescriptor')
62     @api_dms_lcm_v1.marshal_list_with(model)
63     def get(self, deploymentManagerID):
64         return dms_lcm_view.lcm_nfdeploymentdesc_list(
65             deploymentManagerID, bus.uow)
66
67     @api_dms_lcm_v1.doc('Create a NfDeploymentDescriptor')
68     @api_dms_lcm_v1.expect(createdto)
69     @api_dms_lcm_v1.marshal_with(post_resp, code=201)
70     def post(self, deploymentManagerID):
71         data = api_dms_lcm_v1.payload
72         id = dms_lcm_view.lcm_nfdeploymentdesc_create(
73             deploymentManagerID, data, bus.uow)
74         return {"id": id}, 201
75
76
77 @api_dms_lcm_v1\
78     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
79            "NfDeploymentDescriptor/<nfDeploymentDescriptorId>")
80 @api_dms_lcm_v1\
81     .param('deploymentManagerID', 'ID of the deployment manager')
82 @api_dms_lcm_v1.param('nfDeploymentDescriptorId',
83                       'ID of the NfDeploymentDescriptor')
84 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
85 class DmsLcmNfDeploymentDescGetRouter(Resource):
86
87     model = DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_get
88     updatedto = DmsLcmNfDeploymentDescriptorDTO.\
89         NfDeploymentDescriptor_update
90
91     @api_dms_lcm_v1.doc('Get a NfDeploymentDescriptor')
92     @api_dms_lcm_v1.marshal_with(model)
93     def get(self, nfDeploymentDescriptorId, deploymentManagerID):
94         result = dms_lcm_view\
95             .lcm_nfdeploymentdesc_one(nfDeploymentDescriptorId, bus.uow)
96         if result is not None:
97             return result
98         api_dms_lcm_v1.abort(
99             404, "NfDeploymentDescriptor {} doesn't exist".format(
100                 nfDeploymentDescriptorId))
101
102     @api_dms_lcm_v1.doc('Update a NfDeploymentDescriptor')
103     @api_dms_lcm_v1.expect(updatedto)
104     def put(self, nfDeploymentDescriptorId, deploymentManagerID):
105         data = api_dms_lcm_v1.payload
106         dms_lcm_view.lcm_nfdeploymentdesc_update(
107             nfDeploymentDescriptorId, data, bus.uow)
108         return {}, 201
109
110     @api_dms_lcm_v1.doc('Delete NfDeploymentDescriptor by ID')
111     @api_dms_lcm_v1.response(204, 'NfDeploymentDescriptor deleted')
112     def delete(self, nfDeploymentDescriptorId, deploymentManagerID):
113         with bus.uow:
114             bus.uow.nfdeployment_descs.delete(nfDeploymentDescriptorId)
115             bus.uow.commit()
116         return '', 204
117
118
119 # LCM services #
120 @api_dms_lcm_v1\
121     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
122            "NfDeployment")
123 @api_dms_lcm_v1\
124     .param('deploymentManagerID', 'ID of the deployment manager')
125 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
126 class DmsLcmNfDeploymentListRouter(Resource):
127
128     model = DmsLcmNfDeploymentDTO.NfDeployment_get
129
130     createdto = DmsLcmNfDeploymentDTO.NfDeployment_create
131     post_resp = DmsLcmNfDeploymentDTO.\
132         NfDeployment_create_post_resp
133
134     @api_dms_lcm_v1.doc('Get a list of NfDeployment')
135     @api_dms_lcm_v1.marshal_list_with(model)
136     def get(self, deploymentManagerID):
137         return dms_lcm_view.lcm_nfdeployment_list(
138             deploymentManagerID, bus.uow)
139
140     @api_dms_lcm_v1.doc('Create a NfDeployment')
141     @api_dms_lcm_v1.expect(createdto)
142     @api_dms_lcm_v1.marshal_with(post_resp, code=201)
143     def post(self, deploymentManagerID):
144         data = api_dms_lcm_v1.payload
145         id = dms_lcm_view.lcm_nfdeployment_create(
146             deploymentManagerID, data, bus.uow)
147         return {"id": id}, 201
148
149
150 @api_dms_lcm_v1\
151     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
152            "NfDeployment/<nfDeploymentId>")
153 @api_dms_lcm_v1\
154     .param('deploymentManagerID', 'ID of the deployment manager')
155 @api_dms_lcm_v1.param('nfDeploymentId',
156                       'ID of the NfDeployment')
157 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
158 class DmsLcmNfDeploymentGetRouter(Resource):
159
160     model = DmsLcmNfDeploymentDTO.NfDeployment_get
161     updatedto = DmsLcmNfDeploymentDTO.\
162         NfDeployment_update
163
164     @api_dms_lcm_v1.doc('Get a NfDeploymentDescriptor')
165     @api_dms_lcm_v1.marshal_with(model)
166     def get(self, nfDeploymentId, deploymentManagerID):
167         result = dms_lcm_view\
168             .lcm_nfdeployment_one(nfDeploymentId, bus.uow)
169         if result is not None:
170             return result
171         api_dms_lcm_v1.abort(
172             404, "NfDeploymentDescriptor {} doesn't exist".format(
173                 nfDeploymentId))
174
175     @api_dms_lcm_v1.doc('Update a NfDeployment')
176     @api_dms_lcm_v1.expect(updatedto)
177     def put(self, nfDeploymentId, deploymentManagerID):
178         data = api_dms_lcm_v1.payload
179         dms_lcm_view.lcm_nfdeployment_update(
180             nfDeploymentId, data, bus.uow)
181         return {}, 201
182
183     @api_dms_lcm_v1.doc('Delete NfDeployment by ID')
184     @api_dms_lcm_v1.response(204, 'NfDeployment deleted')
185     def delete(self, nfDeploymentId, deploymentManagerID):
186         with bus.uow:
187             bus.uow.nfdeployments.delete(nfDeploymentId)
188             bus.uow.commit()
189         return '', 204
190
191
192 # LCM services #
193 @api_dms_lcm_v1\
194     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
195            "NfDeployment/<nfDeploymentId>/NfOCloudVirtualResource")
196 @api_dms_lcm_v1\
197     .param('deploymentManagerID', 'ID of the Deployment Manager')
198 @api_dms_lcm_v1.param('nfDeploymentId',
199                       'ID of the NfDeployment')
200 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
201 class DmsLcmNfOCloudVResListRouter(Resource):
202
203     model = DmsLcmNfOCloudVResourceDTO.NfOCloudVResource_get
204
205     @api_dms_lcm_v1.doc('Get a list of NfOCloudVirtualResource')
206     @api_dms_lcm_v1.marshal_list_with(model)
207     def get(self, nfDeploymentId, deploymentManagerID):
208         return dms_lcm_view.lcm_nfocloudvresource_list(
209             nfDeploymentId, bus.uow)
210
211
212 @api_dms_lcm_v1\
213     .route("/<deploymentManagerID>/O2dms_DeploymentLifecycle/"
214            "NfDeployment/<nfDeploymentId>"
215            "NfOCloudVirtualResource/<nfOCloudVirtualResourceId>")
216 @api_dms_lcm_v1\
217     .param('deploymentManagerID', 'ID of the deployment manager')
218 @api_dms_lcm_v1.param('nfDeploymentId',
219                       'ID of the NfDeployment')
220 @api_dms_lcm_v1.param('nfOCloudVirtualResourceId',
221                       'ID of the NfOCloudVirtualResource')
222 @api_dms_lcm_v1.response(404, 'DMS LCM not found')
223 class DmsLcmNfOCloudVResGetRouter(Resource):
224
225     model = DmsLcmNfOCloudVResourceDTO.NfOCloudVResource_get
226
227     @api_dms_lcm_v1.doc('Get a NfOCloudVirtualResource')
228     @api_dms_lcm_v1.marshal_with(model)
229     def get(self, nfOCloudVirtualResourceId,
230             nfDeploymentId, deploymentManagerID):
231         result = dms_lcm_view\
232             .lcm_nfocloudvresource_one(nfOCloudVirtualResourceId, bus.uow)
233         if result is not None:
234             return result
235         api_dms_lcm_v1.abort(
236             404, "NfOCloudVirtualResource {} doesn't exist".format(
237                 nfOCloudVirtualResourceId))
238
239
240 def configure_namespace(app, bus_new):
241     app.add_namespace(api_dms_lcm_v1, path=apibase)
242
243     # Set global uow
244     global bus
245     bus = bus_new