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