Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | import argparse |
| 5 | import json |
| 6 | import httplib |
| 7 | import urllib2 |
| 8 | |
| 9 | class RestApi(object): |
| 10 | |
| 11 | def __init__(self, server,port): |
| 12 | self.server = server |
| 13 | self.port = port |
| 14 | |
| 15 | def get(self, path): |
| 16 | #ret = self.rest_call(path, {}, 'GET') |
| 17 | #return ret[2] |
| 18 | f = urllib2.urlopen('http://'+self.server+':'+str(self.port)+path) |
| 19 | ret = f.read() |
| 20 | return json.loads(ret) |
| 21 | |
| 22 | def set(self, path, data): |
| 23 | ret = self.rest_call(path, data, 'POST') |
| 24 | return ret[0] == 200 |
| 25 | |
| 26 | def remove(self, objtype, data): |
| 27 | #ret = self.rest_call(data, 'DELETE') |
| 28 | return ret[0] == 200 |
| 29 | |
| 30 | def rest_call(self, path, data, action): |
| 31 | headers = { |
| 32 | 'Content-type': 'application/json', |
| 33 | 'Accept': 'application/json', |
| 34 | } |
| 35 | body = json.dumps(data) |
| 36 | conn = httplib.HTTPConnection(self.server, self.port) |
| 37 | conn.request(action, path, body, headers) |
| 38 | response = conn.getresponse() |
| 39 | ret = (response.status, response.reason, response.read()) |
| 40 | conn.close() |
| 41 | print str(ret[2]) |
| 42 | return ret |
| 43 | |
| 44 | |
| 45 | |
| 46 | usage_desc = """ |
| 47 | Command descriptions: |
| 48 | |
| 49 | host [debug] |
| 50 | link [tunnellinks] |
| 51 | port <blocked | broadcast> |
| 52 | memory |
| 53 | switch |
| 54 | switchclusters |
| 55 | counter [DPID] <name> |
| 56 | switch_stats [DPID] <port | queue | flow | aggregate | desc | table | features | host> |
| 57 | """ |
| 58 | |
| 59 | def lookupPath(cmd): |
| 60 | path = '' |
| 61 | |
| 62 | numargs = len(args.otherargs) |
| 63 | |
| 64 | if args.cmd == 'switch_stats': |
| 65 | if numargs == 1: |
| 66 | path = '/wm/core/switch/all/'+args.otherargs[0]+'/json' |
| 67 | elif numargs == 2: |
| 68 | path = '/wm/core/switch/'+args.otherargs[0]+'/'+args.otherargs[1]+'/json' |
| 69 | elif args.cmd == 'switch': |
| 70 | path = '/wm/core/controller/switches/json' |
| 71 | elif args.cmd == 'counter': |
| 72 | if numargs == 1: |
| 73 | path = '/wm/core/counter/'+args.otherargs[0]+'/json' |
| 74 | elif numargs == 2: |
| 75 | path = '/wm/core/counter/'+args.otherargs[0]+'/'+args.otherargs[1]+'/json' |
| 76 | elif args.cmd == 'memory': |
| 77 | path = '/wm/core/memory/json' |
| 78 | elif args.cmd == 'link': |
| 79 | if numargs == 0: |
| 80 | path = '/wm/topology/links/json' |
| 81 | elif numargs == 1: |
| 82 | path = '/wm/topology/'+args.otherargs[0]+'/json' |
| 83 | elif args.cmd == 'port' and numargs == 1: |
| 84 | if args.otherargs[0] == "blocked": |
| 85 | path = '/wm/topology/blockedports/json' |
| 86 | elif args.otherargs[0] == "broadcast": |
| 87 | path = '/wm/topology/broadcastdomainports/json' |
| 88 | elif args.cmd == 'switchclusters': |
| 89 | path = '/wm/topology/switchclusters/json' |
| 90 | elif args.cmd == 'host': |
| 91 | path = '/wm/device/' |
| 92 | if len(args.otherargs) == 1 and args.otherargs[0] == 'debug': |
| 93 | path = '/wm/device/debug' |
| 94 | else: |
| 95 | print usage_desc |
| 96 | path = '' |
| 97 | exit(0) |
| 98 | return path |
| 99 | |
| 100 | parser = argparse.ArgumentParser(description='process args', usage=usage_desc, epilog='foo bar help') |
| 101 | parser.add_argument('--ip', default='localhost') |
| 102 | parser.add_argument('--port', default=8080) |
| 103 | parser.add_argument('cmd') |
| 104 | parser.add_argument('otherargs', nargs='*') |
| 105 | args = parser.parse_args() |
| 106 | |
| 107 | #print args |
| 108 | |
| 109 | rest = RestApi(args.ip, args.port) |
| 110 | path = lookupPath(args.cmd) |
| 111 | |
| 112 | out = rest.get(path) |
| 113 | print json.dumps(out,sort_keys=True, indent=4) |
| 114 | print "Number of items: " + str(len(out)) |
| 115 | |