let OpenStack APIs work inside a container
[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 from emuvim.api.openstack.helper import get_host
9
10
11 class SFC(Resource):
12 def __init__(self, api):
13 self.api = api
14
15
16 ###############################################################################
17 # Port Pair
18 ###############################################################################
19
20 class PortPairsCreate(SFC):
21 def post(self):
22 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
23
24 try:
25 request_dict = json.loads(request.data).get("port_pair")
26 name = request_dict["name"]
27
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"])
30
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"]
38
39 resp = {
40 "port_pair": port_pair.create_dict(self.api.compute)
41 }
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')
46
47
48 class PortPairsUpdate(SFC):
49 def put(self, pair_id):
50 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
51
52 try:
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"]
59
60 resp = {
61 "port_pair": port_pair.create_dict(self.api.compute)
62 }
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')
67
68
69 class PortPairsDelete(SFC):
70 def delete(self, pair_id):
71 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
72 try:
73 self.api.compute.delete_port_pair(pair_id)
74
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')
79
80
81 class PortPairsList(SFC):
82 def get(self):
83 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
84 try:
85 port_pair_list = []
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}
89
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')
94
95
96 class PortPairsShow(SFC):
97 def get(self, pair_id):
98 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
99
100 try:
101 port_pair = self.api.compute.find_port_pair_by_name_or_id(pair_id)
102 resp = {
103 "port_pair": port_pair.create_dict(self.api.compute)
104 }
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')
109
110
111 ###############################################################################
112 # Port Pair Group
113 ###############################################################################
114
115 class PortPairGroupCreate(SFC):
116 def post(self):
117 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
118
119 try:
120 request_dict = json.loads(request.data).get("port_pair_group")
121
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"]
128
129 resp = {
130 "port_pair_group": port_pair_group.create_dict(self.api.compute)
131 }
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')
136
137
138 class PortPairGroupUpdate(SFC):
139 def put(self, group_id):
140 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
141
142 try:
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"]
151
152 resp = {
153 "port_pair_group": port_pair_group.create_dict(self.api.compute)
154 }
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')
159
160
161 class PortPairGroupDelete(SFC):
162 def delete(self, group_id):
163 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
164 try:
165 self.api.compute.delete_port_pair_group(group_id)
166
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')
171
172
173 class PortPairGroupList(SFC):
174 def get(self):
175 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
176 try:
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}
181
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')
186
187
188 class PortPairGroupShow(SFC):
189 def get(self, group_id):
190 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
191
192 try:
193 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
194 resp = {
195 "port_pair_group": port_pair_group.create_dict(self.api.compute)
196 }
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')
201
202
203 ###############################################################################
204 # Flow Classifier
205 ###############################################################################
206
207 class FlowClassifierCreate(SFC):
208 def post(self):
209 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
210
211 try:
212 request_dict = json.loads(request.data).get("flow_classifier")
213
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"]
239
240 resp = {
241 "flow_classifier": flow_classifier.create_dict(self.api.compute)
242 }
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')
247
248
249 class FlowClassifierUpdate(SFC):
250 def put(self, flow_classifier_id):
251 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
252
253 try:
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"]
260
261 resp = {
262 "flow_classifier": flow_classifier.create_dict(self.api.compute)
263 }
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')
268
269
270 class FlowClassifierDelete(SFC):
271 def delete(self, flow_classifier_id):
272 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
273 try:
274 self.api.compute.delete_flow_classifier(flow_classifier_id)
275
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')
281
282
283 class FlowClassifierList(SFC):
284 def get(self):
285 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
286 try:
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}
291
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')
296
297
298 class FlowClassifierShow(SFC):
299 def get(self, flow_classifier_id):
300 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
301
302 try:
303 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
304 resp = {
305 "flow_classifier": flow_classifier.create_dict(self.api.compute)
306 }
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')
311
312
313 ###############################################################################
314 # Port Chain
315 ###############################################################################
316
317 class PortChainCreate(SFC):
318 def post(self):
319 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
320
321 try:
322 request_dict = json.loads(request.data).get("port_chain")
323
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"]
332
333 port_chain.install(self.api.compute)
334
335 resp = {
336 "port_chain": port_chain.create_dict(self.api.compute)
337 }
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')
342
343
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")
348
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"]
362
363 port_chain.update()
364 try:
365 resp = {
366 "port_chain": port_chain.create_dict(self.api.compute)
367 }
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')
372
373
374 class PortChainDelete(SFC):
375 def delete(self, chain_id):
376 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
377
378 self.api.compute.delete_port_chain(chain_id)
379 try:
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')
384
385
386 class PortChainList(SFC):
387 def get(self):
388 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
389 try:
390 port_chain_list = []
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}
394
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')
399
400
401 class PortChainShow(SFC):
402 def get(self, chain_id):
403 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
404
405 try:
406 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
407 resp = {
408 "port_chain": port_chain.create_dict(self.api.compute)
409 }
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')