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("", 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')
109 class PortPairsList(SFC
):
111 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
124 class PortPairsShow(SFC
):
125 def get(self
, pair_id
):
126 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
129 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
131 "port_pair": port_pair
.create_dict(self
.api
.compute
)
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')
139 ###############################################################################
141 ###############################################################################
143 class PortPairGroupCreate(SFC
):
145 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
148 request_dict
= json
.loads(request
.data
).get("port_pair_group")
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"]
158 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
166 class PortPairGroupUpdate(SFC
):
167 def put(self
, group_id
):
168 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
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"]
181 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
189 class PortPairGroupDelete(SFC
):
190 def delete(self
, group_id
):
191 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
193 self
.api
.compute
.delete_port_pair_group(group_id
)
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')
202 class PortPairGroupList(SFC
):
204 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
217 class PortPairGroupShow(SFC
):
218 def get(self
, group_id
):
219 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
222 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
224 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
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')
232 ###############################################################################
234 ###############################################################################
236 class FlowClassifierCreate(SFC
):
238 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
241 request_dict
= json
.loads(request
.data
).get("flow_classifier")
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"]
270 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
278 class FlowClassifierUpdate(SFC
):
279 def put(self
, flow_classifier_id
):
280 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
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"]
291 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
299 class FlowClassifierDelete(SFC
):
300 def delete(self
, flow_classifier_id
):
301 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
303 self
.api
.compute
.delete_flow_classifier(flow_classifier_id
)
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')
312 class FlowClassifierList(SFC
):
314 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
327 class FlowClassifierShow(SFC
):
328 def get(self
, flow_classifier_id
):
329 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
332 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
334 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
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')
342 ###############################################################################
344 ###############################################################################
346 class PortChainCreate(SFC
):
348 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
351 request_dict
= json
.loads(request
.data
).get("port_chain")
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"]
362 port_chain
.install(self
.api
.compute
)
365 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')
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")
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"]
395 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')
403 class PortChainDelete(SFC
):
404 def delete(self
, chain_id
):
405 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
407 self
.api
.compute
.delete_port_chain(chain_id
)
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')
416 class PortChainList(SFC
):
418 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
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
}
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')
431 class PortChainShow(SFC
):
432 def get(self
, chain_id
):
433 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
436 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
438 "port_chain": port_chain
.create_dict(self
.api
.compute
)
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')