blob: b534516e2c43f1956e7144ebac3059c16d00333c [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:
27 result=os.popen(cmd).read()
28 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):
42 json_flow = json.load(open(filename, 'r'))
43 nr_flow_entries = 0
44 fdb_nmap={}
45 for flow in json_flow:
46 fid = flow['flowId']['value']
47 dl_src = flow['flowEntryMatch']['srcMac']['value'].lower()
48 dl_dst = flow['flowEntryMatch']['dstMac']['value'].lower()
49 e = {}
50 for entry in flow['dataPath']['flowEntries']:
51 dpid = entry['dpid']['value'].replace(":","").lower()
52 cookie = entry['flowEntryId']
53 in_port = entry['flowEntryMatch']['inPort']['value']
54
55 outport = []
56 for p in entry['flowEntryActions']:
57 outport.append(p['actionOutput']['port']['value'])
58 outport.sort()
59
60 e['dpid']=dpid
61 e['cookie']=cookie
62 e['in_port']=in_port
63 e['dl_src']=dl_src
64 e['dl_dst']=dl_dst
65 e['actions']=outport
66 e['fid']=fid
67 key = make_key(dpid, in_port, dl_src, dl_dst, outport[0])
68
69 fdb_nmap[key]=e
70 nr_flow_entries += 1
71
72 print "nmap contained %d flow entries" % nr_flow_entries
73 return fdb_nmap
74
75def fdb_raw(filename):
76 f = open(filename, 'r')
77 fdb_raw={}
78 nr_flow_entries = 0
79 for line in f:
80 e = {}
81 if line[0] == '#':
82 continue
83 dpid=re.search("dpid=([0-9]|[a-f])*", line.strip()).group().split("=")[1]
84 cookie=re.search("cookie=0x([0-9]|[a-f])*", line.strip()).group().split("=")[1]
85 in_port=re.search("in_port=[0-9]*", line.strip()).group().split("=")[1]
86 dl_src=re.search("dl_src=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1]
87 dl_dst=re.search("dl_dst=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1]
88 outport_list=re.search("actions=(output:[0-9]*,*)*", line.strip()).group().split("=")[1].split(",")
89 outport=[]
90 for i in outport_list:
91 outport.append(int(i.split(":")[1]))
92 outport.sort()
93
94 e['dpid']=dpid
95 e['cookie']=cookie
96 e['in_port']=in_port
97 e['dl_src']=dl_src
98 e['dl_dst']=dl_dst
99 e['actions']=outport
100 key = make_key(dpid, in_port, dl_src, dl_dst, outport[0])
101 fdb_raw[key]=e
102 nr_flow_entries += 1
103
104 print "real switches contained %d flow entries" % nr_flow_entries
105 f.close()
106 return fdb_raw
107
108if __name__ == "__main__":
109 argvs = sys.argv
110 if len(argvs) != 2:
111 f1=".nmap.%d.txt" % pid
112 f2=".rawflow.%d.txt" % pid
113 dump_network_map(f1)
114 dump_switch_table(f2)
115
116 else:
117 f1 = sys.argv[1]
118 f2 = sys.argv[2]
119
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000120
121 fdb_nmap = fdb_nmap(f1)
122 fdb_raw = fdb_raw(f2)
123
124 nr_not_found_in_switch = 0
125 for f in fdb_nmap:
126 if not fdb_raw.has_key(f):
127 nr_not_found_in_switch += 1
128 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'])
129
130 nr_not_found_in_nmap = 0
131 for f in fdb_raw:
132 if not fdb_nmap.has_key(f):
133 nr_not_found_in_nmap += 1
134 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'])
135
136 print "Network Map has %d flow entries, %d not found in switch" % (len(fdb_nmap), nr_not_found_in_switch)
137 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 +0000138 print "dumpfiles: %s %s" % (f1, f2)