admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import json |
| 3 | import os |
| 4 | import sys |
| 5 | |
Jon Hall | 2f42a67 | 2014-05-28 10:13:18 -0700 | [diff] [blame^] | 6 | # http://localhost:8080/wm/onos/topology/switches |
| 7 | # http://localhost:8080/wm/onos/topology/links |
Jon Hall | 8060af0 | 2014-04-14 14:17:58 -0700 | [diff] [blame] | 8 | # http://localhost:8080/wm/onos/registry/controllers/json |
| 9 | # http://localhost:8080/wm/onos/registry/switches/json" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 10 | |
| 11 | def 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 | |
| 26 | def check_switch(RestIP,correct_nr_switch ): |
| 27 | buf = "" |
| 28 | retcode = 0 |
| 29 | RestPort="8080" |
| 30 | |
Jon Hall | 2f42a67 | 2014-05-28 10:13:18 -0700 | [diff] [blame^] | 31 | url="http://%s:%s/wm/onos/topology/switches" % (RestIP, RestPort) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | parsedResult = get_json(url) |
| 33 | |
| 34 | if parsedResult == "": |
| 35 | retcode = 1 |
| 36 | return (retcode, "Rest API has an issue") |
| 37 | |
Jon Hall | 8060af0 | 2014-04-14 14:17:58 -0700 | [diff] [blame] | 38 | url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | 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 | |
| 70 | def check_link(RestIP, nr_links): |
| 71 | RestPort = "8080" |
| 72 | buf = "" |
| 73 | retcode = 0 |
| 74 | |
Jon Hall | 2f42a67 | 2014-05-28 10:13:18 -0700 | [diff] [blame^] | 75 | url = "http://%s:%s/wm/onos/topology/links" % (RestIP, RestPort) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | 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__": |
| 96 | def 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] |