blob: 6f99bdfbc321a221dec2270149af68d03fcc6483 [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
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
stevenvanrossem4fac2af2016-12-22 01:26:02 +010058 def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
stevenvanrossem73efd192016-06-29 01:44:07 +020059 logging.debug("REST CALL: start monitor VNF interface")
60 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010061 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)
stevenvanrossem73efd192016-06-29 01:44:07 +020065 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +010066 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020067 except Exception as ex:
68 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010069 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020070
stevenvanrossem4fac2af2016-12-22 01:26:02 +010071 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=None):
stevenvanrossem73efd192016-06-29 01:44:07 +020072 logging.debug("REST CALL: stop monitor VNF interface")
73 try:
stevenvanrossem4fac2af2016-12-22 01:26:02 +010074 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)
stevenvanrossem73efd192016-06-29 01:44:07 +020078 # return monitor message response
peustermdfc14602017-01-13 08:22:45 +010079 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020080 except Exception as ex:
81 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +010082 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +020083
84
85class 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
stevenvanrossem9c8a4122016-07-16 03:23:13 +020096 def put(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
stevenvanrossem73efd192016-06-29 01:44:07 +020097 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
peustermdfc14602017-01-13 08:22:45 +0100101 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200102 except Exception as ex:
103 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100104 return ex.message, 500, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200105
stevenvanrossem9c8a4122016-07-16 03:23:13 +0200106 def delete(self, vnf_name, vnf_interface=None, metric='tx_packets', cookie=0):
stevenvanrossem73efd192016-06-29 01:44:07 +0200107 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
peustermdfc14602017-01-13 08:22:45 +0100111 return str(c), 200, CORS_HEADER
stevenvanrossem73efd192016-06-29 01:44:07 +0200112 except Exception as ex:
113 logging.exception("API error.")
peustermdfc14602017-01-13 08:22:45 +0100114 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100115
116class MonitorLinkAction(Resource):
117 """
stevenvanrossem9c221db2016-12-22 10:08:22 +0100118 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
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100122 :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.")
peusterm8f2063d2017-02-02 17:49:22 +0100149 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100150
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.")
peusterm8f2063d2017-02-02 17:49:22 +0100159 return ex.message, 500, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100160
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
stevenvanrossemfcd8c9b2017-01-28 15:41:14 +0100207 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)
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100212
213 # return setChain response
peusterm8f2063d2017-02-02 17:49:22 +0100214 return (str(c1) + " " + str(c2)), 200, CORS_HEADER
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100215 except Exception as ex:
216 logging.exception("API error.")
stevenvanrossemb07fe362017-01-28 17:29:45 +0100217 return ex.message, 500
218
219class 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
peusterm8f2063d2017-02-02 17:49:22 +0100247 return str(c), 200, CORS_HEADER
stevenvanrossemb07fe362017-01-28 17:29:45 +0100248 except Exception as ex:
249 logging.exception("API error.")
peusterm8f2063d2017-02-02 17:49:22 +0100250 return ex.message, 500, CORS_HEADER
251