93121c39d269a111af13bed471e0c198488ab791
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / neutron_sfc_dummy_api.py
1 from flask_restful import Resource
2 from flask import request, Response
3 import logging
4 import json
5 import uuid
6
7 from emuvim.api.openstack.resources.port_chain import PortChain
8
9
10 class SFC(Resource):
11 def __init__(self, api):
12 self.api = api
13
14
15 ###############################################################################
16 # Port Pair
17 ###############################################################################
18
19 class PortPairsCreate(SFC):
20 def post(self):
21 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
22
23 try:
24 request_dict = json.loads(request.data).get("port_pair")
25 name = request_dict["name"]
26
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"])
29
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"]
37
38 resp = {
39 "port_pair": port_pair.create_dict(self.api.compute)
40 }
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')
45
46
47 class PortPairsUpdate(SFC):
48 def put(self, pair_id):
49 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
50
51 try:
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"]
58
59 resp = {
60 "port_pair": port_pair.create_dict(self.api.compute)
61 }
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')
66
67
68 class PortPairsDelete(SFC):
69 def delete(self, pair_id):
70 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
71 try:
72 self.api.compute.delete_port_pair(pair_id)
73
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')
78
79
80 class PortPairsList(SFC):
81 def get(self):
82 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
83 try:
84 port_pair_list = []
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}
88
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')
93
94
95 class PortPairsShow(SFC):
96 def get(self, pair_id):
97 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
98
99 try:
100 port_pair = self.api.compute.find_port_pair_by_name_or_id(pair_id)
101 resp = {
102 "port_pair": port_pair.create_dict(self.api.compute)
103 }
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')
108
109
110 ###############################################################################
111 # Port Pair Group
112 ###############################################################################
113
114 class PortPairGroupCreate(SFC):
115 def post(self):
116 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
117
118 try:
119 request_dict = json.loads(request.data).get("port_pair_group")
120
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"]
127
128 resp = {
129 "port_pair_group": port_pair_group.create_dict(self.api.compute)
130 }
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')
135
136
137 class PortPairGroupUpdate(SFC):
138 def put(self, group_id):
139 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
140
141 try:
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"]
150
151 resp = {
152 "port_pair_group": port_pair_group.create_dict(self.api.compute)
153 }
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')
158
159
160 class PortPairGroupDelete(SFC):
161 def delete(self, group_id):
162 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
163 try:
164 self.api.compute.delete_port_pair_group(group_id)
165
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')
170
171
172 class PortPairGroupList(SFC):
173 def get(self):
174 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
175 try:
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}
180
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')
185
186
187 class PortPairGroupShow(SFC):
188 def get(self, group_id):
189 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
190
191 try:
192 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
193 resp = {
194 "port_pair_group": port_pair_group.create_dict(self.api.compute)
195 }
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')
200
201
202 ###############################################################################
203 # Flow Classifier
204 ###############################################################################
205
206 class FlowClassifierCreate(SFC):
207 def post(self):
208 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
209
210 try:
211 request_dict = json.loads(request.data).get("flow_classifier")
212
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"]
238
239 resp = {
240 "flow_classifier": flow_classifier.create_dict(self.api.compute)
241 }
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')
246
247
248 class FlowClassifierUpdate(SFC):
249 def put(self, flow_classifier_id):
250 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
251
252 try:
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"]
259
260 resp = {
261 "flow_classifier": flow_classifier.create_dict(self.api.compute)
262 }
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')
267
268
269 class FlowClassifierDelete(SFC):
270 def delete(self, flow_classifier_id):
271 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
272 try:
273 self.api.compute.delete_flow_classifier(flow_classifier_id)
274
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')
280
281
282 class FlowClassifierList(SFC):
283 def get(self):
284 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
285 try:
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}
290
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')
295
296
297 class FlowClassifierShow(SFC):
298 def get(self, flow_classifier_id):
299 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
300
301 try:
302 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
303 resp = {
304 "flow_classifier": flow_classifier.create_dict(self.api.compute)
305 }
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')
310
311
312 ###############################################################################
313 # Port Chain
314 ###############################################################################
315
316 class PortChainCreate(SFC):
317 def post(self):
318 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
319
320 try:
321 request_dict = json.loads(request.data).get("port_chain")
322
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"]
331
332 port_chain.install(self.api.compute)
333
334 resp = {
335 "port_chain": port_chain.create_dict(self.api.compute)
336 }
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')
341
342
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")
347
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"]
361
362 port_chain.update()
363 try:
364 resp = {
365 "port_chain": port_chain.create_dict(self.api.compute)
366 }
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')
371
372
373 class PortChainDelete(SFC):
374 def delete(self, chain_id):
375 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
376
377 self.api.compute.delete_port_chain(chain_id)
378 try:
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')
383
384
385 class PortChainList(SFC):
386 def get(self):
387 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
388 try:
389 port_chain_list = []
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}
393
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')
398
399
400 class PortChainShow(SFC):
401 def get(self, chain_id):
402 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
403
404 try:
405 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
406 resp = {
407 "port_chain": port_chain.create_dict(self.api.compute)
408 }
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')