Moving emuvim into the src directory
[osm/vim-emu.git] / src / emuvim / dcemulator / monitoring.py
1 __author__ = 'Administrator'
2
3 import urllib2
4 import logging
5 from mininet.node import OVSSwitch
6 import ast
7 logging.basicConfig(level=logging.INFO)
8
9 """
10 class to read openflow stats from the Ryu controller of the DCNEtwork
11 """
12
13 class DCNetworkMonitor():
14 def __init__(self, net):
15 self.net = net
16 # link to REST_API
17 self.ip = '0.0.0.0'
18 self.port = '8080'
19 self.REST_api = 'http://{0}:{1}'.format(self.ip,self.port)
20
21
22 def get_rate(self, vnf_name, direction='tx'):
23 try:
24 vnf_switch = self.net.DCNetwork_graph.neighbors(str(vnf_name))
25
26 if len(vnf_switch) > 1:
27 logging.info("vnf: {0} has multiple ports".format(vnf_name))
28 return
29 elif len(vnf_switch) == 0:
30 logging.info("vnf: {0} is not connected".format(vnf_name))
31 return
32 else:
33 vnf_switch = vnf_switch[0]
34 next_node = self.net.getNodeByName(vnf_switch)
35
36 if not isinstance( next_node, OVSSwitch ):
37 logging.info("vnf: {0} is not connected to switch".format(vnf_name))
38 return
39
40 mon_port = self.net.DCNetwork_graph[vnf_name][vnf_switch]['dst_port']
41 switch_dpid = x = int(str(next_node.dpid),16)
42
43 ret = self.REST_cmd('stats/port', switch_dpid)
44 port_stat_dict = ast.literal_eval(ret)
45 for port_stat in port_stat_dict[str(switch_dpid)]:
46 if port_stat['port_no'] == mon_port:
47 return port_stat
48 break
49
50 return ret
51
52 except Exception as ex:
53 logging.exception("get_txrate error.")
54 return ex.message
55
56
57
58 def REST_cmd(self, prefix, dpid):
59 url = self.REST_api + '/' + str(prefix) + '/' + str(dpid)
60 req = urllib2.Request(url)
61 ret = urllib2.urlopen(req).read()
62 return ret