Merge remote-tracking branch 'upstream/master'
[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
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, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
59 logging.debug("REST CALL: start monitor VNF interface")
60 try:
61 if cookie:
62 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
63 else:
64 c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
65 # return monitor message response
66 return str(c), 200, CORS_HEADER
67 except Exception as ex:
68 logging.exception("API error.")
69 return ex.message, 500, CORS_HEADER
70
71 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
72 logging.debug("REST CALL: stop monitor VNF interface")
73 try:
74 if cookie:
75 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
76 else:
77 c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric)
78 # return monitor message response
79 return str(c), 200, CORS_HEADER
80 except Exception as ex:
81 logging.exception("API error.")
82 return ex.message, 500, CORS_HEADER
83
84
85 class MonitorFlowAction(Resource):
86 """
87 Monitor the counters of a specific flow
88 :param vnf_name: name of the VNF to be monitored
89 :param vnf_interface: name of the VNF interface to be monitored
90 :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets
91 :param cookie: specific identifier of flows to monitor
92 :return: message string indicating if the monitor action is succesful or not
93 """
94 global net
95
96 def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
97 logging.debug("REST CALL: start monitor VNF interface")
98 try:
99 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
100 # return monitor message response
101 return str(c), 200, CORS_HEADER
102 except Exception as ex:
103 logging.exception("API error.")
104 return ex.message, 500, CORS_HEADER
105
106 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
107 logging.debug("REST CALL: stop monitor VNF interface")
108 try:
109 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
110 # return monitor message response
111 return str(c), 200, CORS_HEADER
112 except Exception as ex:
113 logging.exception("API error.")
114 return ex.message, 500, CORS_HEADER
115
116 class MonitorLinkAction(Resource):
117 """
118 Add or remove flow monitoring on chains between VNFs.
119 These chain links are implemented as flow entries in the networks' SDN switches.
120 The monitoring is an extra flow entry on top of the existing chain, with a specific match. (preserving the chaining)
121 The counters of this new monitoring flow are exported
122 :param vnf_src_name: VNF name of the source of the link
123 :param vnf_dst_name: VNF name of the destination of the link
124 :param vnf_src_interface: VNF interface name of the source of the link
125 :param vnf_dst_interface: VNF interface name of the destination of the link
126 :param weight: weight of the link (can be useful for routing calculations)
127 :param match: OpenFlow match format of the flow entry
128 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
129 :param cookie: cookie value, identifier of the flow entry to be installed.
130 :param priority: integer indicating the priority of the flow entry
131 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
132 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
133 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
134 :param metric: tx_packet_rate, tx_byte_rate, rx_packet_rate, rx_byte_rate
135 :return: message string indicating if the chain action is succesful or not
136 """
137
138 # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
139 global net
140
141 def put(self, vnf_src_name, vnf_dst_name):
142 logging.debug("REST CALL: monitor link flow add")
143
144 try:
145 command = 'add-flow'
146 return self._MonitorLinkAction(vnf_src_name, vnf_dst_name, command=command)
147 except Exception as ex:
148 logging.exception("API error.")
149 return ex.message, 500
150
151 def delete(self, vnf_src_name, vnf_dst_name):
152 logging.debug("REST CALL: monitor link flow remove")
153
154 try:
155 command = 'del-flows'
156 return self._MonitorLinkAction(vnf_src_name, vnf_dst_name, command=command)
157 except Exception as ex:
158 logging.exception("API error.")
159 return ex.message, 500
160
161 def _MonitorLinkAction(self, vnf_src_name, vnf_dst_name, command=None):
162 # call DCNetwork method, not really datacenter specific API for now...
163 # no check if vnfs are really connected to this datacenter...
164 try:
165 # check if json data is a dict
166 data = request.json
167 if data is None:
168 data = {}
169 elif type(data) is not dict:
170 data = json.loads(request.json)
171
172 vnf_src_interface = data.get("vnf_src_interface")
173 vnf_dst_interface = data.get("vnf_dst_interface")
174 weight = data.get("weight")
175 match = data.get("match")
176 bidirectional = data.get("bidirectional")
177 cookie = data.get("cookie")
178 priority = data.get("priority")
179 skip_vlan_tag = data.get("skip_vlan_tag")
180 monitor = data.get("monitor")
181 monitor_placement = data.get("monitor_placement")
182
183 #first install monitor flow
184 c1 = net.setChain(
185 vnf_src_name, vnf_dst_name,
186 vnf_src_interface=vnf_src_interface,
187 vnf_dst_interface=vnf_dst_interface,
188 cmd=command,
189 weight=weight,
190 match=match,
191 bidirectional=bidirectional,
192 cookie=cookie,
193 priority=priority,
194 skip_vlan_tag=skip_vlan_tag,
195 monitor=monitor,
196 monitor_placement=monitor_placement)
197
198 #then export monitor flow
199 metric = data.get("metric")
200 if 'rx' in monitor_placement:
201 vnf_name = vnf_dst_name
202 vnf_interface = vnf_dst_interface
203 elif 'tx' in monitor_placement:
204 vnf_name = vnf_src_name
205 vnf_interface = vnf_src_interface
206
207 c2 = 'command unknown'
208 if command == 'add-flow':
209 c2 = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
210 elif command == 'del-flows':
211 c2 = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
212
213 # return setChain response
214 return (str(c1) + " " + str(c2)), 200
215 except Exception as ex:
216 logging.exception("API error.")
217 return ex.message, 500, CORS_HEADER
218
219 class MonitorSkewAction(Resource):
220 """
221 Monitor the counters of a VNF interface
222 :param vnf_name: name of the VNF to be monitored
223 :param resource: the resource to be monitored (cpu, mem, ...)
224 :return: message string indicating if the monitor action is succesful or not
225 """
226 global net
227
228 def put(self, vnf_name, resource_name='cpu'):
229 logging.debug("REST CALL: start monitor skewness")
230 try:
231 # configure skewmon
232 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='start')
233
234 # return monitor message response
235 return str(c), 200
236 except Exception as ex:
237 logging.exception("API error.")
238 return ex.message, 500
239
240 def delete(self, vnf_name, resource_name='cpu'):
241 logging.debug("REST CALL: stop monitor skewness")
242 try:
243 # configure skewmon
244 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
245
246 # return monitor message response
247 return str(c), 200
248 except Exception as ex:
249 logging.exception("API error.")
250 return ex.message, 500, CORS_HEADER
251