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: |
Masayoshi Kobayashi | b96d05c | 2013-04-08 07:43:53 +0000 | [diff] [blame] | 27 | result=json.loads(os.popen(cmd).read()) |
Masayoshi Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 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): |
Masayoshi Kobayashi | b96d05c | 2013-04-08 07:43:53 +0000 | [diff] [blame] | 42 | f=open(filename, 'r') |
| 43 | json_flow = json.load(f) |
Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 44 | nr_flow_entries = 0 |
| 45 | fdb_nmap={} |
Masayoshi Kobayashi | b96d05c | 2013-04-08 07:43:53 +0000 | [diff] [blame] | 46 | ## 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 Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 51 | 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 | |
| 81 | def 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 | |
| 114 | if __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 Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 126 | |
| 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 Kobayashi | 6777116 | 2013-04-08 06:03:51 +0000 | [diff] [blame] | 144 | print "dumpfiles: %s %s" % (f1, f2) |