blob: 8e870bce6cbe9728b3526aae95e48f98639c75f8 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#! /usr/bin/env python
2import json
3import os
4import sys
5
Jon Hall8060af02014-04-14 14:17:58 -07006# http://localhost:8080/wm/onos/ng/switches/json
7# http://localhost:8080/wm/onos/ng/links/json
8# http://localhost:8080/wm/onos/registry/controllers/json
9# http://localhost:8080/wm/onos/registry/switches/json"
adminbae64d82013-08-01 10:50:15 -070010
11def get_json(url):
12 try:
13 command = "curl -s %s" % (url)
14 result = os.popen(command).read()
15 parsedResult = json.loads(result)
16 except:
17 print "REST IF %s has issue" % command
18 parsedResult = ""
19
20 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
21 print "REST %s returned code %s" % (command, parsedResult['code'])
22 parsedResult = ""
23
24 return parsedResult
25
26def check_switch(RestIP,correct_nr_switch ):
27 buf = ""
28 retcode = 0
29 RestPort="8080"
30
Jon Hall8060af02014-04-14 14:17:58 -070031 url="http://%s:%s/wm/onos/ng/switches/json" % (RestIP, RestPort)
adminbae64d82013-08-01 10:50:15 -070032 parsedResult = get_json(url)
33
34 if parsedResult == "":
35 retcode = 1
36 return (retcode, "Rest API has an issue")
37
Jon Hall8060af02014-04-14 14:17:58 -070038 url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
adminbae64d82013-08-01 10:50:15 -070039 registry = get_json(url)
40
41 if registry == "":
42 retcode = 1
43 return (retcode, "Rest API has an issue")
44
45
46 buf += "switch: total %d switches\n" % len(parsedResult)
47 cnt = 0
48 active = 0
49
50 for s in parsedResult:
51 cnt += 1
52
53 if s['state'] == "ACTIVE":
54 active += 1
55
56 if not s['dpid'] in registry:
57 buf += "switch: dpid %s lost controller\n" % (s['dpid'])
58
59 buf += "switch: network %d : %d switches %d active\n" % (0+1, cnt, active)
60 if correct_nr_switch != cnt:
61 buf += "switch fail: network %d should have %d switches but has %d\n" % (1, correct_nr_switch, cnt)
62 retcode = 1
63
64 if correct_nr_switch != active:
65 buf += "switch fail: network %d should have %d active switches but has %d\n" % (1, correct_nr_switch, active)
66 retcode = 1
67
68 return (retcode, buf)
69
70def check_link(RestIP, nr_links):
71 RestPort = "8080"
72 buf = ""
73 retcode = 0
74
Jon Hall8060af02014-04-14 14:17:58 -070075 url = "http://%s:%s/wm/onos/ng/links/json" % (RestIP, RestPort)
adminbae64d82013-08-01 10:50:15 -070076 parsedResult = get_json(url)
77
78 if parsedResult == "":
79 retcode = 1
80 return (retcode, "Rest API has an issue")
81
82 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
83 intra = 0
84 interlink=0
85
86 for s in parsedResult:
87 intra = intra + 1
88
89 if intra != nr_links:
90 buf += "link fail: network %d should have %d intra links but has %d\n" % (1, nr_links, intra)
91 retcode = 1
92
93 return (retcode, buf)
94
95#if __name__ == "__main__":
96def check_status(ip, numoswitch, numolink):
97
98 switch = check_switch(ip, numoswitch)
99 link = check_link(ip, numolink)
100 value = switch[0]
101 value += link[0]
102 if value != 0:
103 print "FAIL"
104 return 0
105 else:
106 print "PASS"
107 return 1
108 print "%s" % switch[1]
109 print "%s" % link[1]
110 # print "%s" % check_switch_local()[1]
111 # print "%s" % check_controllers(8)[1]