1 from flask_restful
import Resource
2 from flask
import request
, Response
7 from emuvim
.api
.openstack
.resources
.port_chain
import PortChain
11 def __init__(self
, api
):
15 ###############################################################################
17 ###############################################################################
19 class PortPairsCreate(SFC
):
21 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
24 request_dict
= json
.loads(request
.data
).get("port_pair")
25 name
= request_dict
["name"]
27 ingress_port
= self
.api
.compute
.find_port_by_name_or_id(request_dict
["ingress"])
28 egress_port
= self
.api
.compute
.find_port_by_name_or_id(request_dict
["egress"])
30 port_pair
= self
.api
.compute
.create_port_pair(name
)
31 port_pair
.ingress
= ingress_port
32 port_pair
.egress
= egress_port
33 if "description" in request_dict
:
34 port_pair
.description
= request_dict
["description"]
35 if "service_function_parameters" in request_dict
:
36 port_pair
.service_function_parameters
= request_dict
["service_function_parameters"]
39 "port_pair": port_pair
.create_dict(self
.api
.compute
)
41 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
42 except Exception as ex
:
43 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
44 return Response(ex
.message
, status
=500, mimetype
='application/json')
47 class PortPairsUpdate(SFC
):
48 def put(self
, pair_id
):
49 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
52 request_dict
= json
.loads(request
.data
).get("port_pair")
53 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
54 if "name" in request_dict
:
55 port_pair
.name
= request_dict
["name"]
56 if "description" in request_dict
:
57 port_pair
.description
= request_dict
["description"]
60 "port_pair": port_pair
.create_dict(self
.api
.compute
)
62 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
63 except Exception as ex
:
64 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
65 return Response(ex
.message
, status
=500, mimetype
='application/json')
68 class PortPairsDelete(SFC
):
69 def delete(self
, pair_id
):
70 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
72 self
.api
.compute
.delete_port_pair(pair_id
)
74 return Response("Port pair %s deleted.\n" % pair_id
, status
=204, mimetype
='application/json')
75 except Exception as ex
:
76 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
77 return Response(ex
.message
, status
=500, mimetype
='application/json')
80 class PortPairsList(SFC
):
82 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
85 for port_pair
in self
.api
.compute
.port_pairs
.values():
86 port_pair_list
.append(port_pair
.create_dict(self
.api
.compute
))
87 resp
= {"port_pairs": port_pair_list
}
89 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
90 except Exception as ex
:
91 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
92 return Response(ex
.message
, status
=500, mimetype
='application/json')
95 class PortPairsShow(SFC
):
96 def get(self
, pair_id
):
97 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
100 port_pair
= self
.api
.compute
.find_port_pair_by_name_or_id(pair_id
)
102 "port_pair": port_pair
.create_dict(self
.api
.compute
)
104 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
105 except Exception as ex
:
106 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
107 return Response(ex
.message
, status
=500, mimetype
='application/json')
110 ###############################################################################
112 ###############################################################################
114 class PortPairGroupCreate(SFC
):
116 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
119 request_dict
= json
.loads(request
.data
).get("port_pair_group")
121 port_pair_group
= self
.api
.compute
.create_port_pair_group(request_dict
["name"])
122 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
123 if "description" in request_dict
:
124 port_pair_group
.description
= request_dict
["description"]
125 if "port_pair_group_parameters" in request_dict
:
126 port_pair_group
.port_pair_group_parameters
= request_dict
["port_pair_group_parameters"]
129 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
131 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
132 except Exception as ex
:
133 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
134 return Response(ex
.message
, status
=500, mimetype
='application/json')
137 class PortPairGroupUpdate(SFC
):
138 def put(self
, group_id
):
139 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
142 request_dict
= json
.loads(request
.data
).get("port_pair_group")
143 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
144 if "name" in request_dict
:
145 port_pair_group
.name
= request_dict
["name"]
146 if "description" in request_dict
:
147 port_pair_group
.description
= request_dict
["description"]
148 if "port_pairs" in request_dict
:
149 port_pair_group
.port_pairs
= request_dict
["port_pairs"]
152 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
154 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
155 except Exception as ex
:
156 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
157 return Response(ex
.message
, status
=500, mimetype
='application/json')
160 class PortPairGroupDelete(SFC
):
161 def delete(self
, group_id
):
162 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
164 self
.api
.compute
.delete_port_pair_group(group_id
)
166 return Response("Port pair group %s deleted.\n" % group_id
, status
=204, mimetype
='application/json')
167 except Exception as ex
:
168 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
169 return Response(ex
.message
, status
=500, mimetype
='application/json')
172 class PortPairGroupList(SFC
):
174 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
176 port_pair_group_list
= []
177 for port_pair_group
in self
.api
.compute
.port_pair_groups
.values():
178 port_pair_group_list
.append(port_pair_group
.create_dict(self
.api
.compute
))
179 resp
= {"port_pair_groups": port_pair_group_list
}
181 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
182 except Exception as ex
:
183 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
184 return Response(ex
.message
, status
=500, mimetype
='application/json')
187 class PortPairGroupShow(SFC
):
188 def get(self
, group_id
):
189 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
192 port_pair_group
= self
.api
.compute
.find_port_pair_group_by_name_or_id(group_id
)
194 "port_pair_group": port_pair_group
.create_dict(self
.api
.compute
)
196 return Response(json
.dumps(resp
), status
=200, 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 ###############################################################################
204 ###############################################################################
206 class FlowClassifierCreate(SFC
):
208 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
211 request_dict
= json
.loads(request
.data
).get("flow_classifier")
213 flow_classifier
= self
.api
.compute
.create_flow_classifier(request_dict
["name"])
214 if "description" in request_dict
:
215 flow_classifier
.description
= request_dict
["description"]
216 if "ethertype" in request_dict
:
217 flow_classifier
.ethertype
= request_dict
["ethertype"]
218 if "protocol" in request_dict
:
219 flow_classifier
.protocol
= request_dict
["protocol"]
220 if "source_port_range_min" in request_dict
:
221 flow_classifier
.source_port_range_min
= request_dict
["source_port_range_min"]
222 if "source_port_range_max" in request_dict
:
223 flow_classifier
.source_port_range_max
= request_dict
["source_port_range_max"]
224 if "destination_port_range_min" in request_dict
:
225 flow_classifier
.destination_port_range_min
= request_dict
["destination_port_range_min"]
226 if "destination_port_range_max" in request_dict
:
227 flow_classifier
.destination_port_range_max
= request_dict
["destination_port_range_max"]
228 if "source_ip_prefix" in request_dict
:
229 flow_classifier
.source_ip_prefix
= request_dict
["source_ip_prefix"]
230 if "destination_ip_prefix" in request_dict
:
231 flow_classifier
.destination_ip_prefix
= request_dict
["destination_ip_prefix"]
232 if "logical_source_port" in request_dict
:
233 flow_classifier
.logical_source_port
= request_dict
["logical_source_port"]
234 if "logical_destination_port" in request_dict
:
235 flow_classifier
.logical_destination_port
= request_dict
["logical_destination_port"]
236 if "l7_parameters" in request_dict
:
237 flow_classifier
.l7_parameters
= request_dict
["l7_parameters"]
240 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
242 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
243 except Exception as ex
:
244 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
245 return Response(ex
.message
, status
=500, mimetype
='application/json')
248 class FlowClassifierUpdate(SFC
):
249 def put(self
, flow_classifier_id
):
250 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
253 request_dict
= json
.loads(request
.data
).get("flow_classifier")
254 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
255 if "name" in request_dict
:
256 flow_classifier
.name
= request_dict
["name"]
257 if "description" in request_dict
:
258 flow_classifier
.description
= request_dict
["description"]
261 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
263 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
264 except Exception as ex
:
265 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
266 return Response(ex
.message
, status
=500, mimetype
='application/json')
269 class FlowClassifierDelete(SFC
):
270 def delete(self
, flow_classifier_id
):
271 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
273 self
.api
.compute
.delete_flow_classifier(flow_classifier_id
)
275 return Response("Port pair group %s deleted.\n" % flow_classifier_id
, status
=204,
276 mimetype
='application/json')
277 except Exception as ex
:
278 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
279 return Response(ex
.message
, status
=500, mimetype
='application/json')
282 class FlowClassifierList(SFC
):
284 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
286 flow_classifier_list
= []
287 for flow_classifier
in self
.api
.compute
.flow_classifiers
.values():
288 flow_classifier_list
.append(flow_classifier
.create_dict(self
.api
.compute
))
289 resp
= {"flow_classifiers": flow_classifier_list
}
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 FlowClassifierShow(SFC
):
298 def get(self
, flow_classifier_id
):
299 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
302 flow_classifier
= self
.api
.compute
.find_flow_classifier_by_name_or_id(flow_classifier_id
)
304 "flow_classifier": flow_classifier
.create_dict(self
.api
.compute
)
306 return Response(json
.dumps(resp
), status
=200, 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 ###############################################################################
314 ###############################################################################
316 class PortChainCreate(SFC
):
318 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
321 request_dict
= json
.loads(request
.data
).get("port_chain")
323 port_chain
= self
.api
.compute
.create_port_chain(request_dict
["name"])
324 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
325 if "description" in request_dict
:
326 port_chain
.description
= request_dict
["description"]
327 if "flow_classifiers" in request_dict
:
328 port_chain
.flow_classifiers
= request_dict
["flow_classifiers"]
329 if "chain_parameters" in request_dict
:
330 port_chain
.chain_parameters
= request_dict
["chain_parameters"]
332 port_chain
.install(self
.api
.compute
)
335 "port_chain": port_chain
.create_dict(self
.api
.compute
)
337 return Response(json
.dumps(resp
), status
=201, mimetype
='application/json')
338 except Exception as ex
:
339 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
340 return Response(ex
.message
, status
=500, mimetype
='application/json')
343 class PortChainUpdate(SFC
):
344 def put(self
, chain_id
):
345 logging
.debug("API CALL: %s PUT" % str(self
.__class
__.__name
__))
346 request_dict
= json
.loads(request
.data
).get("port_chain")
348 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
349 if "name" in request_dict
:
350 port_chain
.name
= request_dict
["name"]
351 if "description" in request_dict
:
352 port_chain
.description
= request_dict
["description"]
353 if "flow_classfiers" in request_dict
:
354 # TODO: update chain implementation
355 port_chain
.description
= request_dict
["flow_classifiers"]
356 if "no_flow_classfiers" in request_dict
:
357 port_chain
.description
= []
358 if "port_pair_groups" in request_dict
:
359 # TODO: update chain implementation
360 port_chain
.port_pair_groups
= request_dict
["port_pair_groups"]
365 "port_chain": port_chain
.create_dict(self
.api
.compute
)
367 return Response(json
.dumps(resp
), status
=200, 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 PortChainDelete(SFC
):
374 def delete(self
, chain_id
):
375 logging
.debug("API CALL: %s DELETE" % str(self
.__class
__.__name
__))
377 self
.api
.compute
.delete_port_chain(chain_id
)
379 return Response("Port chain %s deleted.\n" % chain_id
, status
=204, mimetype
='application/json')
380 except Exception as ex
:
381 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
382 return Response(ex
.message
, status
=500, mimetype
='application/json')
385 class PortChainList(SFC
):
387 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
390 for port_chain
in self
.api
.compute
.port_chains
.values():
391 port_chain_list
.append(port_chain
.create_dict(self
.api
.compute
))
392 resp
= {"port_chains": port_chain_list
}
394 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
395 except Exception as ex
:
396 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
397 return Response(ex
.message
, status
=500, mimetype
='application/json')
400 class PortChainShow(SFC
):
401 def get(self
, chain_id
):
402 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
405 port_chain
= self
.api
.compute
.find_port_chain_by_name_or_id(chain_id
)
407 "port_chain": port_chain
.create_dict(self
.api
.compute
)
409 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
410 except Exception as ex
:
411 logging
.exception("Neutron SFC: %s Exception." % str(self
.__class
__.__name
__))
412 return Response(ex
.message
, status
=500, mimetype
='application/json')