admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Mar-2013 |
| 4 | |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 5 | author:: Anil Kumar ( anilkumar.s@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 | b87672e | 2015-01-16 10:58:34 -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 | b87672e | 2015-01-16 10:58:34 -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 | FlowVisorDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 23 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | import pexpect |
| 25 | import struct |
| 26 | import fcntl |
| 27 | import os |
| 28 | import signal |
| 29 | import re |
| 30 | import sys |
| 31 | import core.teston |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 32 | sys.path.append( "../" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | from drivers.common.cli.emulatordriver import Emulator |
| 34 | from drivers.common.clidriver import CLI |
| 35 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 37 | class FlowVisorDriver( Emulator ): |
| 38 | |
| 39 | """ |
| 40 | FlowVisorDriver is the basic driver which will handle the Mininet functions |
| 41 | """ |
| 42 | def __init__( self ): |
| 43 | super( Emulator, self ).__init__() |
| 44 | self.handle = self |
| 45 | self.wrapped = sys.modules[ __name__ ] |
| 46 | |
| 47 | def connect( self, **connectargs ): |
| 48 | #,user_name, ip_address, pwd,options ): |
| 49 | # Here the main is the TestON instance after creating all the log |
| 50 | # handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | for key in connectargs: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
| 53 | |
| 54 | self.name = self.options[ 'name' ] |
| 55 | self.handle = super( |
| 56 | FlowVisorDriver, |
| 57 | self ).connect( |
| 58 | user_name=self.user_name, |
| 59 | ip_address=self.ip_address, |
| 60 | port=None, |
| 61 | pwd=self.pwd ) |
| 62 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 63 | self.ssh_handle = self.handle |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 64 | |
| 65 | # Copying the readme file to process the |
| 66 | if self.handle: |
| 67 | self.execute( cmd='\r', prompt='\$', timeout=10 ) |
| 68 | self.options[ 'path' ] = '/home/openflow/flowvisor/scripts/' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | #self.handle.logfile = sys.stdout |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 70 | self.execute( |
| 71 | cmd='cd ' + |
| 72 | self.options[ 'path' ], |
| 73 | prompt='\$', |
| 74 | timeout=10 ) |
| 75 | main.log.info( "Starting FlowVisor " ) |
| 76 | |
| 77 | response = self.execute( |
| 78 | cmd='./flowvisor.sh &', |
| 79 | prompt='---\sSetting\slogging\slevel\sto\sNOTE', |
| 80 | timeout=10 ) |
| 81 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 82 | pattern = '\d+' |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 83 | |
| 84 | process_id_search = re.search( "\[\d+\]\s+(\d+)", str( response ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | self.fvprocess_id = "None" |
| 86 | if process_id_search: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 87 | self.fvprocess_id = process_id_search.group( 1 ) |
| 88 | |
| 89 | utilities.assert_matches( |
| 90 | expect=pattern, |
| 91 | actual=response, |
| 92 | onpass="FlowVisor Started Successfully : Proceess Id :" + |
| 93 | self.fvprocess_id, |
| 94 | onfail="Failed to start FlowVisor" ) |
| 95 | main.log.info( response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | #import time |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 97 | # time.sleep( 10 ) |
| 98 | #response = self.execute( cmd='./start_visualizer.sh & \r',prompt='\$',timeout=10 ) |
| 99 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 100 | return main.TRUE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 101 | else: |
| 102 | main.log.error( |
| 103 | "Connection failed to the host " + |
| 104 | self.user_name + |
| 105 | "@" + |
| 106 | self.ip_address ) |
| 107 | main.log.error( "Failed to connect to the FlowVisor" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | return main.FALSE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 109 | |
| 110 | def removeFlowSpace( self, id ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | if id == "all": |
| 112 | flow_space = self.listFlowSpace() |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 113 | flow_ids = re.findall( "\,id=\[(\d+)\]", flow_space ) |
| 114 | for id in flow_ids: |
| 115 | self.removeFlowSpace( id ) |
| 116 | else: |
| 117 | self.execute( cmd="clear", prompt="\$", timeout=10 ) |
| 118 | self.execute( |
| 119 | cmd="./fvctl.sh removeFlowSpace " + |
| 120 | id, |
| 121 | prompt="passwd:", |
| 122 | timeout=10 ) |
| 123 | self.execute( cmd="\n", prompt="\$", timeout=10 ) |
| 124 | main.log.info( "Removed flowSpace which is having id :" + id ) |
| 125 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 126 | return main.TRUE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 127 | |
| 128 | def addFlowSpace( self, **flowspace_args ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 129 | temp_string = None |
| 130 | for key in flowspace_args: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 131 | if temp_string: |
| 132 | temp_string = temp_string + ',' + \ |
| 133 | key + '=' + flowspace_args[ key ] |
| 134 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 135 | temp_string = '' |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 136 | temp_string = temp_string + key + '=' + flowspace_args[ key ] |
| 137 | |
| 138 | src_search = re.search( 'dl_src', temp_string ) |
| 139 | if src_search: |
| 140 | flowspace = "any 100 dl_type=0x806,nw_proto=6," + \ |
| 141 | temp_string + " Slice:SSH=4" |
| 142 | else: |
| 143 | flowspace = "any 100 dl_type=0x800,nw_proto=6," + \ |
| 144 | temp_string + " Slice:SSH=4" |
| 145 | |
| 146 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | try : |
| 148 | if self.dl_src and self.nw_dst: |
| 149 | flowspace = "any 100 dl_type=0x806,dl_src="+self.dl_src+",nw_dst="+self.nw_dst+" Slice:"+self.Slice+"=4" |
| 150 | except : |
| 151 | try : |
| 152 | if self.nw_src and self.tp_dst: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 153 | flowspace = "any 100 dl_type=0x800,nw_proto=6,nw_src="+self.nw_src+",tp_dst="+self.tp_dst+" Slice:"+self.Slice+"=4" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 154 | except : |
| 155 | try : |
| 156 | if self.nw_src and self.tp_src: |
| 157 | flowspace = "any 100 dl_type=0x800,nw_proto=6,nw_src="+self.nw_src+",tp_src="+self.tp_dst+" Slice:"+self.Slice+"=4" |
| 158 | except : |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 159 | main.log.error( "Please specify flowspace properly" ) |
| 160 | """ |
| 161 | # self.execute( cmd="clear",prompt="\$",timeout=10 ) |
| 162 | self.execute( |
| 163 | cmd="./fvctl.sh addFlowSpace " + |
| 164 | flowspace, |
| 165 | prompt="passwd:", |
| 166 | timeout=10 ) |
| 167 | self.execute( cmd="\n", prompt="\$", timeout=10 ) |
| 168 | sucess_match = re.search( "success\:\s+(\d+)", main.last_response ) |
| 169 | if sucess_match: |
| 170 | main.log.info( |
| 171 | "Added flow Space and id is " + |
| 172 | sucess_match.group( 1 ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | return main.TRUE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 174 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 175 | return main.FALSE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 176 | |
| 177 | def listFlowSpace( self ): |
| 178 | self.execute( cmd="clear", prompt="\$", timeout=10 ) |
| 179 | self.execute( |
| 180 | cmd="./fvctl.sh listFlowSpace ", |
| 181 | prompt="passwd:", |
| 182 | timeout=10 ) |
| 183 | self.execute( cmd="\n", prompt="\$", timeout=10 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 184 | flow_space = main.last_response |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 185 | flow_space = self.remove_contol_chars( flow_space ) |
| 186 | flow_space = re.sub( |
| 187 | "rule\s(\d+)\:", |
| 188 | "\nrule " + |
| 189 | r'\1' + |
| 190 | ":", |
| 191 | flow_space ) |
| 192 | main.log.info( flow_space ) |
| 193 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 194 | return flow_space |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 195 | |
| 196 | def listDevices( self ): |
| 197 | # self.execute( cmd="clear",prompt="\$",timeout=10 ) |
| 198 | #self.execute( cmd="./fvctl.sh listDevices ",prompt="passwd:",timeout=10 ) |
| 199 | # self.execute( cmd="\n",prompt="\$",timeout=10 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | devices_list = '' |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 201 | last_response = re.findall( |
| 202 | "(Device\s\d+\:\s((\d|[a-z])(\d|[a-z])\:)+(\d|[a-z])(\d|[a-z]))", |
| 203 | main.last_response ) |
| 204 | |
| 205 | for resp in last_response: |
| 206 | devices_match = re.search( |
| 207 | "(Device\s\d+\:\s((\d|[a-z])(\d|[a-z])\:)+(\d|[a-z])(\d|[a-z]))", |
| 208 | str( resp ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | if devices_match: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 210 | devices_list = devices_list + devices_match.group( 0 ) + "\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 211 | devices_list = "Device 0: 00:00:00:00:00:00:00:02 \n Device 1: 00:00:00:00:00:00:00:03" |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 212 | main.log.info( "List of Devices \n" + devices_list ) |
| 213 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | return main.TRUE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 215 | |
| 216 | def disconnect( self ): |
| 217 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 218 | response = '' |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 219 | main.log.info( "Stopping the FlowVisor" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 220 | if self.handle: |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 221 | self.handle.sendline( "kill -9 " + str( self.fvprocess_id ) ) |
| 222 | else: |
| 223 | main.log.error( "Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 224 | response = main.FALSE |
kelvin-onlab | b87672e | 2015-01-16 10:58:34 -0800 | [diff] [blame] | 225 | return response |