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