blob: 74f7acf279a3bf31bed681b4cc751c49ac34fb59 [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
40logging.basicConfig(level=logging.INFO)
41
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:
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100202 # get URL parameters
203 data = request.args
204 #then no data
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100205 if data is None:
206 data = {}
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100207
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100208
209 vnf_src_name = data.get("vnf_src_name")
210 vnf_dst_name = data.get("vnf_dst_name")
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100211 vnf_src_interface = data.get("vnf_src_interface")
212 vnf_dst_interface = data.get("vnf_dst_interface")
213 weight = data.get("weight")
214 match = data.get("match")
215 bidirectional = data.get("bidirectional")
216 cookie = data.get("cookie")
217 priority = data.get("priority")
218 skip_vlan_tag = data.get("skip_vlan_tag")
219 monitor = data.get("monitor")
220 monitor_placement = data.get("monitor_placement")
221
222 #first install monitor flow
223 c1 = net.setChain(
224 vnf_src_name, vnf_dst_name,
225 vnf_src_interface=vnf_src_interface,
226 vnf_dst_interface=vnf_dst_interface,
227 cmd=command,
228 weight=weight,
229 match=match,
230 bidirectional=bidirectional,
231 cookie=cookie,
232 priority=priority,
233 skip_vlan_tag=skip_vlan_tag,
234 monitor=monitor,
235 monitor_placement=monitor_placement)
236
237 #then export monitor flow
238 metric = data.get("metric")
239 if 'rx' in monitor_placement:
240 vnf_name = vnf_dst_name
241 vnf_interface = vnf_dst_interface
242 elif 'tx' in monitor_placement:
243 vnf_name = vnf_src_name
244 vnf_interface = vnf_src_interface
245
stevenvanrossemfcd8c9b2017-01-28 15:41:14 +0100246 c2 = 'command unknown'
247 if command == 'add-flow':
248 c2 = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
249 elif command == 'del-flows':
250 c2 = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100251
252 # return setChain response
peusterm8f2063d2017-02-02 17:49:22 +0100253 return (str(c1) + " " + str(c2)), 200, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100254 except Exception as ex:
255 logging.exception("API error.")
stevenvanrossema6ce6f32017-02-03 16:11:42 +0100256 return ex.message, 500, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100257
258class MonitorSkewAction(Resource):
259 """
260 Monitor the counters of a VNF interface
261 :param vnf_name: name of the VNF to be monitored
262 :param resource: the resource to be monitored (cpu, mem, ...)
263 :return: message string indicating if the monitor action is succesful or not
264 """
265 global net
266
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100267 def put(self):
stevenvanrossemb07fe362017-01-28 17:29:45 +0100268 logging.debug("REST CALL: start monitor skewness")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100269 # get URL parameters
270 data = request.args
271 if data is None:
272 data = {}
273 vnf_name = data.get("vnf_name")
274 resource_name = data.get("resource_name", 'cpu')
stevenvanrossemb07fe362017-01-28 17:29:45 +0100275 try:
276 # configure skewmon
277 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='start')
278
279 # return monitor message response
280 return str(c), 200
281 except Exception as ex:
282 logging.exception("API error.")
283 return ex.message, 500
284
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100285 def delete(self):
stevenvanrossemb07fe362017-01-28 17:29:45 +0100286 logging.debug("REST CALL: stop monitor skewness")
stevenvanrossem3c544ac2017-02-08 12:11:07 +0100287 # get URL parameters
288 data = request.args
289 if data is None:
290 data = {}
291 vnf_name = data.get("vnf_name")
292 resource_name = data.get("resource_name", 'cpu')
stevenvanrossemb07fe362017-01-28 17:29:45 +0100293 try:
294 # configure skewmon
295 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
296
297 # return monitor message response
peusterm8f2063d2017-02-02 17:49:22 +0100298 return str(c), 200, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100299 except Exception as ex:
300 logging.exception("API error.")
stevenvanrossema6ce6f32017-02-03 16:11:42 +0100301 return ex.message, 500, CORS_HEADER
302