blob: c1c7831b2fd6315d6857c5a0cd8e1686fb6d3705 [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
36from flask_restful import Resource
37from flask import request
38import json
39
40logging.basicConfig(level=logging.INFO)
41
42net = None
43
44
45
46class MonitorInterfaceAction(Resource):
47 """
48 Monitor the counters of a VNF interface
49 :param vnf_name: name of the VNF to be monitored
50 :param vnf_interface: name of the VNF interface to be monitored
51 :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets
52 :return: message string indicating if the monitor action is succesful or not
53 """
54 global net
55
stevenvanrossem4fac2af2016-12-22 01:26:02 +010056 def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
stevenvanrossem73efd192016-06-29 01:44:07 +020057 logging.debug("REST CALL: start monitor VNF interface")
58 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010059 if cookie:
60 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
61 else:
62 c = net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
stevenvanrossem73efd192016-06-29 01:44:07 +020063 # return monitor message response
64 return str(c), 200
65 except Exception as ex:
66 logging.exception("API error.")
67 return ex.message, 500
68
stevenvanrossem4fac2af2016-12-22 01:26:02 +010069 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
stevenvanrossem73efd192016-06-29 01:44:07 +020070 logging.debug("REST CALL: stop monitor VNF interface")
71 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010072 if cookie:
73 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
74 else:
75 c = net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric)
stevenvanrossem73efd192016-06-29 01:44:07 +020076 # return monitor message response
77 return str(c), 200
78 except Exception as ex:
79 logging.exception("API error.")
80 return ex.message, 500
81
82
83class MonitorFlowAction(Resource):
84 """
85 Monitor the counters of a specific flow
86 :param vnf_name: name of the VNF to be monitored
87 :param vnf_interface: name of the VNF interface to be monitored
88 :param metric: tx_bytes, rx_bytes, tx_packets, rx_packets
89 :param cookie: specific identifier of flows to monitor
90 :return: message string indicating if the monitor action is succesful or not
91 """
92 global net
93
stevenvanrossem9c8a4122016-07-16 03:23:13 +020094 def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
stevenvanrossem73efd192016-06-29 01:44:07 +020095 logging.debug("REST CALL: start monitor VNF interface")
96 try:
97 c = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
98 # return monitor message response
99 return str(c), 200
100 except Exception as ex:
101 logging.exception("API error.")
102 return ex.message, 500
103
stevenvanrossem9c8a4122016-07-16 03:23:13 +0200104 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
stevenvanrossem73efd192016-06-29 01:44:07 +0200105 logging.debug("REST CALL: stop monitor VNF interface")
106 try:
107 c = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
108 # return monitor message response
109 return str(c), 200
110 except Exception as ex:
111 logging.exception("API error.")
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100112 return ex.message, 500
113
114class MonitorLinkAction(Resource):
115 """
stevenvanrossem9c221db2016-12-22 10:08:22 +0100116 Add or remove flow monitoring on chains between VNFs.
117 These chain links are implemented as flow entries in the networks' SDN switches.
118 The monitoring is an extra flow entry on top of the existing chain, with a specific match. (preserving the chaining)
119 The counters of this new monitoring flow are exported
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100120 :param vnf_src_name: VNF name of the source of the link
121 :param vnf_dst_name: VNF name of the destination of the link
122 :param vnf_src_interface: VNF interface name of the source of the link
123 :param vnf_dst_interface: VNF interface name of the destination of the link
124 :param weight: weight of the link (can be useful for routing calculations)
125 :param match: OpenFlow match format of the flow entry
126 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
127 :param cookie: cookie value, identifier of the flow entry to be installed.
128 :param priority: integer indicating the priority of the flow entry
129 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
130 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
131 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
132 :param metric: tx_packet_rate, tx_byte_rate, rx_packet_rate, rx_byte_rate
133 :return: message string indicating if the chain action is succesful or not
134 """
135
136 # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
137 global net
138
139 def put(self, vnf_src_name, vnf_dst_name):
140 logging.debug("REST CALL: monitor link flow add")
141
142 try:
143 command = 'add-flow'
144 return self._MonitorLinkAction(vnf_src_name, vnf_dst_name, command=command)
145 except Exception as ex:
146 logging.exception("API error.")
147 return ex.message, 500
148
149 def delete(self, vnf_src_name, vnf_dst_name):
150 logging.debug("REST CALL: monitor link flow remove")
151
152 try:
153 command = 'del-flows'
154 return self._MonitorLinkAction(vnf_src_name, vnf_dst_name, command=command)
155 except Exception as ex:
156 logging.exception("API error.")
157 return ex.message, 500
158
159 def _MonitorLinkAction(self, vnf_src_name, vnf_dst_name, command=None):
160 # call DCNetwork method, not really datacenter specific API for now...
161 # no check if vnfs are really connected to this datacenter...
162 try:
163 # check if json data is a dict
164 data = request.json
165 if data is None:
166 data = {}
167 elif type(data) is not dict:
168 data = json.loads(request.json)
169
170 vnf_src_interface = data.get("vnf_src_interface")
171 vnf_dst_interface = data.get("vnf_dst_interface")
172 weight = data.get("weight")
173 match = data.get("match")
174 bidirectional = data.get("bidirectional")
175 cookie = data.get("cookie")
176 priority = data.get("priority")
177 skip_vlan_tag = data.get("skip_vlan_tag")
178 monitor = data.get("monitor")
179 monitor_placement = data.get("monitor_placement")
180
181 #first install monitor flow
182 c1 = net.setChain(
183 vnf_src_name, vnf_dst_name,
184 vnf_src_interface=vnf_src_interface,
185 vnf_dst_interface=vnf_dst_interface,
186 cmd=command,
187 weight=weight,
188 match=match,
189 bidirectional=bidirectional,
190 cookie=cookie,
191 priority=priority,
192 skip_vlan_tag=skip_vlan_tag,
193 monitor=monitor,
194 monitor_placement=monitor_placement)
195
196 #then export monitor flow
197 metric = data.get("metric")
198 if 'rx' in monitor_placement:
199 vnf_name = vnf_dst_name
200 vnf_interface = vnf_dst_interface
201 elif 'tx' in monitor_placement:
202 vnf_name = vnf_src_name
203 vnf_interface = vnf_src_interface
204
stevenvanrossemfcd8c9b2017-01-28 15:41:14 +0100205 c2 = 'command unknown'
206 if command == 'add-flow':
207 c2 = net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
208 elif command == 'del-flows':
209 c2 = net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100210
211 # return setChain response
212 return (str(c1) + " " + str(c2)), 200
213 except Exception as ex:
214 logging.exception("API error.")
stevenvanrossemb07fe362017-01-28 17:29:45 +0100215 return ex.message, 500
216
217class MonitorSkewAction(Resource):
218 """
219 Monitor the counters of a VNF interface
220 :param vnf_name: name of the VNF to be monitored
221 :param resource: the resource to be monitored (cpu, mem, ...)
222 :return: message string indicating if the monitor action is succesful or not
223 """
224 global net
225
226 def put(self, vnf_name, resource_name='cpu'):
227 logging.debug("REST CALL: start monitor skewness")
228 try:
229 # configure skewmon
230 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='start')
231
232 # return monitor message response
233 return str(c), 200
234 except Exception as ex:
235 logging.exception("API error.")
236 return ex.message, 500
237
238 def delete(self, vnf_name, resource_name='cpu'):
239 logging.debug("REST CALL: stop monitor skewness")
240 try:
241 # configure skewmon
242 c = net.monitor_agent.update_skewmon(vnf_name, resource_name, action='stop')
243
244 # return monitor message response
245 return str(c), 200
246 except Exception as ex:
247 logging.exception("API error.")
stevenvanrossem73efd192016-06-29 01:44:07 +0200248 return ex.message, 500