blob: 8dc65f6c6e50c541d2d6e2515f271b2b47b99994 [file] [log] [blame]
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +00001#! /usr/bin/env python
2import os
3import re
4import json
5import sys
6import os
7
8status=0
9
10pid=os.getpid()
11basename=os.getenv("ONOS_CLUSTER_BASENAME")
12RestPort=8080
13
14def dump_switch_table(filename):
15 cmd="dsh \"cd ONOS/scripts; ./showflow.sh\""
16 f=open(filename, 'w')
17 result=os.popen(cmd).read()
Masayoshi Kobayashi67771162013-04-08 06:03:51 +000018
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +000019 f.write(result)
20 f.close()
21
22def dump_network_map(filename):
23 url="http://%s1:%d/wm/flow/getall/json" % (basename, RestPort)
Masayoshi Kobayashi67771162013-04-08 06:03:51 +000024 cmd="curl -s %s" % url
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +000025 f=open(filename, 'w')
Masayoshi Kobayashi67771162013-04-08 06:03:51 +000026 try:
Masayoshi Kobayashib96d05c2013-04-08 07:43:53 +000027 result=json.loads(os.popen(cmd).read())
Masayoshi Kobayashi67771162013-04-08 06:03:51 +000028 except:
29 print "REST has issue"
30 sys.exit(1)
31
32 json.dump(result, f, indent=2, sort_keys=True)
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +000033 f.close()
34
35def make_key(*kargs):
36 key=""
37 for k in kargs:
38 key += str(k)+"_"
39 return key[:-1]
40
41def fdb_nmap(filename):
Masayoshi Kobayashib96d05c2013-04-08 07:43:53 +000042 f=open(filename, 'r')
43 json_flow = json.load(f)
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +000044 nr_flow_entries = 0
45 fdb_nmap={}
Masayoshi Kobayashib96d05c2013-04-08 07:43:53 +000046 ## XXX should be better way to ditect empty list ##
47 if json_flow == "[]":
48 print "nmap contained %d flow entries" % nr_flow_entries
49 return fdb_nmap
50
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +000051 for flow in json_flow:
52 fid = flow['flowId']['value']
53 dl_src = flow['flowEntryMatch']['srcMac']['value'].lower()
54 dl_dst = flow['flowEntryMatch']['dstMac']['value'].lower()
55 e = {}
56 for entry in flow['dataPath']['flowEntries']:
57 dpid = entry['dpid']['value'].replace(":","").lower()
58 cookie = entry['flowEntryId']
59 in_port = entry['flowEntryMatch']['inPort']['value']
60
61 outport = []
62 for p in entry['flowEntryActions']:
63 outport.append(p['actionOutput']['port']['value'])
64 outport.sort()
65
66 e['dpid']=dpid
67 e['cookie']=cookie
68 e['in_port']=in_port
69 e['dl_src']=dl_src
70 e['dl_dst']=dl_dst
71 e['actions']=outport
72 e['fid']=fid
73 key = make_key(dpid, in_port, dl_src, dl_dst, outport[0])
74
75 fdb_nmap[key]=e
76 nr_flow_entries += 1
77
78 print "nmap contained %d flow entries" % nr_flow_entries
79 return fdb_nmap
80
81def fdb_raw(filename):
82 f = open(filename, 'r')
83 fdb_raw={}
84 nr_flow_entries = 0
85 for line in f:
86 e = {}
87 if line[0] == '#':
88 continue
89 dpid=re.search("dpid=([0-9]|[a-f])*", line.strip()).group().split("=")[1]
90 cookie=re.search("cookie=0x([0-9]|[a-f])*", line.strip()).group().split("=")[1]
91 in_port=re.search("in_port=[0-9]*", line.strip()).group().split("=")[1]
92 dl_src=re.search("dl_src=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1]
93 dl_dst=re.search("dl_dst=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1]
94 outport_list=re.search("actions=(output:[0-9]*,*)*", line.strip()).group().split("=")[1].split(",")
95 outport=[]
96 for i in outport_list:
97 outport.append(int(i.split(":")[1]))
98 outport.sort()
99
100 e['dpid']=dpid
101 e['cookie']=cookie
102 e['in_port']=in_port
103 e['dl_src']=dl_src
104 e['dl_dst']=dl_dst
105 e['actions']=outport
106 key = make_key(dpid, in_port, dl_src, dl_dst, outport[0])
107 fdb_raw[key]=e
108 nr_flow_entries += 1
109
110 print "real switches contained %d flow entries" % nr_flow_entries
111 f.close()
112 return fdb_raw
113
114if __name__ == "__main__":
115 argvs = sys.argv
116 if len(argvs) != 2:
117 f1=".nmap.%d.txt" % pid
118 f2=".rawflow.%d.txt" % pid
119 dump_network_map(f1)
120 dump_switch_table(f2)
121
122 else:
123 f1 = sys.argv[1]
124 f2 = sys.argv[2]
125
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000126
127 fdb_nmap = fdb_nmap(f1)
128 fdb_raw = fdb_raw(f2)
129
130 nr_not_found_in_switch = 0
131 for f in fdb_nmap:
132 if not fdb_raw.has_key(f):
133 nr_not_found_in_switch += 1
134 print "fid=%s dpid=%s cookie=%s in_port=%s dl_src=%s dl_dst=%s outport=%s not found in switch" % (fdb_nmap[f]['fid'],fdb_nmap[f]['dpid'],fdb_nmap[f]['cookie'],fdb_nmap[f]['in_port'],fdb_nmap[f]['dl_src'],fdb_nmap[f]['dl_dst'],fdb_nmap[f]['actions'])
135
136 nr_not_found_in_nmap = 0
137 for f in fdb_raw:
138 if not fdb_nmap.has_key(f):
139 nr_not_found_in_nmap += 1
140 print "dpid=%s cookie=%s in_port=%s dl_src=%s dl_dst=%s outport=%s not found in nmap" % (fdb_raw[f]['dpid'],fdb_raw[f]['cookie'],fdb_raw[f]['in_port'],fdb_raw[f]['dl_src'],fdb_raw[f]['dl_dst'],fdb_raw[f]['actions'])
141
142 print "Network Map has %d flow entries, %d not found in switch" % (len(fdb_nmap), nr_not_found_in_switch)
143 print "Switches have %d flow entries, %d not found in network map" % (len(fdb_raw), nr_not_found_in_nmap)
Masayoshi Kobayashi67771162013-04-08 06:03:51 +0000144 print "dumpfiles: %s %s" % (f1, f2)