1 import constants as const
2 import json, pprint, urllib2, base64, sys
11 parser = optparse.OptionParser(version="0.1")
12 parser.add_option("-i", "--ip", action="store", type="string", dest="odlIp", default="localhost",
13 help="opendaylights ip address")
14 parser.add_option("-t", "--port", action="store", type="string", dest="odlPort", default="8080",
15 help="opendaylights listening tcp port on restconf northbound")
16 parser.add_option("-u", "--user", action="store", type="string", dest="odlUsername", default="admin",
17 help="opendaylight restconf username")
18 parser.add_option("-p", "--password", action="store", type="string", dest="odlPassword", default="admin",
19 help="opendaylight restconf password")
20 parser.add_option("-m", "--method", action="store", type="string", dest="callMethod", default=None,
21 help="method to call")
22 parser.add_option("-d", "--tempdir", action="store", type="string", dest="tempDir", default="/tmp/odl",
23 help="temp directory to store data")
24 (options, args) = parser.parse_args(sys.argv)
36 def create_url(dsType, path):
37 return 'http://{}:{}/restconf/{}/{}/'.format(options.odlIp, options.odlPort, dsType, path)
42 return options.tempDir
49 request = urllib2.Request(url)
50 # You need the replace to handle encodestring adding a trailing newline
51 # (https://docs.python.org/2/library/base64.html#base64.encodestring)
52 base64string = base64.encodestring('{}:{}'.format(options.odlUsername, options.odlPassword)).replace('\n', '')
53 request.add_header('Authorization', 'Basic {}'.format(base64string))
54 result = urllib2.urlopen(request)
55 except urllib2.URLError, e:
56 printError('Unable to send request: {}\n'.format(e))
59 if (result.code != 200):
60 printError( '{}\n{}\n\nError: unexpected code: {}\n'.format(result.info(), result.read(), result.code) )
63 data = json.load(result)
73 def pretty_print(arg):
74 pp = pprint.PrettyPrinter(indent=2)
83 def get_port_name(port):
84 prefix = const.VIF_TYPE_TO_PREFIX.get(port[const.VIF_TYPE])
88 return prefix + port['uuid'][:11]
91 def get_dpn_from_ofnodeid(node_id):
92 return node_id.split(':')[1] if node_id else 'none'
95 def get_ofport_from_ncid(ncid):
96 return ncid.split(':')[2] if ncid and ncid.startswith('openflow') else 0
99 def to_hex(data, ele=None):
101 data = ("0x%x" % data) if data else None
104 data[ele] = "0x%x" % data[ele]
110 def sort(data, field):
111 return sorted(data, key=lambda x: x[field])
114 def filter_flow(flow_dict, filter_list):
117 for flow_filter in filter_list:
118 if flow_dict.get(flow_filter):
123 def show_optionals(flow):
125 lport = flow.get('lport')
126 elantag = flow.get('elan-tag')
127 label = flow.get('mpls')
128 vpnid = flow.get('vpnid')
129 ip = flow.get('iface-ips')
130 smac = flow.get('src-mac')
131 dmac = flow.get('dst-mac')
132 vlanid = flow.get('vlanid')
133 ofport = flow.get('ofport')
135 result = '{},LportTag:{}/{}'.format(result, lport, to_hex(lport))
137 result = '{},OfPort:{}'.format(result, ofport)
139 result = '{},VlanId:{}'.format(result, vlanid)
141 result = '{},VpnId:{}/{}'.format(result, vpnid, to_hex(vpnid*2))
143 result = '{},MplsLabel:{}'.format(result, label)
145 result = '{},ElanTag:{}/{}'.format(result, elantag, to_hex(elantag))
147 result = '{},SrcMac:{}'.format(result, smac)
149 result = '{},DstMac:{}'.format(result, dmac)
151 result = '{},LportIp:{}'.format(result, json.dumps(ip))
152 result = '{},Reason:{}'.format(result, flow.get('reason'))
156 def get_optionals(m_str):
158 return dict(s.split('=',1) for s in m_str.split(','))