blob: 97039f4977003ec5989483cb70112ee1c0cefb3c [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A.R Karthickbe7768c2017-03-17 11:39:41 -070017import subprocess
18import socket
19import fcntl
20import struct
21import os
A R Karthick933f5b52017-03-27 15:27:16 -070022import logging
23
24log_test = logging.getLogger('cordTester')
25test_consolehandler = logging.StreamHandler()
26#test_consolehandler.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))
27log_test.addHandler(test_consolehandler)
A.R Karthickbe7768c2017-03-17 11:39:41 -070028
29# we use subprocess as commands.getstatusoutput would be deprecated
30def getstatusoutput(cmd):
A.R Karthickc2697a12017-05-24 14:01:15 -070031 command = [ '/bin/bash', '-c', cmd ]
A.R Karthickbe7768c2017-03-17 11:39:41 -070032 p = subprocess.Popen(command, stdout = subprocess.PIPE)
33 out, _ = p.communicate()
34 return p.returncode, out.strip()
35
36def get_ip(iface):
37 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
38 try:
39 info = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(iface[:15])))
40 except:
41 info = None
42 s.close()
43 if info:
44 return '.'.join( [ str(ord(c)) for c in info[20:24] ] )
45 return None
46
47def get_mac(iface = None, pad = 4):
48 if iface is None:
49 iface = os.getenv('TEST_SWITCH', 'ovsbr0')
50 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
51 try:
52 info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15])))
53 except:
54 info = ['0'] * 24
55 s.close()
56 sep = ''
57 if pad == 0:
58 sep = ':'
59 return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]])
60
A.R Karthick33cfdbe2017-03-17 18:03:48 -070061def get_default_gw():
62 cmd = "ip route show | grep default | head -1 | awk '{print $3}'"
63 cmd_dev = "ip route show | grep default | head -1 | awk '{print $NF}'"
64 st, gw = getstatusoutput(cmd)
65 st2, gw_device = getstatusoutput(cmd_dev)
66 if st != 0:
67 gw = None
68 if st2 != 0:
69 gw_device = None
70 return gw, gw_device
71
A.R Karthickbe7768c2017-03-17 11:39:41 -070072def get_controllers():
73 controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
74 return controllers.split(',')
75
76def get_controller():
77 controllers = get_controllers()
78 return controllers[0]
A R Karthick93ba8d02017-04-13 11:59:58 -070079
A R Karthick19771192017-04-25 14:57:05 -070080def running_on_pod():
A R Karthick93ba8d02017-04-13 11:59:58 -070081 """If we are running on Ciab or inside a physical podd, key file would be set"""
82 return True if os.environ.get('SSH_KEY_FILE', None) else False