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