1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
33 def __init__(self
, api
):
37 ###############################################################################
39 ###############################################################################
41 class PortPairsCreate(SFC
):
43 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
46 request_dict
= json
.loads(request
.data
).get("port_pair")
47 name
= request_dict
["name"]
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"])
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"]
63 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
74 class PortPairsUpdate(SFC
):
75 def put(self
, pair_id
):
76 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
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"]
87 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
98 class PortPairsDelete(SFC
):
99 def delete(self
, pair_id
):
100 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
102 self
.api
.compute
.delete_port_pair(pair_id
)
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')
113 class PortPairsList(SFC
):
115 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
131 class PortPairsShow(SFC
):
132 def get(self
, pair_id
):
133 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
136 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
138 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
149 ###############################################################################
151 ###############################################################################
153 class PortPairGroupCreate(SFC
):
155 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
158 request_dict
= json
.loads(request
.data
).get("port_pair_group")
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"]
170 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
181 class PortPairGroupUpdate(SFC
):
182 def put(self
, group_id
):
183 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
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(
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"]
197 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
208 class PortPairGroupDelete(SFC
):
209 def delete(self
, group_id
):
210 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
212 self
.api
.compute
.delete_port_pair_group(group_id
)
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')
223 class PortPairGroupList(SFC
):
225 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
242 class PortPairGroupShow(SFC
):
243 def get(self
, group_id
):
244 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
247 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(
250 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
261 ###############################################################################
263 ###############################################################################
265 class FlowClassifierCreate(SFC
):
267 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
270 request_dict
= json
.loads(request
.data
).get("flow_classifier")
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"]
302 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
313 class FlowClassifierUpdate(SFC
):
314 def put(self
, flow_classifier_id
):
315 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
318 request_dict
= json
.loads(request
.data
).get("flow_classifier")
319 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_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"]
327 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
338 class FlowClassifierDelete(SFC
):
339 def delete(self
, flow_classifier_id
):
340 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
342 self
.api
.compute
.delete_flow_classifier(flow_classifier_id
)
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')
353 class FlowClassifierList(SFC
):
355 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
372 class FlowClassifierShow(SFC
):
373 def get(self
, flow_classifier_id
):
374 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
377 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(
380 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
391 ###############################################################################
393 ###############################################################################
395 class PortChainCreate(SFC
):
397 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
400 request_dict
= json
.loads(request
.data
).get("port_chain")
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"]
412 port_chain
.install(self
.api
.compute
)
415 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')
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")
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"]
448 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')
459 class PortChainDelete(SFC
):
460 def delete(self
, chain_id
):
461 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
463 self
.api
.compute
.delete_port_chain(chain_id
)
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')
474 class PortChainList(SFC
):
476 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
493 class PortChainShow(SFC
):
494 def get(self
, chain_id
):
495 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
498 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(
501 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')