1 from flask_restful
import Resource
2 from flask
import request
, Response
7 from emuvim
.api
.openstack
.resources
.port_chain
import PortChain
8 from emuvim
.api
.openstack
.helper
import get_host
12 def __init__(self
, api
):
16 ###############################################################################
18 ###############################################################################
20 class PortPairsCreate(SFC
):
22 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
25 request_dict
= json
.loads(request
.data
).get("port_pair")
26 name
= request_dict
["name"]
28 ingress_port
= self
.api
.compute
.find_port_by_name_or_id(request_dict
["ingress"])
29 egress_port
= self
.api
.compute
.find_port_by_name_or_id(request_dict
["egress"])
31 port_pair
= self
.api
.compute
.create_port_pair(name
)
32 port_pair
.ingress
= ingress_port
33 port_pair
.egress
= egress_port
34 if "description" in request_dict
:
35 port_pair
.description
= request_dict
["description"]
36 if "service_function_parameters" in request_dict
:
37 port_pair
.service_function_parameters
= request_dict
["service_function_parameters"]
40 "port_pair": port_pair
.create_dict(self
.api
.compute
)
42 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
43 except Exception as ex
:
44 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
45 return Response(ex
.message
, status
=500, mimetype
='application/json')
48 class PortPairsUpdate(SFC
):
49 def put(self
, pair_id
):
50 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
53 request_dict
= json
.loads(request
.data
).get("port_pair")
54 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
55 if "name" in request_dict
:
56 port_pair
.name
= request_dict
["name"]
57 if "description" in request_dict
:
58 port_pair
.description
= request_dict
["description"]
61 "port_pair": port_pair
.create_dict(self
.api
.compute
)
63 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
64 except Exception as ex
:
65 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
66 return Response(ex
.message
, status
=500, mimetype
='application/json')
69 class PortPairsDelete(SFC
):
70 def delete(self
, pair_id
):
71 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
73 self
.api
.compute
.delete_port_pair(pair_id
)
75 return Response("Port pair %s deleted.\n" % pair_id
, status
=204, mimetype
='application/json')
76 except Exception as ex
:
77 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
78 return Response(ex
.message
, status
=500, mimetype
='application/json')
81 class PortPairsList(SFC
):
83 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
86 for port_pair
in self
.api
.compute
.port_pairs
.values():
87 port_pair_list
.append(port_pair
.create_dict(self
.api
.compute
))
88 resp
= {"port_pairs": port_pair_list
}
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 PortPairsShow(SFC
):
97 def get(self
, pair_id
):
98 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
101 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
103 "port_pair": port_pair
.create_dict(self
.api
.compute
)
105 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
106 except Exception as ex
:
107 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
108 return Response(ex
.message
, status
=500, mimetype
='application/json')
111 ###############################################################################
113 ###############################################################################
115 class PortPairGroupCreate(SFC
):
117 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
120 request_dict
= json
.loads(request
.data
).get("port_pair_group")
122 port_pair_group
= self
.api
.compute
.create_port_pair_group(request_dict
["name"])
123 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
124 if "description" in request_dict
:
125 port_pair_group
.description
= request_dict
["description"]
126 if "port_pair_group_parameters" in request_dict
:
127 port_pair_group
.port_pair_group_parameters
= request_dict
["port_pair_group_parameters"]
130 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
132 return Response(json
.dumps(resp
), status
=201, 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 class PortPairGroupUpdate(SFC
):
139 def put(self
, group_id
):
140 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
143 request_dict
= json
.loads(request
.data
).get("port_pair_group")
144 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
145 if "name" in request_dict
:
146 port_pair_group
.name
= request_dict
["name"]
147 if "description" in request_dict
:
148 port_pair_group
.description
= request_dict
["description"]
149 if "port_pairs" in request_dict
:
150 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
153 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
155 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
156 except Exception as ex
:
157 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
158 return Response(ex
.message
, status
=500, mimetype
='application/json')
161 class PortPairGroupDelete(SFC
):
162 def delete(self
, group_id
):
163 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
165 self
.api
.compute
.delete_port_pair_group(group_id
)
167 return Response("Port pair group %s deleted.\n" % group_id
, status
=204, mimetype
='application/json')
168 except Exception as ex
:
169 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
170 return Response(ex
.message
, status
=500, mimetype
='application/json')
173 class PortPairGroupList(SFC
):
175 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
177 port_pair_group_list
= []
178 for port_pair_group
in self
.api
.compute
.port_pair_groups
.values():
179 port_pair_group_list
.append(port_pair_group
.create_dict(self
.api
.compute
))
180 resp
= {"port_pair_groups": port_pair_group_list
}
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 PortPairGroupShow(SFC
):
189 def get(self
, group_id
):
190 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
193 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
195 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
197 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
198 except Exception as ex
:
199 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
200 return Response(ex
.message
, status
=500, mimetype
='application/json')
203 ###############################################################################
205 ###############################################################################
207 class FlowClassifierCreate(SFC
):
209 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
212 request_dict
= json
.loads(request
.data
).get("flow_classifier")
214 flow_classifier
= self
.api
.compute
.create_flow_classifier(request_dict
["name"])
215 if "description" in request_dict
:
216 flow_classifier
.description
= request_dict
["description"]
217 if "ethertype" in request_dict
:
218 flow_classifier
.ethertype
= request_dict
["ethertype"]
219 if "protocol" in request_dict
:
220 flow_classifier
.protocol
= request_dict
["protocol"]
221 if "source_port_range_min" in request_dict
:
222 flow_classifier
.source_port_range_min
= request_dict
["source_port_range_min"]
223 if "source_port_range_max" in request_dict
:
224 flow_classifier
.source_port_range_max
= request_dict
["source_port_range_max"]
225 if "destination_port_range_min" in request_dict
:
226 flow_classifier
.destination_port_range_min
= request_dict
["destination_port_range_min"]
227 if "destination_port_range_max" in request_dict
:
228 flow_classifier
.destination_port_range_max
= request_dict
["destination_port_range_max"]
229 if "source_ip_prefix" in request_dict
:
230 flow_classifier
.source_ip_prefix
= request_dict
["source_ip_prefix"]
231 if "destination_ip_prefix" in request_dict
:
232 flow_classifier
.destination_ip_prefix
= request_dict
["destination_ip_prefix"]
233 if "logical_source_port" in request_dict
:
234 flow_classifier
.logical_source_port
= request_dict
["logical_source_port"]
235 if "logical_destination_port" in request_dict
:
236 flow_classifier
.logical_destination_port
= request_dict
["logical_destination_port"]
237 if "l7_parameters" in request_dict
:
238 flow_classifier
.l7_parameters
= request_dict
["l7_parameters"]
241 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
243 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
244 except Exception as ex
:
245 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
246 return Response(ex
.message
, status
=500, mimetype
='application/json')
249 class FlowClassifierUpdate(SFC
):
250 def put(self
, flow_classifier_id
):
251 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
254 request_dict
= json
.loads(request
.data
).get("flow_classifier")
255 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
256 if "name" in request_dict
:
257 flow_classifier
.name
= request_dict
["name"]
258 if "description" in request_dict
:
259 flow_classifier
.description
= request_dict
["description"]
262 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
264 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
265 except Exception as ex
:
266 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
267 return Response(ex
.message
, status
=500, mimetype
='application/json')
270 class FlowClassifierDelete(SFC
):
271 def delete(self
, flow_classifier_id
):
272 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
274 self
.api
.compute
.delete_flow_classifier(flow_classifier_id
)
276 return Response("Port pair group %s deleted.\n" % flow_classifier_id
, status
=204,
277 mimetype
='application/json')
278 except Exception as ex
:
279 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
280 return Response(ex
.message
, status
=500, mimetype
='application/json')
283 class FlowClassifierList(SFC
):
285 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
287 flow_classifier_list
= []
288 for flow_classifier
in self
.api
.compute
.flow_classifiers
.values():
289 flow_classifier_list
.append(flow_classifier
.create_dict(self
.api
.compute
))
290 resp
= {"flow_classifiers": flow_classifier_list
}
292 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
293 except Exception as ex
:
294 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
295 return Response(ex
.message
, status
=500, mimetype
='application/json')
298 class FlowClassifierShow(SFC
):
299 def get(self
, flow_classifier_id
):
300 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
303 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
305 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
307 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
308 except Exception as ex
:
309 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
310 return Response(ex
.message
, status
=500, mimetype
='application/json')
313 ###############################################################################
315 ###############################################################################
317 class PortChainCreate(SFC
):
319 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
322 request_dict
= json
.loads(request
.data
).get("port_chain")
324 port_chain
= self
.api
.compute
.create_port_chain(request_dict
["name"])
325 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
326 if "description" in request_dict
:
327 port_chain
.description
= request_dict
["description"]
328 if "flow_classifiers" in request_dict
:
329 port_chain
.flow_classifiers
= request_dict
["flow_classifiers"]
330 if "chain_parameters" in request_dict
:
331 port_chain
.chain_parameters
= request_dict
["chain_parameters"]
333 port_chain
.install(self
.api
.compute
)
336 "port_chain": port_chain
.create_dict(self
.api
.compute
)
338 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
339 except Exception as ex
:
340 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
341 return Response(ex
.message
, status
=500, mimetype
='application/json')
344 class PortChainUpdate(SFC
):
345 def put(self
, chain_id
):
346 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
347 request_dict
= json
.loads(request
.data
).get("port_chain")
349 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
350 if "name" in request_dict
:
351 port_chain
.name
= request_dict
["name"]
352 if "description" in request_dict
:
353 port_chain
.description
= request_dict
["description"]
354 if "flow_classfiers" in request_dict
:
355 # TODO: update chain implementation
356 port_chain
.description
= request_dict
["flow_classifiers"]
357 if "no_flow_classfiers" in request_dict
:
358 port_chain
.description
= []
359 if "port_pair_groups" in request_dict
:
360 # TODO: update chain implementation
361 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
366 "port_chain": port_chain
.create_dict(self
.api
.compute
)
368 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
369 except Exception as ex
:
370 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
371 return Response(ex
.message
, status
=500, mimetype
='application/json')
374 class PortChainDelete(SFC
):
375 def delete(self
, chain_id
):
376 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
378 self
.api
.compute
.delete_port_chain(chain_id
)
380 return Response("Port chain %s deleted.\n" % chain_id
, status
=204, mimetype
='application/json')
381 except Exception as ex
:
382 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
383 return Response(ex
.message
, status
=500, mimetype
='application/json')
386 class PortChainList(SFC
):
388 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
391 for port_chain
in self
.api
.compute
.port_chains
.values():
392 port_chain_list
.append(port_chain
.create_dict(self
.api
.compute
))
393 resp
= {"port_chains": port_chain_list
}
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 PortChainShow(SFC
):
402 def get(self
, chain_id
):
403 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
406 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
408 "port_chain": port_chain
.create_dict(self
.api
.compute
)
410 return Response(json
.dumps(resp
), status
=200, 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')