blob: 7621f13c22a43e1d9ede60a5addcd783a683d32e [file] [log] [blame]
A.R Karthickbe7768c2017-03-17 11:39:41 -07001import subprocess
2import socket
3import fcntl
4import struct
5import os
A R Karthick933f5b52017-03-27 15:27:16 -07006import logging
7
8log_test = logging.getLogger('cordTester')
9test_consolehandler = logging.StreamHandler()
10#test_consolehandler.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))
11log_test.addHandler(test_consolehandler)
A.R Karthickbe7768c2017-03-17 11:39:41 -070012
13# we use subprocess as commands.getstatusoutput would be deprecated
14def getstatusoutput(cmd):
15 command = [ '/bin/sh', '-c', cmd ]
16 p = subprocess.Popen(command, stdout = subprocess.PIPE)
17 out, _ = p.communicate()
18 return p.returncode, out.strip()
19
20def get_ip(iface):
21 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
22 try:
23 info = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(iface[:15])))
24 except:
25 info = None
26 s.close()
27 if info:
28 return '.'.join( [ str(ord(c)) for c in info[20:24] ] )
29 return None
30
31def get_mac(iface = None, pad = 4):
32 if iface is None:
33 iface = os.getenv('TEST_SWITCH', 'ovsbr0')
34 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
35 try:
36 info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15])))
37 except:
38 info = ['0'] * 24
39 s.close()
40 sep = ''
41 if pad == 0:
42 sep = ':'
43 return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]])
44
A.R Karthick33cfdbe2017-03-17 18:03:48 -070045def get_default_gw():
46 cmd = "ip route show | grep default | head -1 | awk '{print $3}'"
47 cmd_dev = "ip route show | grep default | head -1 | awk '{print $NF}'"
48 st, gw = getstatusoutput(cmd)
49 st2, gw_device = getstatusoutput(cmd_dev)
50 if st != 0:
51 gw = None
52 if st2 != 0:
53 gw_device = None
54 return gw, gw_device
55
A.R Karthickbe7768c2017-03-17 11:39:41 -070056def get_controllers():
57 controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
58 return controllers.split(',')
59
60def get_controller():
61 controllers = get_controllers()
62 return controllers[0]