1 __author__ = "Jan Medved"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "jmedved@cisco.com"
6 from operator import itemgetter, attrgetter
14 INVENTORY_URL = 'restconf/%s/opendaylight-inventory:nodes'
15 hdr = {'Accept': 'application/json'}
22 def crawl_flows(flows):
25 found_flows += len(flows)
26 if in_args.plevel > 1:
27 print ' Flows found: %d' % len(flows)
28 if in_args.plevel > 2:
30 s = json.dumps(f, sort_keys=True, indent=4, separators=(',', ': '))
31 # s = s.replace('{\n', '')
32 # s = s.replace('}', '')
36 s = s.replace('\n', '\n ')
38 print " Flow %s:" % f['flow-node-inventory:id']
43 def crawl_table(table):
47 stats = table['opendaylight-flow-table-statistics:flow-table-statistics']
48 active_flows = stats['opendaylight-flow-table-statistics:active-flows']
51 reported_flows += active_flows
52 if in_args.plevel > 1:
53 print ' Table %s:' % table['flow-node-inventory:id']
54 s = json.dumps(stats, sort_keys=True, indent=12, separators=(',', ': '))
55 s = s.replace('{\n', '')
56 s = s.replace('}', '')
59 print " Stats for Table '%s' not available." % \
60 table['flow-node-inventory:id']
63 flows_in_table = table['flow-node-inventory:flow']
64 crawl_flows(flows_in_table)
73 if in_args.plevel > 1:
74 print "\nNode '%s':" %(node['id'])
75 elif in_args.plevel > 0:
76 print "%s" %(node['id'])
79 tables = node['flow-node-inventory:table']
80 if in_args.plevel > 1:
81 print ' Tables: %d' % len(tables)
86 print ' Data for tables not available.'
88 # print json.dumps(tables, sort_keys=True, indent=4, separators=(',', ': '))
90 def crawl_inventory(url):
91 s = requests.Session()
92 r = s.get(url, headers=hdr, stream=False)
94 if (r.status_code == 200):
96 inv = json.loads(r.content)['nodes']['node']
98 for n in range(len(inv)):
99 if re.search('openflow', inv[n]['id']) != None:
102 # sinv = sorted(sinv, key=lambda k: int(k['id'].split(':')[-1]))
104 sinv = sorted(sinv, key=lambda k: int(re.findall('\d+', k['id'])[0]))
105 for n in range(len(sinv)):
108 print 'Fuck! %s' % sinv[n]['id']
111 print 'Could not retrieve inventory, response not in JSON format'
113 print 'Could not retrieve inventory, HTTP error %d' % r.status_code
117 if __name__ == "__main__":
119 parser = argparse.ArgumentParser(description='Restconf test program')
120 parser.add_argument('--odlhost', default='127.0.0.1', help='host where '
121 'odl controller is running (default is 127.0.0.1)')
122 parser.add_argument('--odlport', default='8080', help='port on '
123 'which odl\'s RESTCONF is listening (default is 8080)')
124 parser.add_argument('--plevel', type=int, default=0, help='Print level: '
125 '0 - Summary (just stats); 1 - Node names; 2 - Node details; '
127 parser.add_argument('--datastore', choices=['operational', 'config'], \
128 default='operational', help='Which data store to crawl; '
129 'default operational')
131 in_args = parser.parse_args()
133 url = 'http://' + in_args.odlhost + ":" + in_args.odlport + '/' + \
134 INVENTORY_URL % in_args.datastore
136 print "Crawling '%s'" % url
141 print ' Nodes: %d' % nodes
142 print ' Reported flows: %d' % reported_flows
143 print ' Found flows: %d' % found_flows