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