Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 17 | import subprocess |
| 18 | import socket |
| 19 | import fcntl |
| 20 | import struct |
| 21 | import os |
A R Karthick | 933f5b5 | 2017-03-27 15:27:16 -0700 | [diff] [blame] | 22 | import logging |
| 23 | |
| 24 | log_test = logging.getLogger('cordTester') |
| 25 | test_consolehandler = logging.StreamHandler() |
| 26 | #test_consolehandler.setFormatter(logging.Formatter("%(levelname)s:%(message)s")) |
| 27 | log_test.addHandler(test_consolehandler) |
A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 28 | |
| 29 | # we use subprocess as commands.getstatusoutput would be deprecated |
| 30 | def getstatusoutput(cmd): |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 31 | command = [ '/bin/bash', '-c', cmd ] |
A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 32 | p = subprocess.Popen(command, stdout = subprocess.PIPE) |
| 33 | out, _ = p.communicate() |
| 34 | return p.returncode, out.strip() |
| 35 | |
| 36 | def 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 | |
| 47 | def 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 Karthick | 33cfdbe | 2017-03-17 18:03:48 -0700 | [diff] [blame] | 61 | def 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 Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 72 | def get_controllers(): |
| 73 | controllers = os.getenv('ONOS_CONTROLLER_IP') or 'localhost' |
| 74 | return controllers.split(',') |
| 75 | |
| 76 | def get_controller(): |
| 77 | controllers = get_controllers() |
| 78 | return controllers[0] |
A R Karthick | 93ba8d0 | 2017-04-13 11:59:58 -0700 | [diff] [blame] | 79 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 80 | def running_on_pod(): |
A R Karthick | 93ba8d0 | 2017-04-13 11:59:58 -0700 | [diff] [blame] | 81 | """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 |