2 Copyright (c) 2017 SONATA-NFV and Paderborn University
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
28 from flask_restful
import Resource
29 from flask
import request
, Response
34 from emuvim
.api
.openstack
.resources
.port_chain
import PortChain
35 from emuvim
.api
.openstack
.helper
import get_host
39 def __init__(self
, api
):
43 ###############################################################################
45 ###############################################################################
47 class PortPairsCreate(SFC
):
49 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
52 request_dict
= json
.loads(request
.data
).get("port_pair")
53 name
= request_dict
["name"]
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"])
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"]
67 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
75 class PortPairsUpdate(SFC
):
76 def put(self
, pair_id
):
77 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
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"]
88 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
96 class PortPairsDelete(SFC
):
97 def delete(self
, pair_id
):
98 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
100 self
.api
.compute
.delete_port_pair(pair_id
)
102 return Response("Port pair %s deleted.\n" % pair_id
, status
=204, mimetype
='application/json')
103 except Exception as ex
:
104 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
105 return Response(ex
.message
, status
=500, mimetype
='application/json')
108 class PortPairsList(SFC
):
110 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
113 for port_pair
in self
.api
.compute
.port_pairs
.values():
114 port_pair_list
.append(port_pair
.create_dict(self
.api
.compute
))
115 resp
= {"port_pairs": port_pair_list
}
117 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
118 except Exception as ex
:
119 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
120 return Response(ex
.message
, status
=500, mimetype
='application/json')
123 class PortPairsShow(SFC
):
124 def get(self
, pair_id
):
125 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
128 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
130 "port_pair": port_pair
.create_dict(self
.api
.compute
)
132 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
133 except Exception as ex
:
134 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
135 return Response(ex
.message
, status
=500, mimetype
='application/json')
138 ###############################################################################
140 ###############################################################################
142 class PortPairGroupCreate(SFC
):
144 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
147 request_dict
= json
.loads(request
.data
).get("port_pair_group")
149 port_pair_group
= self
.api
.compute
.create_port_pair_group(request_dict
["name"])
150 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
151 if "description" in request_dict
:
152 port_pair_group
.description
= request_dict
["description"]
153 if "port_pair_group_parameters" in request_dict
:
154 port_pair_group
.port_pair_group_parameters
= request_dict
["port_pair_group_parameters"]
157 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
159 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
160 except Exception as ex
:
161 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
162 return Response(ex
.message
, status
=500, mimetype
='application/json')
165 class PortPairGroupUpdate(SFC
):
166 def put(self
, group_id
):
167 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
170 request_dict
= json
.loads(request
.data
).get("port_pair_group")
171 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
172 if "name" in request_dict
:
173 port_pair_group
.name
= request_dict
["name"]
174 if "description" in request_dict
:
175 port_pair_group
.description
= request_dict
["description"]
176 if "port_pairs" in request_dict
:
177 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
180 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
182 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
183 except Exception as ex
:
184 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
185 return Response(ex
.message
, status
=500, mimetype
='application/json')
188 class PortPairGroupDelete(SFC
):
189 def delete(self
, group_id
):
190 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
192 self
.api
.compute
.delete_port_pair_group(group_id
)
194 return Response("Port pair group %s deleted.\n" % group_id
, status
=204, mimetype
='application/json')
195 except Exception as ex
:
196 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
197 return Response(ex
.message
, status
=500, mimetype
='application/json')
200 class PortPairGroupList(SFC
):
202 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
204 port_pair_group_list
= []
205 for port_pair_group
in self
.api
.compute
.port_pair_groups
.values():
206 port_pair_group_list
.append(port_pair_group
.create_dict(self
.api
.compute
))
207 resp
= {"port_pair_groups": port_pair_group_list
}
209 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
210 except Exception as ex
:
211 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
212 return Response(ex
.message
, status
=500, mimetype
='application/json')
215 class PortPairGroupShow(SFC
):
216 def get(self
, group_id
):
217 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
220 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
222 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
224 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
225 except Exception as ex
:
226 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
227 return Response(ex
.message
, status
=500, mimetype
='application/json')
230 ###############################################################################
232 ###############################################################################
234 class FlowClassifierCreate(SFC
):
236 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
239 request_dict
= json
.loads(request
.data
).get("flow_classifier")
241 flow_classifier
= self
.api
.compute
.create_flow_classifier(request_dict
["name"])
242 if "description" in request_dict
:
243 flow_classifier
.description
= request_dict
["description"]
244 if "ethertype" in request_dict
:
245 flow_classifier
.ethertype
= request_dict
["ethertype"]
246 if "protocol" in request_dict
:
247 flow_classifier
.protocol
= request_dict
["protocol"]
248 if "source_port_range_min" in request_dict
:
249 flow_classifier
.source_port_range_min
= request_dict
["source_port_range_min"]
250 if "source_port_range_max" in request_dict
:
251 flow_classifier
.source_port_range_max
= request_dict
["source_port_range_max"]
252 if "destination_port_range_min" in request_dict
:
253 flow_classifier
.destination_port_range_min
= request_dict
["destination_port_range_min"]
254 if "destination_port_range_max" in request_dict
:
255 flow_classifier
.destination_port_range_max
= request_dict
["destination_port_range_max"]
256 if "source_ip_prefix" in request_dict
:
257 flow_classifier
.source_ip_prefix
= request_dict
["source_ip_prefix"]
258 if "destination_ip_prefix" in request_dict
:
259 flow_classifier
.destination_ip_prefix
= request_dict
["destination_ip_prefix"]
260 if "logical_source_port" in request_dict
:
261 flow_classifier
.logical_source_port
= request_dict
["logical_source_port"]
262 if "logical_destination_port" in request_dict
:
263 flow_classifier
.logical_destination_port
= request_dict
["logical_destination_port"]
264 if "l7_parameters" in request_dict
:
265 flow_classifier
.l7_parameters
= request_dict
["l7_parameters"]
268 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
270 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
271 except Exception as ex
:
272 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
273 return Response(ex
.message
, status
=500, mimetype
='application/json')
276 class FlowClassifierUpdate(SFC
):
277 def put(self
, flow_classifier_id
):
278 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
281 request_dict
= json
.loads(request
.data
).get("flow_classifier")
282 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
283 if "name" in request_dict
:
284 flow_classifier
.name
= request_dict
["name"]
285 if "description" in request_dict
:
286 flow_classifier
.description
= request_dict
["description"]
289 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
291 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
292 except Exception as ex
:
293 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
294 return Response(ex
.message
, status
=500, mimetype
='application/json')
297 class FlowClassifierDelete(SFC
):
298 def delete(self
, flow_classifier_id
):
299 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
301 self
.api
.compute
.delete_flow_classifier(flow_classifier_id
)
303 return Response("Port pair group %s deleted.\n" % flow_classifier_id
, status
=204,
304 mimetype
='application/json')
305 except Exception as ex
:
306 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
307 return Response(ex
.message
, status
=500, mimetype
='application/json')
310 class FlowClassifierList(SFC
):
312 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
314 flow_classifier_list
= []
315 for flow_classifier
in self
.api
.compute
.flow_classifiers
.values():
316 flow_classifier_list
.append(flow_classifier
.create_dict(self
.api
.compute
))
317 resp
= {"flow_classifiers": flow_classifier_list
}
319 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
320 except Exception as ex
:
321 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
322 return Response(ex
.message
, status
=500, mimetype
='application/json')
325 class FlowClassifierShow(SFC
):
326 def get(self
, flow_classifier_id
):
327 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
330 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
332 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
334 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
335 except Exception as ex
:
336 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
337 return Response(ex
.message
, status
=500, mimetype
='application/json')
340 ###############################################################################
342 ###############################################################################
344 class PortChainCreate(SFC
):
346 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
349 request_dict
= json
.loads(request
.data
).get("port_chain")
351 port_chain
= self
.api
.compute
.create_port_chain(request_dict
["name"])
352 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
353 if "description" in request_dict
:
354 port_chain
.description
= request_dict
["description"]
355 if "flow_classifiers" in request_dict
:
356 port_chain
.flow_classifiers
= request_dict
["flow_classifiers"]
357 if "chain_parameters" in request_dict
:
358 port_chain
.chain_parameters
= request_dict
["chain_parameters"]
360 port_chain
.install(self
.api
.compute
)
363 "port_chain": port_chain
.create_dict(self
.api
.compute
)
365 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
366 except Exception as ex
:
367 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
368 return Response(ex
.message
, status
=500, mimetype
='application/json')
371 class PortChainUpdate(SFC
):
372 def put(self
, chain_id
):
373 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
374 request_dict
= json
.loads(request
.data
).get("port_chain")
376 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
377 if "name" in request_dict
:
378 port_chain
.name
= request_dict
["name"]
379 if "description" in request_dict
:
380 port_chain
.description
= request_dict
["description"]
381 if "flow_classfiers" in request_dict
:
382 # TODO: update chain implementation
383 port_chain
.description
= request_dict
["flow_classifiers"]
384 if "no_flow_classfiers" in request_dict
:
385 port_chain
.description
= []
386 if "port_pair_groups" in request_dict
:
387 # TODO: update chain implementation
388 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
393 "port_chain": port_chain
.create_dict(self
.api
.compute
)
395 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
396 except Exception as ex
:
397 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
398 return Response(ex
.message
, status
=500, mimetype
='application/json')
401 class PortChainDelete(SFC
):
402 def delete(self
, chain_id
):
403 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
405 self
.api
.compute
.delete_port_chain(chain_id
)
407 return Response("Port chain %s deleted.\n" % chain_id
, status
=204, mimetype
='application/json')
408 except Exception as ex
:
409 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
410 return Response(ex
.message
, status
=500, mimetype
='application/json')
413 class PortChainList(SFC
):
415 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
418 for port_chain
in self
.api
.compute
.port_chains
.values():
419 port_chain_list
.append(port_chain
.create_dict(self
.api
.compute
))
420 resp
= {"port_chains": port_chain_list
}
422 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
423 except Exception as ex
:
424 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
425 return Response(ex
.message
, status
=500, mimetype
='application/json')
428 class PortChainShow(SFC
):
429 def get(self
, chain_id
):
430 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
433 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
435 "port_chain": port_chain
.create_dict(self
.api
.compute
)
437 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
438 except Exception as ex
:
439 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
440 return Response(ex
.message
, status
=500, mimetype
='application/json')