blob: bf0f26bd42d0e8372f20583009171acffd95cafb [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
stevenvanrossem3c544ac2017-02-08 12:11:07 +010036from flask_restful import Resource, reqparse
stevenvanrossem73efd192016-06-29 01:44:07 +020037from flask import request
38import json
39
peusterm5b428742017-06-16 10:08:11 +020040logging.basicConfig()
stevenvanrossem73efd192016-06-29 01:44:07 +020041
peustermdfc14602017-01-13 08:22:45 +010042CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
43
stevenvanrossem73efd192016-06-29 01:44:07 +020044net = None
45
46
47
48class MonitorInterfaceAction(Resource):
49 """
50 Monitor the counters of a VNF interface
51 :param vnf_name: name of the VNF to be monitored
52 :param vnf_interface: name of the VNF interface to be monitored
53 :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets
54 :return: message string indicating if the monitor action is succesful or not
55 """
56 global net
57
stevenvanrossem3c544ac2017-02-08 12:11:07 +010058 def put(self):
stevenvanrossem73efd192016-06-29 01:44:07 +020059 logging.debug("REST CALL: start monitor VNF interface")
stevenvanrossem3c544ac2017-02-08 12:11:07 +010060 # get URL parameters
61 data = request.args
62 if data is None:
63 data = {}
64 vnf_name = data.get("vnf_name")
65 vnf_interface = data.get("vnf_interface", None)
66 metric = data.get("metric", 'tx_packets')
67 cookie = data.get("cookie")
68
stevenvanrossem73efd192016-06-29 01:44:07 +020069 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010070 if cookie:
71 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
72 else:
73 c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
stevenvanrossem73efd192016-06-29 01:44:07 +020074 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +010075 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020076 except Exception as ex:
77 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010078 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020079
stevenvanrossem3c544ac2017-02-08 12:11:07 +010080 def delete(self):
stevenvanrossem73efd192016-06-29 01:44:07 +020081 logging.debug("REST CALL: stop monitor VNF interface")
stevenvanrossem3c544ac2017-02-08 12:11:07 +010082 # get URL parameters
83 data = request.args
84 if data is None:
85 data = {}
86 vnf_name = data.get("vnf_name")
87 vnf_interface = data.get("vnf_interface", None)
88 metric = data.get("metric", 'tx_packets')
89 cookie = data.get("cookie")
90
stevenvanrossem73efd192016-06-29 01:44:07 +020091 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010092 if cookie:
93 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
94 else:
95 c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric)
stevenvanrossem73efd192016-06-29 01:44:07 +020096 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +010097 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020098 except Exception as ex:
99 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100100 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200101
102
103class MonitorFlowAction(Resource):
104 """
105 Monitor the counters of a specific flow
106 :param vnf_name: name of the VNF to be monitored
107 :param vnf_interface: name of the VNF interface to be monitored
108 :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets
109 :param cookie: specific identifier of flows to monitor
110 :return: message string indicating if the monitor action is succesful or not
111 """
112 global net
113
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100114 def put(self):
stevenvanrossem73efd192016-06-29 01:44:07 +0200115 logging.debug("REST CALL: start monitor VNF interface")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100116 # get URL parameters
117 data = request.args
118 if data is None:
119 data = {}
120 vnf_name = data.get("vnf_name")
121 vnf_interface = data.get("vnf_interface", None)
122 metric = data.get("metric", 'tx_packets')
123 cookie = data.get("cookie", 0)
124
stevenvanrossem73efd192016-06-29 01:44:07 +0200125 try:
126 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
127 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +0100128 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200129 except Exception as ex:
130 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100131 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200132
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100133 def delete(self):
stevenvanrossem73efd192016-06-29 01:44:07 +0200134 logging.debug("REST CALL: stop monitor VNF interface")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100135 # get URL parameters
136 data = request.args
137 if data is None:
138 data = {}
139 vnf_name = data.get("vnf_name")
140 vnf_interface = data.get("vnf_interface", None)
141 metric = data.get("metric", 'tx_packets')
142 cookie = data.get("cookie", 0)
143
stevenvanrossem73efd192016-06-29 01:44:07 +0200144 try:
145 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
146 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +0100147 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200148 except Exception as ex:
149 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100150 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100151
152class MonitorLinkAction(Resource):
153 """
stevenvanrossem9c221db2016-12-22 10:08:22 +0100154 Add or remove flow monitoring on chains between VNFs.
155 These chain links are implemented as flow entries in the networks' SDN switches.
156 The monitoring is an extra flow entry on top of the existing chain, with a specific match. (preserving the chaining)
157 The counters of this new monitoring flow are exported
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100158 :param vnf_src_name: VNF name of the source of the link
159 :param vnf_dst_name: VNF name of the destination of the link
160 :param vnf_src_interface: VNF interface name of the source of the link
161 :param vnf_dst_interface: VNF interface name of the destination of the link
162 :param weight: weight of the link (can be useful for routing calculations)
163 :param match: OpenFlow match format of the flow entry
164 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
165 :param cookie: cookie value, identifier of the flow entry to be installed.
166 :param priority: integer indicating the priority of the flow entry
167 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
168 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
169 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
170 :param metric: tx_packet_rate, tx_byte_rate, rx_packet_rate, rx_byte_rate
171 :return: message string indicating if the chain action is succesful or not
172 """
173
174 # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
175 global net
176
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100177 def put(self):
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100178 logging.debug("REST CALL: monitor link flow add")
179
180 try:
181 command = 'add-flow'
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100182 return self._MonitorLinkAction(command=command)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100183 except Exception as ex:
184 logging.exception("API error.")
peusterm8f2063d2017-02-02 17:49:22 +0100185 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100186
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100187 def delete(self):
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100188 logging.debug("REST CALL: monitor link flow remove")
189
190 try:
191 command = 'del-flows'
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100192 return self._MonitorLinkAction(command=command)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100193 except Exception as ex:
194 logging.exception("API error.")
peusterm8f2063d2017-02-02 17:49:22 +0100195 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100196
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100197 def _MonitorLinkAction(self, command=None):
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100198 # call DCNetwork method, not really datacenter specific API for now...
199 # no check if vnfs are really connected to this datacenter...
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100200
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100201 try:
stevenvanrossem167aa3c2017-06-01 16:45:42 +0200202 # check json payload
203 logging.debug("json: {}".format(request.json))
204 logging.debug("args: {}".format(request.args))
205
206 data = request.json
207 if data is None:
208 data = request.args
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100209 if data is None:
210 data = {}
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100211
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100212 vnf_src_name = data.get("vnf_src_name")
213 vnf_dst_name = data.get("vnf_dst_name")
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100214 vnf_src_interface = data.get("vnf_src_interface")
215 vnf_dst_interface = data.get("vnf_dst_interface")
216 weight = data.get("weight")
217 match = data.get("match")
218 bidirectional = data.get("bidirectional")
219 cookie = data.get("cookie")
220 priority = data.get("priority")
221 skip_vlan_tag = data.get("skip_vlan_tag")
222 monitor = data.get("monitor")
223 monitor_placement = data.get("monitor_placement")
224
225 #first install monitor flow
226 c1 = net.setChain(
227 vnf_src_name, vnf_dst_name,
228 vnf_src_interface=vnf_src_interface,
229 vnf_dst_interface=vnf_dst_interface,
230 cmd=command,
231 weight=weight,
232 match=match,
233 bidirectional=bidirectional,
234 cookie=cookie,
235 priority=priority,
236 skip_vlan_tag=skip_vlan_tag,
237 monitor=monitor,
238 monitor_placement=monitor_placement)
239
240 #then export monitor flow
241 metric = data.get("metric")
242 if 'rx' in monitor_placement:
243 vnf_name = vnf_dst_name
244 vnf_interface = vnf_dst_interface
245 elif 'tx' in monitor_placement:
246 vnf_name = vnf_src_name
247 vnf_interface = vnf_src_interface
248
stevenvanrossemfcd8c9b2017-01-28 15:41:14 +0100249 c2 = 'command unknown'
250 if command == 'add-flow':
251 c2 = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
252 elif command == 'del-flows':
253 c2 = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100254
255 # return setChain response
peusterm8f2063d2017-02-02 17:49:22 +0100256 return (str(c1) + " " + str(c2)), 200, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100257 except Exception as ex:
258 logging.exception("API error.")
stevenvanrossema6ce6f32017-02-03 16:11:42 +0100259 return ex.message, 500, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100260
261class MonitorSkewAction(Resource):
262 """
263 Monitor the counters of a VNF interface
264 :param vnf_name: name of the VNF to be monitored
265 :param resource: the resource to be monitored (cpu, mem, ...)
266 :return: message string indicating if the monitor action is succesful or not
267 """
268 global net
269
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100270 def put(self):
stevenvanrossemb07fe362017-01-28 17:29:45 +0100271 logging.debug("REST CALL: start monitor skewness")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100272 # get URL parameters
273 data = request.args
274 if data is None:
275 data = {}
276 vnf_name = data.get("vnf_name")
277 resource_name = data.get("resource_name", 'cpu')
stevenvanrossemb07fe362017-01-28 17:29:45 +0100278 try:
279 # configure skewmon
280 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='start')
281
282 # return monitor message response
stevenvanrossemc63c5492017-05-08 16:10:13 +0200283 return str(c), 200, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100284 except Exception as ex:
285 logging.exception("API error.")
stevenvanrossemc63c5492017-05-08 16:10:13 +0200286 return ex.message, 500, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100287
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100288 def delete(self):
stevenvanrossemb07fe362017-01-28 17:29:45 +0100289 logging.debug("REST CALL: stop monitor skewness")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100290 # get URL parameters
291 data = request.args
292 if data is None:
293 data = {}
294 vnf_name = data.get("vnf_name")
295 resource_name = data.get("resource_name", 'cpu')
stevenvanrossemb07fe362017-01-28 17:29:45 +0100296 try:
297 # configure skewmon
298 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
299
300 # return monitor message response
peusterm8f2063d2017-02-02 17:49:22 +0100301 return str(c), 200, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100302 except Exception as ex:
303 logging.exception("API error.")
stevenvanrossema6ce6f32017-02-03 16:11:42 +0100304 return ex.message, 500, CORS_HEADER
305
stevenvanrossemc63c5492017-05-08 16:10:13 +0200306class MonitorTerminal(Resource):
307 """
308 start a terminal for the selected VNFs
309 :param vnf_list: list of names of the VNFs to start a terminal from (all VNFs if None)
310 :return: message string indicating if the monitor action is succesful or not
311 """
312 global net
313
314 def get(self):
315 # get URL parameters
316 data = request.args
317 if data is None:
318 data = {}
319 vnf_list = data.get("vnf_list")
320 logging.debug("REST CALL: start terminal for: {}".format(vnf_list))
321 try:
322 # start terminals
323 c = net.monitor_agent.term(vnf_list)
324
325 # return monitor message response
326 return str(c), 200, CORS_HEADER
327 except Exception as ex:
328 logging.exception("API error.")
peusterm5b428742017-06-16 10:08:11 +0200329 return ex.message, 500, CORS_HEADER