30422c123aa4baf5855475dcfa3c232e44f7df2e
[osm/vim-emu.git] / src / emuvim / api / rest / monitor.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, reqparse
37 from flask import request
38 import json
39
40 logging.basicConfig(level=logging.INFO)
41
42 CORS_HEADER = {'Access-Control-Allow-Origin': '*'}
43
44 net = None
45
46
47
48 class 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
58 def put(self):
59 logging.debug("REST CALL: start monitor VNF interface")
60 # 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
69 try:
70 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)
74 # return monitor message response
75 return str(c), 200, CORS_HEADER
76 except Exception as ex:
77 logging.exception("API error.")
78 return ex.message, 500, CORS_HEADER
79
80 def delete(self):
81 logging.debug("REST CALL: stop monitor VNF interface")
82 # 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
91 try:
92 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)
96 # return monitor message response
97 return str(c), 200, CORS_HEADER
98 except Exception as ex:
99 logging.exception("API error.")
100 return ex.message, 500, CORS_HEADER
101
102
103 class 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
114 def put(self):
115 logging.debug("REST CALL: start monitor VNF interface")
116 # 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
125 try:
126 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
127 # return monitor message response
128 return str(c), 200, CORS_HEADER
129 except Exception as ex:
130 logging.exception("API error.")
131 return ex.message, 500, CORS_HEADER
132
133 def delete(self):
134 logging.debug("REST CALL: stop monitor VNF interface")
135 # 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
144 try:
145 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
146 # return monitor message response
147 return str(c), 200, CORS_HEADER
148 except Exception as ex:
149 logging.exception("API error.")
150 return ex.message, 500, CORS_HEADER
151
152 class MonitorLinkAction(Resource):
153 """
154 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
158 :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
177 def put(self):
178 logging.debug("REST CALL: monitor link flow add")
179
180 try:
181 command = 'add-flow'
182 return self._MonitorLinkAction(command=command)
183 except Exception as ex:
184 logging.exception("API error.")
185 return ex.message, 500, CORS_HEADER
186
187 def delete(self):
188 logging.debug("REST CALL: monitor link flow remove")
189
190 try:
191 command = 'del-flows'
192 return self._MonitorLinkAction(command=command)
193 except Exception as ex:
194 logging.exception("API error.")
195 return ex.message, 500, CORS_HEADER
196
197 def _MonitorLinkAction(self, command=None):
198 # call DCNetwork method, not really datacenter specific API for now...
199 # no check if vnfs are really connected to this datacenter...
200
201 try:
202 # 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
209 if data is None:
210 data = {}
211
212 vnf_src_name = data.get("vnf_src_name")
213 vnf_dst_name = data.get("vnf_dst_name")
214 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
249 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)
254
255 # return setChain response
256 return (str(c1) + " " + str(c2)), 200, CORS_HEADER
257 except Exception as ex:
258 logging.exception("API error.")
259 return ex.message, 500, CORS_HEADER
260
261 class 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
270 def put(self):
271 logging.debug("REST CALL: start monitor skewness")
272 # 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')
278 try:
279 # configure skewmon
280 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='start')
281
282 # return monitor message response
283 return str(c), 200, CORS_HEADER
284 except Exception as ex:
285 logging.exception("API error.")
286 return ex.message, 500, CORS_HEADER
287
288 def delete(self):
289 logging.debug("REST CALL: stop monitor skewness")
290 # 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')
296 try:
297 # configure skewmon
298 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
299
300 # return monitor message response
301 return str(c), 200, CORS_HEADER
302 except Exception as ex:
303 logging.exception("API error.")
304 return ex.message, 500, CORS_HEADER
305
306 class 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.")
329 return ex.message, 500, CORS_HEADER