start Docker VNF xterm via the rest api (can be called from the dashboard)
[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 # get URL parameters
203 data = request.args
204 #then no data
205 if data is None:
206 data = {}
207
208
209 vnf_src_name = data.get("vnf_src_name")
210 vnf_dst_name = data.get("vnf_dst_name")
211 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
246 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)
251
252 # return setChain response
253 return (str(c1) + " " + str(c2)), 200, CORS_HEADER
254 except Exception as ex:
255 logging.exception("API error.")
256 return ex.message, 500, CORS_HEADER
257
258 class 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
267 def put(self):
268 logging.debug("REST CALL: start monitor skewness")
269 # 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')
275 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, CORS_HEADER
281 except Exception as ex:
282 logging.exception("API error.")
283 return ex.message, 500, CORS_HEADER
284
285 def delete(self):
286 logging.debug("REST CALL: stop monitor skewness")
287 # 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')
293 try:
294 # configure skewmon
295 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
296
297 # return monitor message response
298 return str(c), 200, CORS_HEADER
299 except Exception as ex:
300 logging.exception("API error.")
301 return ex.message, 500, CORS_HEADER
302
303 class MonitorTerminal(Resource):
304 """
305 start a terminal for the selected VNFs
306 :param vnf_list: list of names of the VNFs to start a terminal from (all VNFs if None)
307 :return: message string indicating if the monitor action is succesful or not
308 """
309 global net
310
311 def get(self):
312 # get URL parameters
313 data = request.args
314 if data is None:
315 data = {}
316 vnf_list = data.get("vnf_list")
317 logging.debug("REST CALL: start terminal for: {}".format(vnf_list))
318 try:
319 # start terminals
320 c = net.monitor_agent.term(vnf_list)
321
322 # return monitor message response
323 return str(c), 200, CORS_HEADER
324 except Exception as ex:
325 logging.exception("API error.")
326 return ex.message, 500, CORS_HEADER