blob: 9aab1e606eda51ad56788f8ca2170866c8599b51 [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
stevenvanrossemf3712012017-05-04 00:01:52 +020039import networkx
stevenvanrossem73efd192016-06-29 01:44:07 +020040
peusterm5b428742017-06-16 10:08:11 +020041logging.basicConfig()
stevenvanrossem73efd192016-06-29 01:44:07 +020042
peustermdfc14602017-01-13 08:22:45 +010043CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
44
stevenvanrossem4fac2af2016-12-22 01:26:02 +010045# the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
stevenvanrossem73efd192016-06-29 01:44:07 +020046net = None
47
48
49class NetworkAction(Resource):
50 """
51 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
52 :param vnf_src_name: VNF name of the source of the link
53 :param vnf_dst_name: VNF name of the destination of the link
54 :param vnf_src_interface: VNF interface name of the source of the link
55 :param vnf_dst_interface: VNF interface name of the destination of the link
56 :param weight: weight of the link (can be useful for routing calculations)
57 :param match: OpenFlow match format of the flow entry
58 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
59 :param cookie: cookie value, identifier of the flow entry to be installed.
stevenvanrossem65819b82016-08-05 18:21:47 +020060 :param priority: integer indicating the priority of the flow entry
stevenvanrossembf1754e2016-11-17 10:20:52 +010061 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
62 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
63 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
stevenvanrossem73efd192016-06-29 01:44:07 +020064 :return: message string indicating if the chain action is succesful or not
65 """
66
67 global net
68
stevenvanrossem3c544ac2017-02-08 12:11:07 +010069 def put(self):
stevenvanrossem73efd192016-06-29 01:44:07 +020070 logging.debug("REST CALL: network chain add")
71 command = 'add-flow'
stevenvanrossem3c544ac2017-02-08 12:11:07 +010072 return self._NetworkAction(command=command)
stevenvanrossem73efd192016-06-29 01:44:07 +020073
stevenvanrossem3c544ac2017-02-08 12:11:07 +010074 def delete(self):
stevenvanrossem73efd192016-06-29 01:44:07 +020075 logging.debug("REST CALL: network chain remove")
76 command = 'del-flows'
stevenvanrossem3c544ac2017-02-08 12:11:07 +010077 return self._NetworkAction(command=command)
stevenvanrossem73efd192016-06-29 01:44:07 +020078
stevenvanrossem3c544ac2017-02-08 12:11:07 +010079 def _NetworkAction(self, command=None):
stevenvanrossem73efd192016-06-29 01:44:07 +020080 # call DCNetwork method, not really datacenter specific API for now...
81 # no check if vnfs are really connected to this datacenter...
82 try:
stevenvanrossem284ba2b2017-06-01 16:17:51 +020083 # check json payload
stevenvanrossem167aa3c2017-06-01 16:45:42 +020084 logging.debug("json: {}".format(request.json))
85 logging.debug("args: {}".format(request.args))
stevenvanrossem284ba2b2017-06-01 16:17:51 +020086
stevenvanrossemf693a3b2017-06-01 15:15:59 +020087 data = request.json
stevenvanrossem1027edc2016-07-14 22:02:02 +020088 if data is None:
stevenvanrossem1085e7e2017-06-01 16:37:52 +020089 data = request.args
90 if data is None:
stevenvanrossem1027edc2016-07-14 22:02:02 +020091 data = {}
stevenvanrossemff6b4042016-07-14 20:51:37 +020092
stevenvanrossem3c544ac2017-02-08 12:11:07 +010093 vnf_src_name = data.get("vnf_src_name")
94 vnf_dst_name = data.get("vnf_dst_name")
stevenvanrossemff6b4042016-07-14 20:51:37 +020095 vnf_src_interface = data.get("vnf_src_interface")
96 vnf_dst_interface = data.get("vnf_dst_interface")
97 weight = data.get("weight")
98 match = data.get("match")
99 bidirectional = data.get("bidirectional")
100 cookie = data.get("cookie")
stevenvanrossem61699eb2016-08-05 15:57:59 +0200101 priority = data.get("priority")
stevenvanrossembecc7c52016-11-07 05:52:01 +0100102 skip_vlan_tag = data.get("skip_vlan_tag")
103 monitor = data.get("monitor")
104 monitor_placement = data.get("monitor_placement")
105
stevenvanrossem73efd192016-06-29 01:44:07 +0200106 c = net.setChain(
107 vnf_src_name, vnf_dst_name,
108 vnf_src_interface=vnf_src_interface,
109 vnf_dst_interface=vnf_dst_interface,
110 cmd=command,
111 weight=weight,
112 match=match,
113 bidirectional=bidirectional,
stevenvanrossem61699eb2016-08-05 15:57:59 +0200114 cookie=cookie,
stevenvanrossembecc7c52016-11-07 05:52:01 +0100115 priority=priority,
116 skip_vlan_tag=skip_vlan_tag,
117 monitor=monitor,
118 monitor_placement=monitor_placement)
stevenvanrossem73efd192016-06-29 01:44:07 +0200119 # return setChain response
peustermdfc14602017-01-13 08:22:45 +0100120 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200121 except Exception as ex:
122 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100123 return ex.message, 500, CORS_HEADER
stevenvanrossemba51a812017-04-23 01:22:59 +0200124
125
126class DrawD3jsgraph(Resource):
127
128 global net
129
130 def get(self):
131 nodes = list()
132 nodes2 = list()
133 links = list()
134 # add all DCs
stevenvanrossemf3712012017-05-04 00:01:52 +0200135 node_attr = networkx.get_node_attributes(net.DCNetwork_graph, 'type')
stevenvanrossemba51a812017-04-23 01:22:59 +0200136 for node_name in net.DCNetwork_graph.nodes():
137 nodes2.append(node_name)
138 node_index = nodes2.index(node_name)
stevenvanrossemf3712012017-05-04 00:01:52 +0200139 type = node_attr[node_name]
140 node_dict = {"name":node_name,"group":type}
stevenvanrossemba51a812017-04-23 01:22:59 +0200141 nodes.append(node_dict)
142
143 # add links between other DCs
144 for node1_name in net.DCNetwork_graph.nodes():
145 node1_index = nodes2.index(node1_name)
146 for node2_name in net.DCNetwork_graph.neighbors(node1_name):
147 node2_index = nodes2.index(node2_name)
148 edge_dict = {"source": node1_index, "target": node2_index, "value": 10}
149 links.append(edge_dict)
150
151 json = {"nodes":nodes, "links":links}
peusterm5b428742017-06-16 10:08:11 +0200152 return json, 200, CORS_HEADER