blob: 61f2108ca29e79f12476aca4cb0ce271472a02bd [file] [log] [blame]
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +00001#! /usr/bin/env python
2import json
3import os
4
5urls="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 Kobayashi95c30532013-04-03 19:33:48 +00006RestIP=os.environ.get("ONOS_CLUSTER_BASENAME")+"1"
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +00007RestPort="8080"
8
9core_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"]
10correct_nr_switch=[6,50,25,25,25,25,25,25]
11correct_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
14nr_links= (49 + 24 * 6 + 7 + 8) * 2
15
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000016cluster_basename=os.environ.get("ONOS_CLUSTER_BASENAME")
17nr_nodes=os.environ.get("ONOS_CLUSTER_NR_NODES")
18
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000019def get_json(url):
20 print url
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000021 try:
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000022 command = "curl -s %s" % (url)
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000023 result = os.popen(command).read()
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000024 parsedResult = json.loads(result)
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000025 except:
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000026 print "REST IF %s has issue" % command
27 parsedResult = ""
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000028
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000029 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
35def check_switch():
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000036 buf = ""
37 retcode = 0
38
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000039 url="http://%s:%s/wm/core/topology/switches/all/json" % (RestIP, RestPort)
40 parsedResult = get_json(url)
41
42 if parsedResult == "":
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000043 retcode = 1
44 return (retcode, "Rest API has an issue")
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000045
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000046 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 Kobayashi76949e22013-04-03 12:00:04 +000055 cnt = []
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000056 active = []
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000057 for r in range(8):
58 cnt.append(0)
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000059 active.append(0)
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000060
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000061 for s in parsedResult:
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000062 if s['dpid'] in core_switches:
63 nw_index = 0
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000064 else:
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000065 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 Kobayashi4bc8a5e2013-04-07 06:16:35 +000071 if not s['dpid'] in registry:
72 buf += "switch: dpid %s lost controller\n" % (s['dpid'])
73
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000074 for r in range(8):
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000075 buf += "switch: network %d : %d switches %d active\n" % (r+1, cnt[r], active[r])
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000076 if correct_nr_switch[r] != cnt[r]:
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000077 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 Kobayashiecf9c472013-04-03 19:17:49 +000079
80 if correct_nr_switch[r] != active[r]:
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000081 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 Kobayashi76949e22013-04-03 12:00:04 +000085
86def check_link():
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000087 buf = ""
88 retcode = 0
89
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000090 url = "http://%s:%s/wm/core/topology/links/json" % (RestIP, RestPort)
91 parsedResult = get_json(url)
92
93 if parsedResult == "":
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000094 retcode = 1
95 return (retcode, "Rest API has an issue")
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +000096
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +000097 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +000098 intra = []
Masayoshi Kobayashi95c30532013-04-03 19:33:48 +000099 interlink=0
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +0000100 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 Kobayashi95c30532013-04-03 19:33:48 +0000118 else:
Masayoshi Kobayashi50f22a12013-04-03 19:35:42 +0000119 interlink += 1
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +0000120
121 for r in range(8):
122 if intra[r] != correct_intra_link[r]:
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000123 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 Kobayashi76949e22013-04-03 12:00:04 +0000125
Masayoshi Kobayashi95c30532013-04-03 19:33:48 +0000126 if interlink != 14:
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000127 buf += "link fail: There should be %d intra links (uni-directional) but %d\n" % (14, interlink)
128 retcode = 1
Masayoshi Kobayashi95c30532013-04-03 19:33:48 +0000129
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000130 return (retcode, buf)
131
132def check_switch_local():
133 buf = "check_switch_local\n"
134 retcode = 0
135
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +0000136 url = "http://%s:%s/wm/registry/switches/json" % (RestIP, RestPort)
137 parsedResult = get_json(url)
138
139 if parsedResult == "":
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000140 retcode = 1
141 return (retcode, "Rest API has an issue")
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +0000142
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +0000143 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 Kobayashi4bc8a5e2013-04-07 06:16:35 +0000152 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 Kobayashi76949e22013-04-03 12:00:04 +0000157
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000158 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
164def check_switch_all(nr_ctrl):
165 buf = "check_switch_all\n"
166 retcode = 0
167
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +0000168 url = "http://%s:%s/wm/registry/controllers/json" % (RestIP, RestPort)
169 parsedResult = get_json(url)
Masayoshi Kobayashi76949e22013-04-03 12:00:04 +0000170
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +0000171 if parsedResult == "":
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000172 retcode = 1
173 return (retcode, "Rest API has an issue")
Masayoshi Kobayashiecf9c472013-04-03 19:17:49 +0000174
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000175 ## 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
221def 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 Kobayashi76949e22013-04-03 12:00:04 +0000240
241if __name__ == "__main__":
Masayoshi Kobayashi4bc8a5e2013-04-07 06:16:35 +0000242 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]