Fixed missing license headers
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_dummies / neutron_sfc_dummy_api.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
3 ALL RIGHTS RESERVED.
4
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
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
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.
16
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
20 permission.
21
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).
27 """
28 from flask_restful import Resource
29 from flask import request, Response
30 import logging
31 import json
32 import uuid
33
34 from emuvim.api.openstack.resources.port_chain import PortChain
35 from emuvim.api.openstack.helper import get_host
36
37
38 class SFC(Resource):
39 def __init__(self, api):
40 self.api = api
41
42
43 ###############################################################################
44 # Port Pair
45 ###############################################################################
46
47 class PortPairsCreate(SFC):
48 def post(self):
49 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
50
51 try:
52 request_dict = json.loads(request.data).get("port_pair")
53 name = request_dict["name"]
54
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"])
57
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"]
65
66 resp = {
67 "port_pair": port_pair.create_dict(self.api.compute)
68 }
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')
73
74
75 class PortPairsUpdate(SFC):
76 def put(self, pair_id):
77 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
78
79 try:
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"]
86
87 resp = {
88 "port_pair": port_pair.create_dict(self.api.compute)
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 PortPairsDelete(SFC):
97 def delete(self, pair_id):
98 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
99 try:
100 self.api.compute.delete_port_pair(pair_id)
101
102 return Response("Port pair %s deleted.\n" % pair_id, status=204, mimetype='application/json')
103 except Exception as ex:
104 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
105 return Response(ex.message, status=500, mimetype='application/json')
106
107
108 class PortPairsList(SFC):
109 def get(self):
110 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
111 try:
112 port_pair_list = []
113 for port_pair in self.api.compute.port_pairs.values():
114 port_pair_list.append(port_pair.create_dict(self.api.compute))
115 resp = {"port_pairs": port_pair_list}
116
117 return Response(json.dumps(resp), status=200, mimetype='application/json')
118 except Exception as ex:
119 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
120 return Response(ex.message, status=500, mimetype='application/json')
121
122
123 class PortPairsShow(SFC):
124 def get(self, pair_id):
125 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
126
127 try:
128 port_pair = self.api.compute.find_port_pair_by_name_or_id(pair_id)
129 resp = {
130 "port_pair": port_pair.create_dict(self.api.compute)
131 }
132 return Response(json.dumps(resp), status=200, 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 ###############################################################################
139 # Port Pair Group
140 ###############################################################################
141
142 class PortPairGroupCreate(SFC):
143 def post(self):
144 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
145
146 try:
147 request_dict = json.loads(request.data).get("port_pair_group")
148
149 port_pair_group = self.api.compute.create_port_pair_group(request_dict["name"])
150 port_pair_group.port_pairs = request_dict["port_pairs"]
151 if "description" in request_dict:
152 port_pair_group.description = request_dict["description"]
153 if "port_pair_group_parameters" in request_dict:
154 port_pair_group.port_pair_group_parameters = request_dict["port_pair_group_parameters"]
155
156 resp = {
157 "port_pair_group": port_pair_group.create_dict(self.api.compute)
158 }
159 return Response(json.dumps(resp), status=201, mimetype='application/json')
160 except Exception as ex:
161 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
162 return Response(ex.message, status=500, mimetype='application/json')
163
164
165 class PortPairGroupUpdate(SFC):
166 def put(self, group_id):
167 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
168
169 try:
170 request_dict = json.loads(request.data).get("port_pair_group")
171 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
172 if "name" in request_dict:
173 port_pair_group.name = request_dict["name"]
174 if "description" in request_dict:
175 port_pair_group.description = request_dict["description"]
176 if "port_pairs" in request_dict:
177 port_pair_group.port_pairs = request_dict["port_pairs"]
178
179 resp = {
180 "port_pair_group": port_pair_group.create_dict(self.api.compute)
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 PortPairGroupDelete(SFC):
189 def delete(self, group_id):
190 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
191 try:
192 self.api.compute.delete_port_pair_group(group_id)
193
194 return Response("Port pair group %s deleted.\n" % group_id, status=204, mimetype='application/json')
195 except Exception as ex:
196 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
197 return Response(ex.message, status=500, mimetype='application/json')
198
199
200 class PortPairGroupList(SFC):
201 def get(self):
202 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
203 try:
204 port_pair_group_list = []
205 for port_pair_group in self.api.compute.port_pair_groups.values():
206 port_pair_group_list.append(port_pair_group.create_dict(self.api.compute))
207 resp = {"port_pair_groups": port_pair_group_list}
208
209 return Response(json.dumps(resp), status=200, mimetype='application/json')
210 except Exception as ex:
211 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
212 return Response(ex.message, status=500, mimetype='application/json')
213
214
215 class PortPairGroupShow(SFC):
216 def get(self, group_id):
217 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
218
219 try:
220 port_pair_group = self.api.compute.find_port_pair_group_by_name_or_id(group_id)
221 resp = {
222 "port_pair_group": port_pair_group.create_dict(self.api.compute)
223 }
224 return Response(json.dumps(resp), status=200, mimetype='application/json')
225 except Exception as ex:
226 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
227 return Response(ex.message, status=500, mimetype='application/json')
228
229
230 ###############################################################################
231 # Flow Classifier
232 ###############################################################################
233
234 class FlowClassifierCreate(SFC):
235 def post(self):
236 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
237
238 try:
239 request_dict = json.loads(request.data).get("flow_classifier")
240
241 flow_classifier = self.api.compute.create_flow_classifier(request_dict["name"])
242 if "description" in request_dict:
243 flow_classifier.description = request_dict["description"]
244 if "ethertype" in request_dict:
245 flow_classifier.ethertype = request_dict["ethertype"]
246 if "protocol" in request_dict:
247 flow_classifier.protocol = request_dict["protocol"]
248 if "source_port_range_min" in request_dict:
249 flow_classifier.source_port_range_min = request_dict["source_port_range_min"]
250 if "source_port_range_max" in request_dict:
251 flow_classifier.source_port_range_max = request_dict["source_port_range_max"]
252 if "destination_port_range_min" in request_dict:
253 flow_classifier.destination_port_range_min = request_dict["destination_port_range_min"]
254 if "destination_port_range_max" in request_dict:
255 flow_classifier.destination_port_range_max = request_dict["destination_port_range_max"]
256 if "source_ip_prefix" in request_dict:
257 flow_classifier.source_ip_prefix = request_dict["source_ip_prefix"]
258 if "destination_ip_prefix" in request_dict:
259 flow_classifier.destination_ip_prefix = request_dict["destination_ip_prefix"]
260 if "logical_source_port" in request_dict:
261 flow_classifier.logical_source_port = request_dict["logical_source_port"]
262 if "logical_destination_port" in request_dict:
263 flow_classifier.logical_destination_port = request_dict["logical_destination_port"]
264 if "l7_parameters" in request_dict:
265 flow_classifier.l7_parameters = request_dict["l7_parameters"]
266
267 resp = {
268 "flow_classifier": flow_classifier.create_dict(self.api.compute)
269 }
270 return Response(json.dumps(resp), status=201, mimetype='application/json')
271 except Exception as ex:
272 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
273 return Response(ex.message, status=500, mimetype='application/json')
274
275
276 class FlowClassifierUpdate(SFC):
277 def put(self, flow_classifier_id):
278 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
279
280 try:
281 request_dict = json.loads(request.data).get("flow_classifier")
282 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
283 if "name" in request_dict:
284 flow_classifier.name = request_dict["name"]
285 if "description" in request_dict:
286 flow_classifier.description = request_dict["description"]
287
288 resp = {
289 "flow_classifier": flow_classifier.create_dict(self.api.compute)
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 FlowClassifierDelete(SFC):
298 def delete(self, flow_classifier_id):
299 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
300 try:
301 self.api.compute.delete_flow_classifier(flow_classifier_id)
302
303 return Response("Port pair group %s deleted.\n" % flow_classifier_id, status=204,
304 mimetype='application/json')
305 except Exception as ex:
306 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
307 return Response(ex.message, status=500, mimetype='application/json')
308
309
310 class FlowClassifierList(SFC):
311 def get(self):
312 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
313 try:
314 flow_classifier_list = []
315 for flow_classifier in self.api.compute.flow_classifiers.values():
316 flow_classifier_list.append(flow_classifier.create_dict(self.api.compute))
317 resp = {"flow_classifiers": flow_classifier_list}
318
319 return Response(json.dumps(resp), status=200, mimetype='application/json')
320 except Exception as ex:
321 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
322 return Response(ex.message, status=500, mimetype='application/json')
323
324
325 class FlowClassifierShow(SFC):
326 def get(self, flow_classifier_id):
327 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
328
329 try:
330 flow_classifier = self.api.compute.find_flow_classifier_by_name_or_id(flow_classifier_id)
331 resp = {
332 "flow_classifier": flow_classifier.create_dict(self.api.compute)
333 }
334 return Response(json.dumps(resp), status=200, mimetype='application/json')
335 except Exception as ex:
336 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
337 return Response(ex.message, status=500, mimetype='application/json')
338
339
340 ###############################################################################
341 # Port Chain
342 ###############################################################################
343
344 class PortChainCreate(SFC):
345 def post(self):
346 logging.debug("API CALL: %s POST" % str(self.__class__.__name__))
347
348 try:
349 request_dict = json.loads(request.data).get("port_chain")
350
351 port_chain = self.api.compute.create_port_chain(request_dict["name"])
352 port_chain.port_pair_groups = request_dict["port_pair_groups"]
353 if "description" in request_dict:
354 port_chain.description = request_dict["description"]
355 if "flow_classifiers" in request_dict:
356 port_chain.flow_classifiers = request_dict["flow_classifiers"]
357 if "chain_parameters" in request_dict:
358 port_chain.chain_parameters = request_dict["chain_parameters"]
359
360 port_chain.install(self.api.compute)
361
362 resp = {
363 "port_chain": port_chain.create_dict(self.api.compute)
364 }
365 return Response(json.dumps(resp), status=201, mimetype='application/json')
366 except Exception as ex:
367 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
368 return Response(ex.message, status=500, mimetype='application/json')
369
370
371 class PortChainUpdate(SFC):
372 def put(self, chain_id):
373 logging.debug("API CALL: %s PUT" % str(self.__class__.__name__))
374 request_dict = json.loads(request.data).get("port_chain")
375
376 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
377 if "name" in request_dict:
378 port_chain.name = request_dict["name"]
379 if "description" in request_dict:
380 port_chain.description = request_dict["description"]
381 if "flow_classfiers" in request_dict:
382 # TODO: update chain implementation
383 port_chain.description = request_dict["flow_classifiers"]
384 if "no_flow_classfiers" in request_dict:
385 port_chain.description = []
386 if "port_pair_groups" in request_dict:
387 # TODO: update chain implementation
388 port_chain.port_pair_groups = request_dict["port_pair_groups"]
389
390 port_chain.update()
391 try:
392 resp = {
393 "port_chain": port_chain.create_dict(self.api.compute)
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 PortChainDelete(SFC):
402 def delete(self, chain_id):
403 logging.debug("API CALL: %s DELETE" % str(self.__class__.__name__))
404
405 self.api.compute.delete_port_chain(chain_id)
406 try:
407 return Response("Port chain %s deleted.\n" % chain_id, status=204, mimetype='application/json')
408 except Exception as ex:
409 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
410 return Response(ex.message, status=500, mimetype='application/json')
411
412
413 class PortChainList(SFC):
414 def get(self):
415 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
416 try:
417 port_chain_list = []
418 for port_chain in self.api.compute.port_chains.values():
419 port_chain_list.append(port_chain.create_dict(self.api.compute))
420 resp = {"port_chains": port_chain_list}
421
422 return Response(json.dumps(resp), status=200, mimetype='application/json')
423 except Exception as ex:
424 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
425 return Response(ex.message, status=500, mimetype='application/json')
426
427
428 class PortChainShow(SFC):
429 def get(self, chain_id):
430 logging.debug("API CALL: %s GET" % str(self.__class__.__name__))
431
432 try:
433 port_chain = self.api.compute.find_port_chain_by_name_or_id(chain_id)
434 resp = {
435 "port_chain": port_chain.create_dict(self.api.compute)
436 }
437 return Response(json.dumps(resp), status=200, mimetype='application/json')
438 except Exception as ex:
439 logging.exception("Neutron SFC: %s Exception." % str(self.__class__.__name__))
440 return Response(ex.message, status=500, mimetype='application/json')