Fix: HTTP 204 response must not contain any payload.
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / neutron_sfc_dummy_api.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 Neither the name of the SONATA-NFV, Paderborn University
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28 from flask_restful import Resource
29 from flask import request, Response
30 import logging
31 import json
32 import uuid
33
34 from emuvim.api.openstack.resources.port_chain import PortChain
35 from emuvim.api.openstack.helper import get_host
36
37
38 class SFC(Resource):
39 def __init__(self, api):
40 self.api = api
41
42
43 ###############################################################################
44 # Port Pair
45 ###############################################################################
46
47 class PortPairsCreate(SFC):
48 def post(self):
49 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
50
51 try:
52 request_dict = json.loads(request.data).get("port_pair")
53 name = request_dict["name"]
54
55 ingress_port = self.api.compute.find_port_by_name_or_id(request_dict["ingress"])
56 egress_port = self.api.compute.find_port_by_name_or_id(request_dict["egress"])
57
58 port_pair = self.api.compute.create_port_pair(name)
59 port_pair.ingress = ingress_port
60 port_pair.egress = egress_port
61 if "description" in request_dict:
62 port_pair.description = request_dict["description"]
63 if "service_function_parameters" in request_dict:
64 port_pair.service_function_parameters = request_dict["service_function_parameters"]
65
66 resp = {
67 "port_pair": port_pair.create_dict(self.api.compute)
68 }
69 return Response(json.dumps(resp), status=201, mimetype='application/json')
70 except Exception as ex:
71 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
72 return Response(ex.message, status=500, mimetype='application/json')
73
74
75 class PortPairsUpdate(SFC):
76 def put(self, pair_id):
77 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
78
79 try:
80 request_dict = json.loads(request.data).get("port_pair")
81 port_pair = self.api.compute.find_port_pair_by_name_or_id(pair_id)
82 if "name" in request_dict:
83 port_pair.name = request_dict["name"]
84 if "description" in request_dict:
85 port_pair.description = request_dict["description"]
86
87 resp = {
88 "port_pair": port_pair.create_dict(self.api.compute)
89 }
90 return Response(json.dumps(resp), status=200, mimetype='application/json')
91 except Exception as ex:
92 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
93 return Response(ex.message, status=500, mimetype='application/json')
94
95
96 class PortPairsDelete(SFC):
97 def delete(self, pair_id):
98 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
99 try:
100 self.api.compute.delete_port_pair(pair_id)
101
102 return Response("", status=204,
103 mimetype='application/json')
104 except Exception as ex:
105 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
106 return Response(ex.message, status=500, mimetype='application/json')
107
108
109 class PortPairsList(SFC):
110 def get(self):
111 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
112 try:
113 port_pair_list = []
114 for port_pair in self.api.compute.port_pairs.values():
115 port_pair_list.append(port_pair.create_dict(self.api.compute))
116 resp = {"port_pairs": port_pair_list}
117
118 return Response(json.dumps(resp), status=200, mimetype='application/json')
119 except Exception as ex:
120 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
121 return Response(ex.message, status=500, mimetype='application/json')
122
123
124 class PortPairsShow(SFC):
125 def get(self, pair_id):
126 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
127
128 try:
129 port_pair = self.api.compute.find_port_pair_by_name_or_id(pair_id)
130 resp = {
131 "port_pair": port_pair.create_dict(self.api.compute)
132 }
133 return Response(json.dumps(resp), status=200, mimetype='application/json')
134 except Exception as ex:
135 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
136 return Response(ex.message, status=500, mimetype='application/json')
137
138
139 ###############################################################################
140 # Port Pair Group
141 ###############################################################################
142
143 class PortPairGroupCreate(SFC):
144 def post(self):
145 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
146
147 try:
148 request_dict = json.loads(request.data).get("port_pair_group")
149
150 port_pair_group = self.api.compute.create_port_pair_group(request_dict["name"])
151 port_pair_group.port_pairs = request_dict["port_pairs"]
152 if "description" in request_dict:
153 port_pair_group.description = request_dict["description"]
154 if "port_pair_group_parameters" in request_dict:
155 port_pair_group.port_pair_group_parameters = request_dict["port_pair_group_parameters"]
156
157 resp = {
158 "port_pair_group": port_pair_group.create_dict(self.api.compute)
159 }
160 return Response(json.dumps(resp), status=201, mimetype='application/json')
161 except Exception as ex:
162 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
163 return Response(ex.message, status=500, mimetype='application/json')
164
165
166 class PortPairGroupUpdate(SFC):
167 def put(self, group_id):
168 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
169
170 try:
171 request_dict = json.loads(request.data).get("port_pair_group")
172 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
173 if "name" in request_dict:
174 port_pair_group.name = request_dict["name"]
175 if "description" in request_dict:
176 port_pair_group.description = request_dict["description"]
177 if "port_pairs" in request_dict:
178 port_pair_group.port_pairs = request_dict["port_pairs"]
179
180 resp = {
181 "port_pair_group": port_pair_group.create_dict(self.api.compute)
182 }
183 return Response(json.dumps(resp), status=200, mimetype='application/json')
184 except Exception as ex:
185 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
186 return Response(ex.message, status=500, mimetype='application/json')
187
188
189 class PortPairGroupDelete(SFC):
190 def delete(self, group_id):
191 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
192 try:
193 self.api.compute.delete_port_pair_group(group_id)
194
195 return Response("", status=204,
196 mimetype='application/json')
197 except Exception as ex:
198 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
199 return Response(ex.message, status=500, mimetype='application/json')
200
201
202 class PortPairGroupList(SFC):
203 def get(self):
204 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
205 try:
206 port_pair_group_list = []
207 for port_pair_group in self.api.compute.port_pair_groups.values():
208 port_pair_group_list.append(port_pair_group.create_dict(self.api.compute))
209 resp = {"port_pair_groups": port_pair_group_list}
210
211 return Response(json.dumps(resp), status=200, mimetype='application/json')
212 except Exception as ex:
213 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
214 return Response(ex.message, status=500, mimetype='application/json')
215
216
217 class PortPairGroupShow(SFC):
218 def get(self, group_id):
219 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
220
221 try:
222 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
223 resp = {
224 "port_pair_group": port_pair_group.create_dict(self.api.compute)
225 }
226 return Response(json.dumps(resp), status=200, mimetype='application/json')
227 except Exception as ex:
228 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
229 return Response(ex.message, status=500, mimetype='application/json')
230
231
232 ###############################################################################
233 # Flow Classifier
234 ###############################################################################
235
236 class FlowClassifierCreate(SFC):
237 def post(self):
238 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
239
240 try:
241 request_dict = json.loads(request.data).get("flow_classifier")
242
243 flow_classifier = self.api.compute.create_flow_classifier(request_dict["name"])
244 if "description" in request_dict:
245 flow_classifier.description = request_dict["description"]
246 if "ethertype" in request_dict:
247 flow_classifier.ethertype = request_dict["ethertype"]
248 if "protocol" in request_dict:
249 flow_classifier.protocol = request_dict["protocol"]
250 if "source_port_range_min" in request_dict:
251 flow_classifier.source_port_range_min = request_dict["source_port_range_min"]
252 if "source_port_range_max" in request_dict:
253 flow_classifier.source_port_range_max = request_dict["source_port_range_max"]
254 if "destination_port_range_min" in request_dict:
255 flow_classifier.destination_port_range_min = request_dict["destination_port_range_min"]
256 if "destination_port_range_max" in request_dict:
257 flow_classifier.destination_port_range_max = request_dict["destination_port_range_max"]
258 if "source_ip_prefix" in request_dict:
259 flow_classifier.source_ip_prefix = request_dict["source_ip_prefix"]
260 if "destination_ip_prefix" in request_dict:
261 flow_classifier.destination_ip_prefix = request_dict["destination_ip_prefix"]
262 if "logical_source_port" in request_dict:
263 flow_classifier.logical_source_port = request_dict["logical_source_port"]
264 if "logical_destination_port" in request_dict:
265 flow_classifier.logical_destination_port = request_dict["logical_destination_port"]
266 if "l7_parameters" in request_dict:
267 flow_classifier.l7_parameters = request_dict["l7_parameters"]
268
269 resp = {
270 "flow_classifier": flow_classifier.create_dict(self.api.compute)
271 }
272 return Response(json.dumps(resp), status=201, mimetype='application/json')
273 except Exception as ex:
274 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
275 return Response(ex.message, status=500, mimetype='application/json')
276
277
278 class FlowClassifierUpdate(SFC):
279 def put(self, flow_classifier_id):
280 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
281
282 try:
283 request_dict = json.loads(request.data).get("flow_classifier")
284 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
285 if "name" in request_dict:
286 flow_classifier.name = request_dict["name"]
287 if "description" in request_dict:
288 flow_classifier.description = request_dict["description"]
289
290 resp = {
291 "flow_classifier": flow_classifier.create_dict(self.api.compute)
292 }
293 return Response(json.dumps(resp), status=200, mimetype='application/json')
294 except Exception as ex:
295 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
296 return Response(ex.message, status=500, mimetype='application/json')
297
298
299 class FlowClassifierDelete(SFC):
300 def delete(self, flow_classifier_id):
301 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
302 try:
303 self.api.compute.delete_flow_classifier(flow_classifier_id)
304
305 return Response("", status=204,
306 mimetype='application/json')
307 except Exception as ex:
308 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
309 return Response(ex.message, status=500, mimetype='application/json')
310
311
312 class FlowClassifierList(SFC):
313 def get(self):
314 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
315 try:
316 flow_classifier_list = []
317 for flow_classifier in self.api.compute.flow_classifiers.values():
318 flow_classifier_list.append(flow_classifier.create_dict(self.api.compute))
319 resp = {"flow_classifiers": flow_classifier_list}
320
321 return Response(json.dumps(resp), status=200, mimetype='application/json')
322 except Exception as ex:
323 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
324 return Response(ex.message, status=500, mimetype='application/json')
325
326
327 class FlowClassifierShow(SFC):
328 def get(self, flow_classifier_id):
329 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
330
331 try:
332 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
333 resp = {
334 "flow_classifier": flow_classifier.create_dict(self.api.compute)
335 }
336 return Response(json.dumps(resp), status=200, mimetype='application/json')
337 except Exception as ex:
338 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
339 return Response(ex.message, status=500, mimetype='application/json')
340
341
342 ###############################################################################
343 # Port Chain
344 ###############################################################################
345
346 class PortChainCreate(SFC):
347 def post(self):
348 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
349
350 try:
351 request_dict = json.loads(request.data).get("port_chain")
352
353 port_chain = self.api.compute.create_port_chain(request_dict["name"])
354 port_chain.port_pair_groups = request_dict["port_pair_groups"]
355 if "description" in request_dict:
356 port_chain.description = request_dict["description"]
357 if "flow_classifiers" in request_dict:
358 port_chain.flow_classifiers = request_dict["flow_classifiers"]
359 if "chain_parameters" in request_dict:
360 port_chain.chain_parameters = request_dict["chain_parameters"]
361
362 port_chain.install(self.api.compute)
363
364 resp = {
365 "port_chain": port_chain.create_dict(self.api.compute)
366 }
367 return Response(json.dumps(resp), status=201, mimetype='application/json')
368 except Exception as ex:
369 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
370 return Response(ex.message, status=500, mimetype='application/json')
371
372
373 class PortChainUpdate(SFC):
374 def put(self, chain_id):
375 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
376 request_dict = json.loads(request.data).get("port_chain")
377
378 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
379 if "name" in request_dict:
380 port_chain.name = request_dict["name"]
381 if "description" in request_dict:
382 port_chain.description = request_dict["description"]
383 if "flow_classfiers" in request_dict:
384 # TODO: update chain implementation
385 port_chain.description = request_dict["flow_classifiers"]
386 if "no_flow_classfiers" in request_dict:
387 port_chain.description = []
388 if "port_pair_groups" in request_dict:
389 # TODO: update chain implementation
390 port_chain.port_pair_groups = request_dict["port_pair_groups"]
391
392 port_chain.update()
393 try:
394 resp = {
395 "port_chain": port_chain.create_dict(self.api.compute)
396 }
397 return Response(json.dumps(resp), status=200, mimetype='application/json')
398 except Exception as ex:
399 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
400 return Response(ex.message, status=500, mimetype='application/json')
401
402
403 class PortChainDelete(SFC):
404 def delete(self, chain_id):
405 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
406
407 self.api.compute.delete_port_chain(chain_id)
408 try:
409 return Response("", status=204,
410 mimetype='application/json')
411 except Exception as ex:
412 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
413 return Response(ex.message, status=500, mimetype='application/json')
414
415
416 class PortChainList(SFC):
417 def get(self):
418 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
419 try:
420 port_chain_list = []
421 for port_chain in self.api.compute.port_chains.values():
422 port_chain_list.append(port_chain.create_dict(self.api.compute))
423 resp = {"port_chains": port_chain_list}
424
425 return Response(json.dumps(resp), status=200, mimetype='application/json')
426 except Exception as ex:
427 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
428 return Response(ex.message, status=500, mimetype='application/json')
429
430
431 class PortChainShow(SFC):
432 def get(self, chain_id):
433 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
434
435 try:
436 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
437 resp = {
438 "port_chain": port_chain.create_dict(self.api.compute)
439 }
440 return Response(json.dumps(resp), status=200, mimetype='application/json')
441 except Exception as ex:
442 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
443 return Response(ex.message, status=500, mimetype='application/json')