Merge pull request #192 from mpeuster/master
[osm/vim-emu.git] / src / emuvim / api / rest / network.py
1 """
2 Copyright (c) 2015 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 [, ANY ADDITIONAL AFFILIATION]
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
29 """
30 Distributed Cloud Emulator (dcemulator)
31 Networking and monitoring functions
32 (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
33 """
34
35 import logging
36 from flask_restful import Resource
37 from flask import request
38 import json
39
40 logging.basicConfig(level=logging.INFO)
41
42 CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
43
44 net = None
45
46
47 class NetworkAction(Resource):
48 """
49 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
50 :param vnf_src_name: VNF name of the source of the link
51 :param vnf_dst_name: VNF name of the destination of the link
52 :param vnf_src_interface: VNF interface name of the source of the link
53 :param vnf_dst_interface: VNF interface name of the destination of the link
54 :param weight: weight of the link (can be useful for routing calculations)
55 :param match: OpenFlow match format of the flow entry
56 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
57 :param cookie: cookie value, identifier of the flow entry to be installed.
58 :param priority: integer indicating the priority of the flow entry
59 :return: message string indicating if the chain action is succesful or not
60 """
61
62 global net
63
64 def put(self, vnf_src_name, vnf_dst_name):
65 logging.debug("REST CALL: network chain add")
66 command = 'add-flow'
67 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
68
69 def delete(self, vnf_src_name, vnf_dst_name):
70 logging.debug("REST CALL: network chain remove")
71 command = 'del-flows'
72 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
73
74 def _NetworkAction(self, vnf_src_name, vnf_dst_name, command=None):
75 # call DCNetwork method, not really datacenter specific API for now...
76 # no check if vnfs are really connected to this datacenter...
77 try:
78 # check if json data is a dict
79 data = request.json
80 if data is None:
81 data = {}
82 elif type(data) is not dict:
83 data = json.loads(request.json)
84
85 vnf_src_interface = data.get("vnf_src_interface")
86 vnf_dst_interface = data.get("vnf_dst_interface")
87 weight = data.get("weight")
88 match = data.get("match")
89 bidirectional = data.get("bidirectional")
90 cookie = data.get("cookie")
91 priority = data.get("priority")
92 c = net.setChain(
93 vnf_src_name, vnf_dst_name,
94 vnf_src_interface=vnf_src_interface,
95 vnf_dst_interface=vnf_dst_interface,
96 cmd=command,
97 weight=weight,
98 match=match,
99 bidirectional=bidirectional,
100 cookie=cookie,
101 priority=priority)
102 # return setChain response
103 return str(c), 200, CORS_HEADER
104 except Exception as ex:
105 logging.exception("API error.")
106 return ex.message, 500, CORS_HEADER