blob: d0e6ea5eff7a9c41aeafa2bb14e177c93de0a086 [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 pexpect
25import struct
26import fcntl
27import os
28import signal
29import re
30import sys
31import core.teston
kelvin-onlabb87672e2015-01-16 10:58:34 -080032sys.path.append( "../" )
adminbae64d82013-08-01 10:50:15 -070033from drivers.common.cli.emulatordriver import Emulator
34from drivers.common.clidriver import CLI
35
adminbae64d82013-08-01 10:50:15 -070036
kelvin-onlabb87672e2015-01-16 10:58:34 -080037class 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.
adminbae64d82013-08-01 10:50:15 -070051 for key in connectargs:
kelvin-onlabb87672e2015-01-16 10:58:34 -080052 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
adminbae64d82013-08-01 10:50:15 -070063 self.ssh_handle = self.handle
kelvin-onlabb87672e2015-01-16 10:58:34 -080064
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/'
adminbae64d82013-08-01 10:50:15 -070069 #self.handle.logfile = sys.stdout
kelvin-onlabb87672e2015-01-16 10:58:34 -080070 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
adminbae64d82013-08-01 10:50:15 -070082 pattern = '\d+'
kelvin-onlabb87672e2015-01-16 10:58:34 -080083
84 process_id_search = re.search( "\[\d+\]\s+(\d+)", str( response ) )
adminbae64d82013-08-01 10:50:15 -070085 self.fvprocess_id = "None"
86 if process_id_search:
kelvin-onlabb87672e2015-01-16 10:58:34 -080087 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 )
adminbae64d82013-08-01 10:50:15 -070096 #import time
kelvin-onlabb87672e2015-01-16 10:58:34 -080097 # time.sleep( 10 )
98 #response = self.execute( cmd='./start_visualizer.sh & \r',prompt='\$',timeout=10 )
99
adminbae64d82013-08-01 10:50:15 -0700100 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800101 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" )
adminbae64d82013-08-01 10:50:15 -0700108 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800109
110 def removeFlowSpace( self, id ):
adminbae64d82013-08-01 10:50:15 -0700111 if id == "all":
112 flow_space = self.listFlowSpace()
kelvin-onlabb87672e2015-01-16 10:58:34 -0800113 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
adminbae64d82013-08-01 10:50:15 -0700126 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800127
128 def addFlowSpace( self, **flowspace_args ):
adminbae64d82013-08-01 10:50:15 -0700129 temp_string = None
130 for key in flowspace_args:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800131 if temp_string:
132 temp_string = temp_string + ',' + \
133 key + '=' + flowspace_args[ key ]
134 else:
adminbae64d82013-08-01 10:50:15 -0700135 temp_string = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800136 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 """
adminbae64d82013-08-01 10:50:15 -0700147 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-onlabb87672e2015-01-16 10:58:34 -0800153 flowspace = "any 100 dl_type=0x800,nw_proto=6,nw_src="+self.nw_src+",tp_dst="+self.tp_dst+" Slice:"+self.Slice+"=4"
adminbae64d82013-08-01 10:50:15 -0700154 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-onlabb87672e2015-01-16 10:58:34 -0800159 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 ) )
adminbae64d82013-08-01 10:50:15 -0700173 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800174 else:
adminbae64d82013-08-01 10:50:15 -0700175 return main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800176
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 )
adminbae64d82013-08-01 10:50:15 -0700184 flow_space = main.last_response
kelvin-onlabb87672e2015-01-16 10:58:34 -0800185 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
adminbae64d82013-08-01 10:50:15 -0700194 return flow_space
kelvin-onlabb87672e2015-01-16 10:58:34 -0800195
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 )
adminbae64d82013-08-01 10:50:15 -0700200 devices_list = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800201 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 ) )
adminbae64d82013-08-01 10:50:15 -0700209 if devices_match:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800210 devices_list = devices_list + devices_match.group( 0 ) + "\n"
adminbae64d82013-08-01 10:50:15 -0700211 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 -0800212 main.log.info( "List of Devices \n" + devices_list )
213
adminbae64d82013-08-01 10:50:15 -0700214 return main.TRUE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800215
216 def disconnect( self ):
217
adminbae64d82013-08-01 10:50:15 -0700218 response = ''
kelvin-onlabb87672e2015-01-16 10:58:34 -0800219 main.log.info( "Stopping the FlowVisor" )
adminbae64d82013-08-01 10:50:15 -0700220 if self.handle:
kelvin-onlabb87672e2015-01-16 10:58:34 -0800221 self.handle.sendline( "kill -9 " + str( self.fvprocess_id ) )
222 else:
223 main.log.error( "Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -0700224 response = main.FALSE
kelvin-onlabb87672e2015-01-16 10:58:34 -0800225 return response