Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import json |
| 3 | import os |
| 4 | |
| 5 | urls="http://localhost:8080/wm/core/topology/switches/all/json http://localhost:8080/wm/core/topology/links/json http://localhost:8080/wm/registry/controllers/json http://localhost:8080/wm/registry/switches/json" |
Masayoshi Kobayashi | 95c3053 | 2013-04-03 19:33:48 +0000 | [diff] [blame] | 6 | RestIP=os.environ.get("ONOS_CLUSTER_BASENAME")+"1" |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 7 | RestPort="8080" |
| 8 | |
| 9 | core_switches=["00:00:00:00:ba:5e:ba:11", "00:00:00:00:00:00:ba:12", "00:00:20:4e:7f:51:8a:35", "00:00:00:00:ba:5e:ba:13", "00:00:00:08:a2:08:f9:01", "00:00:00:16:97:08:9a:46"] |
| 10 | correct_nr_switch=[6,50,25,25,25,25,25,25] |
| 11 | correct_intra_link=[16, 98, 48, 48, 48, 48, 48, 48] |
| 12 | |
| 13 | #nr_links=(switch[1]+switch[2]+switch[3]+switch[4]+switch[5]+switch[6]+switch[7]+len(switch)-1+8)*2 |
| 14 | nr_links= (49 + 24 * 6 + 7 + 8) * 2 |
| 15 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 16 | cluster_basename=os.environ.get("ONOS_CLUSTER_BASENAME") |
| 17 | nr_nodes=os.environ.get("ONOS_CLUSTER_NR_NODES") |
| 18 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 19 | def get_json(url): |
| 20 | print url |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 21 | try: |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 22 | command = "curl -s %s" % (url) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 23 | result = os.popen(command).read() |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 24 | parsedResult = json.loads(result) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 25 | except: |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 26 | print "REST IF %s has issue" % command |
| 27 | parsedResult = "" |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 28 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 29 | if type(parsedResult) == 'dict' and parsedResult.has_key('code'): |
| 30 | print "REST %s returned code %s" % (command, parsedResult['code']) |
| 31 | parsedResult = "" |
| 32 | |
| 33 | return parsedResult |
| 34 | |
| 35 | def check_switch(): |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 36 | buf = "" |
| 37 | retcode = 0 |
| 38 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 39 | url="http://%s:%s/wm/core/topology/switches/all/json" % (RestIP, RestPort) |
| 40 | parsedResult = get_json(url) |
| 41 | |
| 42 | if parsedResult == "": |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 43 | retcode = 1 |
| 44 | return (retcode, "Rest API has an issue") |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 45 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 46 | url = "http://%s:%s/wm/registry/switches/json" % (RestIP, RestPort) |
| 47 | registry = get_json(url) |
| 48 | |
| 49 | if registry == "": |
| 50 | retcode = 1 |
| 51 | return (retcode, "Rest API has an issue") |
| 52 | |
| 53 | |
| 54 | buf += "switch: total %d switches\n" % len(parsedResult) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 55 | cnt = [] |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 56 | active = [] |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 57 | for r in range(8): |
| 58 | cnt.append(0) |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 59 | active.append(0) |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 60 | |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 61 | for s in parsedResult: |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 62 | if s['dpid'] in core_switches: |
| 63 | nw_index = 0 |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 64 | else: |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 65 | nw_index =int(s['dpid'].split(':')[-2], 16) - 1 |
| 66 | cnt[nw_index] += 1 |
| 67 | |
| 68 | if s['state'] == "ACTIVE": |
| 69 | active[nw_index] += 1 |
| 70 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 71 | if not s['dpid'] in registry: |
| 72 | buf += "switch: dpid %s lost controller\n" % (s['dpid']) |
| 73 | |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 74 | for r in range(8): |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 75 | buf += "switch: network %d : %d switches %d active\n" % (r+1, cnt[r], active[r]) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 76 | if correct_nr_switch[r] != cnt[r]: |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 77 | buf += "switch fail: network %d should have %d switches but has %d\n" % (r+1, correct_nr_switch[r], cnt[r]) |
| 78 | retcode = 1 |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 79 | |
| 80 | if correct_nr_switch[r] != active[r]: |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 81 | buf += "switch fail: network %d should have %d active switches but has %d\n" % (r+1, correct_nr_switch[r], active[r]) |
| 82 | retcode = 1 |
| 83 | |
| 84 | return (retcode, buf) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 85 | |
| 86 | def check_link(): |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 87 | buf = "" |
| 88 | retcode = 0 |
| 89 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 90 | url = "http://%s:%s/wm/core/topology/links/json" % (RestIP, RestPort) |
| 91 | parsedResult = get_json(url) |
| 92 | |
| 93 | if parsedResult == "": |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 94 | retcode = 1 |
| 95 | return (retcode, "Rest API has an issue") |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 96 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 97 | buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 98 | intra = [] |
Masayoshi Kobayashi | 95c3053 | 2013-04-03 19:33:48 +0000 | [diff] [blame] | 99 | interlink=0 |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 100 | for r in range(8): |
| 101 | intra.append(0) |
| 102 | |
| 103 | for s in parsedResult: |
| 104 | if s['src-switch'] in core_switches: |
| 105 | src_nw = 1 |
| 106 | else: |
| 107 | src_nw =int(s['src-switch'].split(':')[-2], 16) |
| 108 | |
| 109 | if s['dst-switch'] in core_switches: |
| 110 | dst_nw = 1 |
| 111 | else: |
| 112 | dst_nw =int(s['dst-switch'].split(':')[-2], 16) |
| 113 | |
| 114 | src_swid =int(s['src-switch'].split(':')[-1], 16) |
| 115 | dst_swid =int(s['dst-switch'].split(':')[-1], 16) |
| 116 | if src_nw == dst_nw: |
| 117 | intra[src_nw - 1] = intra[src_nw - 1] + 1 |
Masayoshi Kobayashi | 95c3053 | 2013-04-03 19:33:48 +0000 | [diff] [blame] | 118 | else: |
Masayoshi Kobayashi | 50f22a1 | 2013-04-03 19:35:42 +0000 | [diff] [blame] | 119 | interlink += 1 |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 120 | |
| 121 | for r in range(8): |
| 122 | if intra[r] != correct_intra_link[r]: |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 123 | buf += "link fail: network %d should have %d intra links but has %d\n" % (r+1, correct_intra_link[r], intra[r]) |
| 124 | retcode = 1 |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 125 | |
Masayoshi Kobayashi | 95c3053 | 2013-04-03 19:33:48 +0000 | [diff] [blame] | 126 | if interlink != 14: |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 127 | buf += "link fail: There should be %d intra links (uni-directional) but %d\n" % (14, interlink) |
| 128 | retcode = 1 |
Masayoshi Kobayashi | 95c3053 | 2013-04-03 19:33:48 +0000 | [diff] [blame] | 129 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 130 | return (retcode, buf) |
| 131 | |
| 132 | def check_switch_local(): |
| 133 | buf = "check_switch_local\n" |
| 134 | retcode = 0 |
| 135 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 136 | url = "http://%s:%s/wm/registry/switches/json" % (RestIP, RestPort) |
| 137 | parsedResult = get_json(url) |
| 138 | |
| 139 | if parsedResult == "": |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 140 | retcode = 1 |
| 141 | return (retcode, "Rest API has an issue") |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 142 | |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 143 | for s in parsedResult: |
| 144 | #print s,len(s),s[0]['controllerId'] |
| 145 | ctrl=parsedResult[s][0]['controllerId'] |
| 146 | if s in core_switches: |
| 147 | nw = 1 |
| 148 | else: |
| 149 | nw =int(s.split(':')[-2], 16) |
| 150 | |
| 151 | if len(parsedResult[s]) > 1: |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 152 | buf += "switch_local warn: switch %s has more than 1 controller: " % (s) |
| 153 | for i in parsedResult[s]: |
| 154 | buf += "%s " % (i['controllerId']) |
| 155 | buf += "\n" |
| 156 | retcode = 1 |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 157 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 158 | if int(ctrl[-1]) != nw: |
| 159 | buf += "switch_local fail: switch %s is wrongly controlled by %s\n" % (s, ctrl) |
| 160 | retcode = 1 |
| 161 | |
| 162 | return (retcode, buf) |
| 163 | |
| 164 | def check_switch_all(nr_ctrl): |
| 165 | buf = "check_switch_all\n" |
| 166 | retcode = 0 |
| 167 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 168 | url = "http://%s:%s/wm/registry/controllers/json" % (RestIP, RestPort) |
| 169 | parsedResult = get_json(url) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 170 | |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 171 | if parsedResult == "": |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 172 | retcode = 1 |
| 173 | return (retcode, "Rest API has an issue") |
Masayoshi Kobayashi | ecf9c47 | 2013-04-03 19:17:49 +0000 | [diff] [blame] | 174 | |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 175 | ## Check Dup Controller ## |
| 176 | controllers=list(set(parsedResult)) |
| 177 | if len (controllers) != len(parsedResult): |
| 178 | buf += "Duplicated Controller in registory: " + str(parsedResult) + "\n" |
| 179 | retcode = 1 |
| 180 | |
| 181 | ## Check Missing Controller ## |
| 182 | if len (controllers) != nr_ctrl: |
| 183 | buf += "Missiing Controller in registory: " + str(parsedResult) + "\n" |
| 184 | retcode = 1 |
| 185 | |
| 186 | ## Check Core Controller Exist ## |
| 187 | core_ctrl="%s1" % (cluster_basename) |
| 188 | if not core_ctrl in controllers: |
| 189 | buf += "Core controller missing in registory: " + str(parsedResult) + "\n" |
| 190 | retcode = 1 |
| 191 | |
| 192 | controllers.remove(core_ctrl) |
| 193 | |
| 194 | url = "http://%s:%s/wm/registry/switches/json" % (RestIP, RestPort) |
| 195 | parsedResult = get_json(url) |
| 196 | |
| 197 | if parsedResult == "": |
| 198 | retcode = 1 |
| 199 | return (retcode, "Rest API has an issue") |
| 200 | |
| 201 | for s in parsedResult: |
| 202 | ctrl_set = [] |
| 203 | for c in parsedResult[s]: |
| 204 | ctrl_set.append(c['controllerId']) |
| 205 | |
| 206 | if s in core_switches: |
| 207 | nw = 1 |
| 208 | else: |
| 209 | nw =int(s.split(':')[-2], 16) |
| 210 | |
| 211 | if nw == 1 and len(ctrl_set) != 1: |
| 212 | buf += "Core switch %s has more than 1 controller: %s\n" % (s, ctrl_set) |
| 213 | elif nw != 1: |
| 214 | if len(list(set(ctrl_set))) != len(ctrl_set): |
| 215 | buf += "Edge switch %s has dup controller: %s\n" % (s, ctrl_set) |
| 216 | elif len(list(set(ctrl_set))) != len(controllers): |
| 217 | buf += "Edge switch %s has missing controller: %s\n" % (s, ctrl_set) |
| 218 | |
| 219 | return (retcode, buf) |
| 220 | |
| 221 | def check_controllers(n): |
| 222 | retcode = 0 |
| 223 | buf = "" |
| 224 | url = "http://%s:%s/wm/registry/controllers/json" % (RestIP, RestPort) |
| 225 | parsedResult = get_json(url) |
| 226 | |
| 227 | if parsedResult == "": |
| 228 | retcode = 1 |
| 229 | |
| 230 | return (retcode, "Rest API has an issue") |
| 231 | |
| 232 | for i,c in enumerate(parsedResult): |
| 233 | buf += "%d : %s\n" % (i,c) |
| 234 | |
| 235 | if len(parsedResult) != n: |
| 236 | buf += "controller fail: there are %d controllers (should be %d)\n" % (len(parsedResult), n) |
| 237 | retcode = 1 |
| 238 | |
| 239 | return (retcode, buf) |
Masayoshi Kobayashi | 76949e2 | 2013-04-03 12:00:04 +0000 | [diff] [blame] | 240 | |
| 241 | if __name__ == "__main__": |
Masayoshi Kobayashi | 4bc8a5e | 2013-04-07 06:16:35 +0000 | [diff] [blame] | 242 | print "%s" % check_switch()[1] |
| 243 | print "%s" % check_link()[1] |
| 244 | print "%s" % check_switch_local()[1] |
| 245 | print "%s" % check_controllers(8)[1] |