Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import os |
| 3 | import re |
| 4 | import json |
| 5 | import sys |
| 6 | import os |
| 7 | |
| 8 | status=0 |
| 9 | |
| 10 | pid=os.getpid() |
| 11 | basename=os.getenv("ONOS_CLUSTER_BASENAME") |
| 12 | RestPort=8080 |
| 13 | |
| 14 | def dump_switch_table(filename): |
| 15 | cmd="dsh \"cd ONOS/scripts; ./showflow.sh\"" |
| 16 | f=open(filename, 'w') |
| 17 | result=os.popen(cmd).read() |
Masayoshi Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 18 | |
Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 19 | f.write(result) |
| 20 | f.close() |
| 21 | |
| 22 | def dump_network_map(filename): |
| 23 | url="http://%s1:%d/wm/flow/getall/json" % (basename, RestPort) |
Masayoshi Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 24 | cmd="curl -s %s" % url |
Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 25 | f=open(filename, 'w') |
Masayoshi Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 26 | 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 Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 33 | f.close() |
| 34 | |
| 35 | def make_key(*kargs): |
| 36 | key="" |
| 37 | for k in kargs: |
| 38 | key += str(k)+"_" |
| 39 | return key[:-1] |
| 40 | |
| 41 | def 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 | |
| 75 | def 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 | |
| 108 | if __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 Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 120 | |
| 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 Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 138 | print "dumpfiles: %s %s" % (f1, f2) |