admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Oct-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2015 by ON.Lab |
| 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 9 | |
| 10 | author:: Raghav Kashyap( raghavkashyap@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 11 | |
| 12 | |
| 13 | TestON is free software: you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation, either version 2 of the License, or |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 16 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 17 | |
| 18 | TestON is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 24 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | pox driver provides the basic functions of POX controller |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 28 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | import pexpect |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 30 | import struct |
| 31 | import fcntl |
| 32 | import os |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 33 | import signal |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 34 | import sys |
| 35 | from drivers.common.cli.emulatordriver import Emulator |
| 36 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 37 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 38 | class PoxCliDriver( Emulator ): |
| 39 | |
| 40 | """ |
| 41 | PoxCliDriver driver provides the basic functions of POX controller |
| 42 | """ |
| 43 | def __init__( self ): |
| 44 | super( Emulator, self ).__init__() |
| 45 | self.handle = self |
| 46 | self.wrapped = sys.modules[ __name__ ] |
| 47 | |
| 48 | def connect( self, **connectargs ): |
| 49 | #,user_name, ip_address, pwd,options ): |
| 50 | """ |
| 51 | this subroutine is to launch pox controller . It must have arguments as : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | user_name = host name , |
| 53 | ip_address = ip address of the host , |
| 54 | pwd = password of host , |
| 55 | options = it is a topology hash which will consists the component's details for the test run |
| 56 | |
| 57 | *** host is here a virtual mahine or system where pox framework hierarchy exists |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 58 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | for key in connectargs: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 60 | vars( self )[ key ] = connectargs[ key ] |
| 61 | |
| 62 | self.name = self.options[ 'name' ] |
| 63 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | poxLibPath = 'default' |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 65 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 66 | self.handle = super( |
| 67 | PoxCliDriver, |
| 68 | self ).connect( |
| 69 | user_name=self.user_name, |
| 70 | ip_address=self.ip_address, |
| 71 | port=None, |
| 72 | pwd=self.pwd ) |
| 73 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | if self.handle: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 75 | self.handle.expect( "openflow" ) |
| 76 | command = self.getcmd( self.options ) |
| 77 | # print command |
| 78 | main.log.info( "Entering into POX hierarchy" ) |
| 79 | if self.options[ 'pox_lib_location' ] != 'default': |
| 80 | self.execute( |
| 81 | cmd="cd " + |
| 82 | self.options[ 'pox_lib_location' ], |
| 83 | prompt="/pox\$", |
| 84 | timeout=120 ) |
| 85 | else: |
| 86 | self.execute( |
| 87 | cmd="cd ~/TestON/lib/pox/", |
| 88 | prompt="/pox\$", |
| 89 | timeout=120 ) |
| 90 | # launching pox with components |
| 91 | main.log.info( "launching POX controller with given components" ) |
| 92 | self.execute( cmd=command, prompt="DEBUG:", timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.error( |
| 96 | "Connection failed to the host " + |
| 97 | self.user_name + |
| 98 | "@" + |
| 99 | self.ip_address ) |
| 100 | main.log.error( "Failed to connect to the POX controller" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 102 | |
| 103 | def disconnect( self, handle ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 104 | if self.handle: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 105 | self.execute( cmd="exit()", prompt="/pox\$", timeout=120 ) |
| 106 | else: |
| 107 | main.log.error( "Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 109 | def get_version( self ): |
| 110 | file_input = path + '/lib/pox/core.py' |
| 111 | version = super( PoxCliDriver, self ).get_version() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | pattern = '\s*self\.version(.*)' |
| 113 | import re |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 114 | for line in open( file_input, 'r' ).readlines(): |
| 115 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | if result: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 117 | version = result.group( 0 ) |
| 118 | version = re.sub( |
| 119 | "\s*self\.version\s*=\s*|\(|\)", |
| 120 | '', |
| 121 | version ) |
| 122 | version = re.sub( ",", '.', version ) |
| 123 | version = "POX " + version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 124 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 125 | return version |
| 126 | |
| 127 | def getcmd( self, options ): |
| 128 | command = "./pox.py " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 129 | for item in options.keys(): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 130 | if isinstance( options[ item ], dict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | command = command + item |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 132 | for items in options[ item ].keys(): |
| 133 | if options[ item ][ items ] == "None": |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 134 | command = command + " --" + items + " " |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 135 | else: |
| 136 | command = command + " --" + items + \ |
| 137 | "=" + options[ item ][ items ] + " " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 138 | else: |
| 139 | if item == 'pox_lib_location': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 140 | poxLibPath = options[ item ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | elif item == 'type' or item == 'name': |
| 142 | pass |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 143 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | command = command + item |
| 145 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 146 | return command |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 148 | |
| 149 | if __name__ != "__main__": |
| 150 | import sys |
| 151 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 152 | sys.modules[ __name__ ] = PoxCliDriver() |