blob: 46c47a7e7b13c53fc1f027abc013e0684aef8b52 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlabb87672e2015-01-16 10:58:34 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Mar-2013
4
kelvin-onlabb87672e2015-01-16 10:58:34 -08005author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07006
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-onlabb87672e2015-01-16 10:58:34 -080011 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070012
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-onlabb87672e2015-01-16 10:58:34 -080019 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
22FlowVisorDriver is the basic driver which will handle the Mininet functions
kelvin-onlabb87672e2015-01-16 10:58:34 -080023"""
adminbae64d82013-08-01 10:50:15 -070024import re
25import sys
kelvin-onlabb87672e2015-01-16 10:58:34 -080026sys.path.append( "../" )
adminbae64d82013-08-01 10:50:15 -070027from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070028
adminbae64d82013-08-01 10:50:15 -070029
kelvin-onlabb87672e2015-01-16 10:58:34 -080030class 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.
adminbae64d82013-08-01 10:50:15 -070044 for key in connectargs:
kelvin-onlabb87672e2015-01-16 10:58:34 -080045 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
adminbae64d82013-08-01 10:50:15 -070056 self.ssh_handle = self.handle
kelvin-onlabb87672e2015-01-16 10:58:34 -080057
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/'
adminbae64d82013-08-01 10:50:15 -070062 #self.handle.logfile = sys.stdout
kelvin-onlabb87672e2015-01-16 10:58:34 -080063 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
adminbae64d82013-08-01 10:50:15 -070075 pattern = '\d+'
kelvin-onlabb87672e2015-01-16 10:58:34 -080076
77 process_id_search = re.search( "\[\d+\]\s+(\d+)", str( response ) )
adminbae64d82013-08-01 10:50:15 -070078 self.fvprocess_id = "None"
79 if process_id_search:
kelvin-onlabb87672e2015-01-16 10:58:34 -080080 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 )
adminbae64d82013-08-01 10:50:15 -070089 #import time
kelvin-onlabb87672e2015-01-16 10:58:34 -080090 # time.sleep( 10 )
91 #response = self.execute( cmd='./start_visualizer.sh & \r',prompt='\$',timeout=10 )
92
adminbae64d82013-08-01 10:50:15 -070093 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -080094 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" )
adminbae64d82013-08-01 10:50:15 -0700101 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800102
103 def removeFlowSpace( self, id ):
adminbae64d82013-08-01 10:50:15 -0700104 if id == "all":
105 flow_space = self.listFlowSpace()
kelvin-onlabb87672e2015-01-16 10:58:34 -0800106 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
adminbae64d82013-08-01 10:50:15 -0700119 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800120
121 def addFlowSpace( self, **flowspace_args ):
adminbae64d82013-08-01 10:50:15 -0700122 temp_string = None
123 for key in flowspace_args:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800124 if temp_string:
125 temp_string = temp_string + ',' + \
126 key + '=' + flowspace_args[ key ]
127 else:
adminbae64d82013-08-01 10:50:15 -0700128 temp_string = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800129 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 """
adminbae64d82013-08-01 10:50:15 -0700140 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 Hallfebb1c72015-03-05 13:30:09 -0800143 except Exception:
adminbae64d82013-08-01 10:50:15 -0700144 try :
145 if self.nw_src and self.tp_dst:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800146 flowspace = "any 100 dl_type=0x800,nw_proto=6,nw_src="+self.nw_src+",tp_dst="+self.tp_dst+" Slice:"+self.Slice+"=4"
Jon Hallfebb1c72015-03-05 13:30:09 -0800147 except Exception:
adminbae64d82013-08-01 10:50:15 -0700148 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 Hallfebb1c72015-03-05 13:30:09 -0800151 except Exception:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800152 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 ) )
adminbae64d82013-08-01 10:50:15 -0700166 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800167 else:
adminbae64d82013-08-01 10:50:15 -0700168 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800169
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 )
adminbae64d82013-08-01 10:50:15 -0700177 flow_space = main.last_response
kelvin-onlabb87672e2015-01-16 10:58:34 -0800178 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
adminbae64d82013-08-01 10:50:15 -0700187 return flow_space
kelvin-onlabb87672e2015-01-16 10:58:34 -0800188
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 )
adminbae64d82013-08-01 10:50:15 -0700193 devices_list = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800194 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 ) )
adminbae64d82013-08-01 10:50:15 -0700202 if devices_match:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800203 devices_list = devices_list + devices_match.group( 0 ) + "\n"
adminbae64d82013-08-01 10:50:15 -0700204 devices_list = "Device 0: 00:00:00:00:00:00:00:02 \n Device 1: 00:00:00:00:00:00:00:03"
kelvin-onlabb87672e2015-01-16 10:58:34 -0800205 main.log.info( "List of Devices \n" + devices_list )
206
adminbae64d82013-08-01 10:50:15 -0700207 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800208
209 def disconnect( self ):
210
adminbae64d82013-08-01 10:50:15 -0700211 response = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800212 main.log.info( "Stopping the FlowVisor" )
adminbae64d82013-08-01 10:50:15 -0700213 if self.handle:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800214 self.handle.sendline( "kill -9 " + str( self.fvprocess_id ) )
215 else:
216 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700217 response = main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800218 return response