blob: 83fbde767ede016b6003c587adf5226c44529e33 [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
stevenvanrosseme131bf52016-07-14 11:42:09 +020028
29"""
30Distributed Cloud Emulator (dcemulator)
31Networking and monitoring functions
32(c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
33"""
34
stevenvanrossem73efd192016-06-29 01:44:07 +020035import logging
36from flask_restful import Resource
37from flask import request
38import json
39
40logging.basicConfig(level=logging.INFO)
41
42net = None
43
44
45class NetworkAction(Resource):
46 """
47 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
48 :param vnf_src_name: VNF name of the source of the link
49 :param vnf_dst_name: VNF name of the destination of the link
50 :param vnf_src_interface: VNF interface name of the source of the link
51 :param vnf_dst_interface: VNF interface name of the destination of the link
52 :param weight: weight of the link (can be useful for routing calculations)
53 :param match: OpenFlow match format of the flow entry
54 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
55 :param cookie: cookie value, identifier of the flow entry to be installed.
stevenvanrossem65819b82016-08-05 18:21:47 +020056 :param priority: integer indicating the priority of the flow entry
stevenvanrossem73efd192016-06-29 01:44:07 +020057 :return: message string indicating if the chain action is succesful or not
58 """
59
60 global net
61
62 def put(self, vnf_src_name, vnf_dst_name):
63 logging.debug("REST CALL: network chain add")
64 command = 'add-flow'
65 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
66
67 def delete(self, vnf_src_name, vnf_dst_name):
68 logging.debug("REST CALL: network chain remove")
69 command = 'del-flows'
70 return self._NetworkAction(vnf_src_name, vnf_dst_name, command=command)
71
72 def _NetworkAction(self, vnf_src_name, vnf_dst_name, command=None):
73 # call DCNetwork method, not really datacenter specific API for now...
74 # no check if vnfs are really connected to this datacenter...
75 try:
stevenvanrossemff6b4042016-07-14 20:51:37 +020076 # check if json data is a dict
77 data = request.json
stevenvanrossem1027edc2016-07-14 22:02:02 +020078 if data is None:
79 data = {}
80 elif type(data) is not dict:
stevenvanrossemff6b4042016-07-14 20:51:37 +020081 data = json.loads(request.json)
82
83 vnf_src_interface = data.get("vnf_src_interface")
84 vnf_dst_interface = data.get("vnf_dst_interface")
85 weight = data.get("weight")
86 match = data.get("match")
87 bidirectional = data.get("bidirectional")
88 cookie = data.get("cookie")
stevenvanrossem61699eb2016-08-05 15:57:59 +020089 priority = data.get("priority")
stevenvanrossem73efd192016-06-29 01:44:07 +020090 c = net.setChain(
91 vnf_src_name, vnf_dst_name,
92 vnf_src_interface=vnf_src_interface,
93 vnf_dst_interface=vnf_dst_interface,
94 cmd=command,
95 weight=weight,
96 match=match,
97 bidirectional=bidirectional,
stevenvanrossem61699eb2016-08-05 15:57:59 +020098 cookie=cookie,
99 priority=priority)
stevenvanrossem73efd192016-06-29 01:44:07 +0200100 # return setChain response
101 return str(c), 200
102 except Exception as ex:
103 logging.exception("API error.")
104 return ex.message, 500