2 Copyright (c) 2015 SONATA-NFV and Paderborn University
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
30 Distributed Cloud Emulator (dcemulator)
31 Networking and monitoring functions
32 (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
36 from flask_restful
import Resource
37 from flask
import request
40 logging
.basicConfig(level
=logging
.INFO
)
46 class MonitorInterfaceAction(Resource
):
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
56 def put(self
, vnf_name
, vnf_interface
=None, metric
='tx_packets', cookie
=None):
57 logging
.debug("REST CALL: start monitor VNF interface")
60 c
= net
.monitor_agent
.setup_flow(vnf_name
, vnf_interface
, metric
, cookie
)
62 c
= net
.monitor_agent
.setup_metric(vnf_name
, vnf_interface
, metric
)
63 # return monitor message response
65 except Exception as ex
:
66 logging
.exception("API error.")
67 return ex
.message
, 500
69 def delete(self
, vnf_name
, vnf_interface
=None, metric
='tx_packets', cookie
=None):
70 logging
.debug("REST CALL: stop monitor VNF interface")
73 c
= net
.monitor_agent
.stop_flow(vnf_name
, vnf_interface
, metric
, cookie
)
75 c
= net
.monitor_agent
.stop_metric(vnf_name
, vnf_interface
, metric
)
76 # return monitor message response
78 except Exception as ex
:
79 logging
.exception("API error.")
80 return ex
.message
, 500
83 class MonitorFlowAction(Resource
):
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
94 def put(self
, vnf_name
, vnf_interface
=None, metric
='tx_packets', cookie
=0):
95 logging
.debug("REST CALL: start monitor VNF interface")
97 c
= net
.monitor_agent
.setup_flow(vnf_name
, vnf_interface
, metric
, cookie
)
98 # return monitor message response
100 except Exception as ex
:
101 logging
.exception("API error.")
102 return ex
.message
, 500
104 def delete(self
, vnf_name
, vnf_interface
=None, metric
='tx_packets', cookie
=0):
105 logging
.debug("REST CALL: stop monitor VNF interface")
107 c
= net
.monitor_agent
.stop_flow(vnf_name
, vnf_interface
, metric
, cookie
)
108 # return monitor message response
110 except Exception as ex
:
111 logging
.exception("API error.")
112 return ex
.message
, 500
114 class MonitorLinkAction(Resource
):
116 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
117 :param vnf_src_name: VNF name of the source of the link
118 :param vnf_dst_name: VNF name of the destination of the link
119 :param vnf_src_interface: VNF interface name of the source of the link
120 :param vnf_dst_interface: VNF interface name of the destination of the link
121 :param weight: weight of the link (can be useful for routing calculations)
122 :param match: OpenFlow match format of the flow entry
123 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
124 :param cookie: cookie value, identifier of the flow entry to be installed.
125 :param priority: integer indicating the priority of the flow entry
126 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
127 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
128 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
129 :param metric: tx_packet_rate, tx_byte_rate, rx_packet_rate, rx_byte_rate
130 :return: message string indicating if the chain action is succesful or not
133 # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py
136 def put(self
, vnf_src_name
, vnf_dst_name
):
137 logging
.debug("REST CALL: monitor link flow add")
141 return self
._MonitorLinkAction
(vnf_src_name
, vnf_dst_name
, command
=command
)
142 except Exception as ex
:
143 logging
.exception("API error.")
144 return ex
.message
, 500
146 def delete(self
, vnf_src_name
, vnf_dst_name
):
147 logging
.debug("REST CALL: monitor link flow remove")
150 command
= 'del-flows'
151 return self
._MonitorLinkAction
(vnf_src_name
, vnf_dst_name
, command
=command
)
152 except Exception as ex
:
153 logging
.exception("API error.")
154 return ex
.message
, 500
156 def _MonitorLinkAction(self
, vnf_src_name
, vnf_dst_name
, command
=None):
157 # call DCNetwork method, not really datacenter specific API for now...
158 # no check if vnfs are really connected to this datacenter...
160 # check if json data is a dict
164 elif type(data
) is not dict:
165 data
= json
.loads(request
.json
)
167 vnf_src_interface
= data
.get("vnf_src_interface")
168 vnf_dst_interface
= data
.get("vnf_dst_interface")
169 weight
= data
.get("weight")
170 match
= data
.get("match")
171 bidirectional
= data
.get("bidirectional")
172 cookie
= data
.get("cookie")
173 priority
= data
.get("priority")
174 skip_vlan_tag
= data
.get("skip_vlan_tag")
175 monitor
= data
.get("monitor")
176 monitor_placement
= data
.get("monitor_placement")
178 #first install monitor flow
180 vnf_src_name
, vnf_dst_name
,
181 vnf_src_interface
=vnf_src_interface
,
182 vnf_dst_interface
=vnf_dst_interface
,
186 bidirectional
=bidirectional
,
189 skip_vlan_tag
=skip_vlan_tag
,
191 monitor_placement
=monitor_placement
)
193 #then export monitor flow
194 metric
= data
.get("metric")
195 if 'rx' in monitor_placement
:
196 vnf_name
= vnf_dst_name
197 vnf_interface
= vnf_dst_interface
198 elif 'tx' in monitor_placement
:
199 vnf_name
= vnf_src_name
200 vnf_interface
= vnf_src_interface
202 c2
= net
.monitor_agent
.setup_flow(vnf_name
, vnf_interface
, metric
, cookie
)
204 # return setChain response
205 return (str(c1
) + " " + str(c2
)), 200
206 except Exception as ex
:
207 logging
.exception("API error.")
208 return ex
.message
, 500