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