blob: 6ec2ee6f20ec7bf881c621431a972b37a5a8a8d8 [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
adminbae64d82013-08-01 10:50:15 -070026from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070027
adminbae64d82013-08-01 10:50:15 -070028
kelvin-onlabb87672e2015-01-16 10:58:34 -080029class FlowVisorDriver( Emulator ):
30
31 """
32 FlowVisorDriver is the basic driver which will handle the Mininet functions
33 """
34 def __init__( self ):
35 super( Emulator, self ).__init__()
36 self.handle = self
37 self.wrapped = sys.modules[ __name__ ]
38
39 def connect( self, **connectargs ):
40 #,user_name, ip_address, pwd,options ):
41 # Here the main is the TestON instance after creating all the log
42 # handles.
adminbae64d82013-08-01 10:50:15 -070043 for key in connectargs:
kelvin-onlabb87672e2015-01-16 10:58:34 -080044 vars( self )[ key ] = connectargs[ key ]
45
46 self.name = self.options[ 'name' ]
47 self.handle = super(
48 FlowVisorDriver,
49 self ).connect(
50 user_name=self.user_name,
51 ip_address=self.ip_address,
52 port=None,
53 pwd=self.pwd )
54
adminbae64d82013-08-01 10:50:15 -070055 self.ssh_handle = self.handle
kelvin-onlabb87672e2015-01-16 10:58:34 -080056
57 # Copying the readme file to process the
58 if self.handle:
59 self.execute( cmd='\r', prompt='\$', timeout=10 )
60 self.options[ 'path' ] = '/home/openflow/flowvisor/scripts/'
adminbae64d82013-08-01 10:50:15 -070061 #self.handle.logfile = sys.stdout
kelvin-onlabb87672e2015-01-16 10:58:34 -080062 self.execute(
63 cmd='cd ' +
64 self.options[ 'path' ],
65 prompt='\$',
66 timeout=10 )
67 main.log.info( "Starting FlowVisor " )
68
69 response = self.execute(
70 cmd='./flowvisor.sh &',
71 prompt='---\sSetting\slogging\slevel\sto\sNOTE',
72 timeout=10 )
73
adminbae64d82013-08-01 10:50:15 -070074 pattern = '\d+'
kelvin-onlabb87672e2015-01-16 10:58:34 -080075
76 process_id_search = re.search( "\[\d+\]\s+(\d+)", str( response ) )
adminbae64d82013-08-01 10:50:15 -070077 self.fvprocess_id = "None"
78 if process_id_search:
kelvin-onlabb87672e2015-01-16 10:58:34 -080079 self.fvprocess_id = process_id_search.group( 1 )
80
81 utilities.assert_matches(
82 expect=pattern,
83 actual=response,
84 onpass="FlowVisor Started Successfully : Proceess Id :" +
85 self.fvprocess_id,
86 onfail="Failed to start FlowVisor" )
87 main.log.info( response )
adminbae64d82013-08-01 10:50:15 -070088 #import time
kelvin-onlabb87672e2015-01-16 10:58:34 -080089 # time.sleep( 10 )
90 #response = self.execute( cmd='./start_visualizer.sh & \r',prompt='\$',timeout=10 )
91
adminbae64d82013-08-01 10:50:15 -070092 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -080093 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 FlowVisor" )
adminbae64d82013-08-01 10:50:15 -0700100 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800101
102 def removeFlowSpace( self, id ):
adminbae64d82013-08-01 10:50:15 -0700103 if id == "all":
104 flow_space = self.listFlowSpace()
kelvin-onlabb87672e2015-01-16 10:58:34 -0800105 flow_ids = re.findall( "\,id=\[(\d+)\]", flow_space )
106 for id in flow_ids:
107 self.removeFlowSpace( id )
108 else:
109 self.execute( cmd="clear", prompt="\$", timeout=10 )
110 self.execute(
111 cmd="./fvctl.sh removeFlowSpace " +
112 id,
113 prompt="passwd:",
114 timeout=10 )
115 self.execute( cmd="\n", prompt="\$", timeout=10 )
116 main.log.info( "Removed flowSpace which is having id :" + id )
117
adminbae64d82013-08-01 10:50:15 -0700118 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800119
120 def addFlowSpace( self, **flowspace_args ):
adminbae64d82013-08-01 10:50:15 -0700121 temp_string = None
122 for key in flowspace_args:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800123 if temp_string:
124 temp_string = temp_string + ',' + \
125 key + '=' + flowspace_args[ key ]
126 else:
adminbae64d82013-08-01 10:50:15 -0700127 temp_string = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800128 temp_string = temp_string + key + '=' + flowspace_args[ key ]
129
130 src_search = re.search( 'dl_src', temp_string )
131 if src_search:
132 flowspace = "any 100 dl_type=0x806,nw_proto=6," + \
133 temp_string + " Slice:SSH=4"
134 else:
135 flowspace = "any 100 dl_type=0x800,nw_proto=6," + \
136 temp_string + " Slice:SSH=4"
137
138 """
adminbae64d82013-08-01 10:50:15 -0700139 try :
140 if self.dl_src and self.nw_dst:
141 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 -0800142 except Exception:
adminbae64d82013-08-01 10:50:15 -0700143 try :
144 if self.nw_src and self.tp_dst:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800145 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 -0800146 except Exception:
adminbae64d82013-08-01 10:50:15 -0700147 try :
148 if self.nw_src and self.tp_src:
149 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 -0800150 except Exception:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800151 main.log.error( "Please specify flowspace properly" )
152 """
153 # self.execute( cmd="clear",prompt="\$",timeout=10 )
154 self.execute(
155 cmd="./fvctl.sh addFlowSpace " +
156 flowspace,
157 prompt="passwd:",
158 timeout=10 )
159 self.execute( cmd="\n", prompt="\$", timeout=10 )
160 sucess_match = re.search( "success\:\s+(\d+)", main.last_response )
161 if sucess_match:
162 main.log.info(
163 "Added flow Space and id is " +
164 sucess_match.group( 1 ) )
adminbae64d82013-08-01 10:50:15 -0700165 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800166 else:
adminbae64d82013-08-01 10:50:15 -0700167 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800168
169 def listFlowSpace( self ):
170 self.execute( cmd="clear", prompt="\$", timeout=10 )
171 self.execute(
172 cmd="./fvctl.sh listFlowSpace ",
173 prompt="passwd:",
174 timeout=10 )
175 self.execute( cmd="\n", prompt="\$", timeout=10 )
adminbae64d82013-08-01 10:50:15 -0700176 flow_space = main.last_response
kelvin-onlabb87672e2015-01-16 10:58:34 -0800177 flow_space = self.remove_contol_chars( flow_space )
178 flow_space = re.sub(
179 "rule\s(\d+)\:",
180 "\nrule " +
181 r'\1' +
182 ":",
183 flow_space )
184 main.log.info( flow_space )
185
adminbae64d82013-08-01 10:50:15 -0700186 return flow_space
kelvin-onlabb87672e2015-01-16 10:58:34 -0800187
188 def listDevices( self ):
189 # self.execute( cmd="clear",prompt="\$",timeout=10 )
190 #self.execute( cmd="./fvctl.sh listDevices ",prompt="passwd:",timeout=10 )
191 # self.execute( cmd="\n",prompt="\$",timeout=10 )
adminbae64d82013-08-01 10:50:15 -0700192 devices_list = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800193 last_response = re.findall(
194 "(Device\s\d+\:\s((\d|[a-z])(\d|[a-z])\:)+(\d|[a-z])(\d|[a-z]))",
195 main.last_response )
196
197 for resp in last_response:
198 devices_match = re.search(
199 "(Device\s\d+\:\s((\d|[a-z])(\d|[a-z])\:)+(\d|[a-z])(\d|[a-z]))",
200 str( resp ) )
adminbae64d82013-08-01 10:50:15 -0700201 if devices_match:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800202 devices_list = devices_list + devices_match.group( 0 ) + "\n"
adminbae64d82013-08-01 10:50:15 -0700203 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 -0800204 main.log.info( "List of Devices \n" + devices_list )
205
adminbae64d82013-08-01 10:50:15 -0700206 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800207
208 def disconnect( self ):
209
adminbae64d82013-08-01 10:50:15 -0700210 response = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800211 main.log.info( "Stopping the FlowVisor" )
adminbae64d82013-08-01 10:50:15 -0700212 if self.handle:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800213 self.handle.sendline( "kill -9 " + str( self.fvprocess_id ) )
214 else:
215 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700216 response = main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800217 return response