blob: f21c4e9c811bb274e70875be35d0163bbc18406a [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):
A.R Karthickc2697a12017-05-24 14:01:15 -070015 command = [ '/bin/bash', '-c', cmd ]
A.R Karthickbe7768c2017-03-17 11:39:41 -070016 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]
A R Karthick93ba8d02017-04-13 11:59:58 -070063
A R Karthick19771192017-04-25 14:57:05 -070064def running_on_pod():
A R Karthick93ba8d02017-04-13 11:59:58 -070065 """If we are running on Ciab or inside a physical podd, key file would be set"""
66 return True if os.environ.get('SSH_KEY_FILE', None) else False