Merge pull request #131 from stevenvanrossem/master
[osm/vim-emu.git] / src / emuvim / api / rest / network.py
1 import logging
2 from flask_restful import Resource
3 from flask import request
4 import json
5
6 logging.basicConfig(level=logging.INFO)
7
8 net = None
9
10
11 class NetworkAction(Resource):
12 """
13 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
14 :param vnf_src_name: VNF name of the source of the link
15 :param vnf_dst_name: VNF name of the destination of the link
16 :param vnf_src_interface: VNF interface name of the source of the link
17 :param vnf_dst_interface: VNF interface name of the destination of the link
18 :param weight: weight of the link (can be useful for routing calculations)
19 :param match: OpenFlow match format of the flow entry
20 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
21 :param cookie: cookie value, identifier of the flow entry to be installed.
22 :return: message string indicating if the chain action is succesful or not
23 """
24
25 global net
26
27 def put(self, vnf_src_name, vnf_dst_name):
28 logging.debug("REST CALL: network chain add")
29 command = 'add-flow'
30 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
31
32 def delete(self, vnf_src_name, vnf_dst_name):
33 logging.debug("REST CALL: network chain remove")
34 command = 'del-flows'
35 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
36
37 def _NetworkAction(self, vnf_src_name, vnf_dst_name, command=None):
38 # call DCNetwork method, not really datacenter specific API for now...
39 # no check if vnfs are really connected to this datacenter...
40 try:
41 vnf_src_interface = json.loads(request.json).get("vnf_src_interface")
42 vnf_dst_interface = json.loads(request.json).get("vnf_dst_interface")
43 weight = json.loads(request.json).get("weight")
44 match = json.loads(request.json).get("match")
45 bidirectional = json.loads(request.json).get("bidirectional")
46 cookie = json.loads(request.json).get("cookie")
47 c = net.setChain(
48 vnf_src_name, vnf_dst_name,
49 vnf_src_interface=vnf_src_interface,
50 vnf_dst_interface=vnf_dst_interface,
51 cmd=command,
52 weight=weight,
53 match=match,
54 bidirectional=bidirectional,
55 cookie=cookie)
56 # return setChain response
57 return str(c), 200
58 except Exception as ex:
59 logging.exception("API error.")
60 return ex.message, 500