fix son-emu-cli network/monitor CLI
[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 import networkx
40
41 logging.basicConfig(level=logging.INFO)
42
43 CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
44
45 # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
46 net = None
47
48
49 class 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.
60 :param priority: integer indicating the priority of the flow entry
61 :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
64 :return: message string indicating if the chain action is succesful or not
65 """
66
67 global net
68
69 def put(self):
70 logging.debug("REST CALL: network chain add")
71 command = 'add-flow'
72 return self._NetworkAction(command=command)
73
74 def delete(self):
75 logging.debug("REST CALL: network chain remove")
76 command = 'del-flows'
77 return self._NetworkAction(command=command)
78
79 def _NetworkAction(self, command=None):
80 # call DCNetwork method, not really datacenter specific API for now...
81 # no check if vnfs are really connected to this datacenter...
82 try:
83 # check json payload
84 logging.debug("json: {}".format(request.json))
85 logging.debug("args: {}".format(request.args))
86
87 # when called directly with curl via REST
88 data = request.json
89 if data is None:
90 data = {}
91 # check if json data is a dict
92 elif type(data) is not dict:
93 data = json.loads(request.json)
94
95 logging.info("data: {}".format(data))
96 vnf_src_name = data.get("vnf_src_name")
97 vnf_dst_name = data.get("vnf_dst_name")
98 vnf_src_interface = data.get("vnf_src_interface")
99 vnf_dst_interface = data.get("vnf_dst_interface")
100 weight = data.get("weight")
101 match = data.get("match")
102 bidirectional = data.get("bidirectional")
103 cookie = data.get("cookie")
104 priority = data.get("priority")
105 skip_vlan_tag = data.get("skip_vlan_tag")
106 monitor = data.get("monitor")
107 monitor_placement = data.get("monitor_placement")
108
109 c = net.setChain(
110 vnf_src_name, vnf_dst_name,
111 vnf_src_interface=vnf_src_interface,
112 vnf_dst_interface=vnf_dst_interface,
113 cmd=command,
114 weight=weight,
115 match=match,
116 bidirectional=bidirectional,
117 cookie=cookie,
118 priority=priority,
119 skip_vlan_tag=skip_vlan_tag,
120 monitor=monitor,
121 monitor_placement=monitor_placement)
122 # return setChain response
123 return str(c), 200, CORS_HEADER
124 except Exception as ex:
125 logging.exception("API error.")
126 return ex.message, 500, CORS_HEADER
127
128
129 class DrawD3jsgraph(Resource):
130
131 global net
132
133 def get(self):
134 nodes = list()
135 nodes2 = list()
136 links = list()
137 # add all DCs
138 node_attr = networkx.get_node_attributes(net.DCNetwork_graph, 'type')
139 for node_name in net.DCNetwork_graph.nodes():
140 nodes2.append(node_name)
141 node_index = nodes2.index(node_name)
142 type = node_attr[node_name]
143 node_dict = {"name":node_name,"group":type}
144 nodes.append(node_dict)
145
146 # add links between other DCs
147 for node1_name in net.DCNetwork_graph.nodes():
148 node1_index = nodes2.index(node1_name)
149 for node2_name in net.DCNetwork_graph.neighbors(node1_name):
150 node2_index = nodes2.index(node2_name)
151 edge_dict = {"source": node1_index, "target": node2_index, "value": 10}
152 links.append(edge_dict)
153
154 json = {"nodes":nodes, "links":links}
155 return json, 200, CORS_HEADER