blob: 6689dd4298caba6a9894cb6389903c38237bc9a5 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
James Leec9cacaf2014-04-08 09:17:39 -07003Created on 31-May-2013
adminbae64d82013-08-01 10:50:15 -07004
James Leec9cacaf2014-04-08 09:17:39 -07005@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
adminbae64d82013-08-01 10:50:15 -07006
James Leec9cacaf2014-04-08 09:17:39 -07007TestON is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
adminbae64d82013-08-01 10:50:15 -070011
James Leec9cacaf2014-04-08 09:17:39 -070012TestON is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
adminbae64d82013-08-01 10:50:15 -070016
James Leec9cacaf2014-04-08 09:17:39 -070017You should have received a copy of the GNU General Public License
18along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070019
20
James Leec9cacaf2014-04-08 09:17:39 -070021'''
adminbae64d82013-08-01 10:50:15 -070022import time
23import pexpect
24import struct, fcntl, os, sys, signal
adminbae64d82013-08-01 10:50:15 -070025import re
26import json
Jon Hallf89c8552014-04-02 13:14:06 -070027import traceback
adminbae64d82013-08-01 10:50:15 -070028sys.path.append("../")
29from drivers.common.clidriver import CLI
30
31class OnosCliDriver(CLI):
32
33 def __init__(self):
34 super(CLI, self).__init__()
35
36 def connect(self,**connectargs):
Jon Halld8dc5772014-04-08 16:26:29 -070037 '''
38 Creates ssh handle for ONOS.
39 '''
Jon Hallf89c8552014-04-02 13:14:06 -070040 try:
41 for key in connectargs:
admine0eeea22014-04-14 10:22:46 -070042 vars(self)[key] = connectargs[key]
Jon Hallf89c8552014-04-02 13:14:06 -070043 self.home = "~/ONOS"
44 for key in self.options:
admine0eeea22014-04-14 10:22:46 -070045 if key == "home":
46 self.home = self.options['home']
47 break
adminbae64d82013-08-01 10:50:15 -070048
Jon Hallf89c8552014-04-02 13:14:06 -070049
50 self.name = self.options['name']
51 self.handle = super(OnosCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd, home = self.home)
adminbae64d82013-08-01 10:50:15 -070052
Jon Hallf89c8552014-04-02 13:14:06 -070053 if self.handle:
Jon Hallf89c8552014-04-02 13:14:06 -070054 return self.handle
55 else :
56 main.log.info("NO ONOS HANDLE")
57 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -070058 except pexpect.EOF:
59 main.log.error(self.name + ": EOF exception found")
60 main.log.error(self.name + ": " + self.handle.before)
61 main.cleanup()
62 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -070063 except:
64 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
65 main.log.error( traceback.print_exc() )
66 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
67 main.cleanup()
68 main.exit()
adminbae64d82013-08-01 10:50:15 -070069
70 def start(self):
Jon Halld8dc5772014-04-08 16:26:29 -070071 '''
72 Starts ONOS on remote machine.
73 Returns false if any errors were encountered.
74 '''
James Leec9cacaf2014-04-08 09:17:39 -070075 try:
Jon Hallf89c8552014-04-02 13:14:06 -070076 self.handle.sendline("")
Jon Hallae05dc22014-04-16 10:56:28 -070077 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
adminf939f8b2014-04-03 17:22:56 -070078 self.handle.sendline(self.home + "/onos.sh core start")
Jon Hallae05dc22014-04-16 10:56:28 -070079 i=self.handle.expect(["STARTED","FAILED",pexpect.EOF,pexpect.TIMEOUT])
80 response = self.handle.before + str(self.handle.after)
Jon Hallf89c8552014-04-02 13:14:06 -070081 if i==0:
Jon Hallae05dc22014-04-16 10:56:28 -070082 j = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], timeout=60)
83 if re.search("Killed",response):
84 main.log.warn(self.name + ": Killed existing process")
85 if j==0:
Jon Hallf89c8552014-04-02 13:14:06 -070086 main.log.info(self.name + ": ONOS Started ")
Jon Hallae05dc22014-04-16 10:56:28 -070087 return main.TRUE
88 elif j==1:
89 main.log.error(self.name + ": EOF exception found")
90 main.log.error(self.name + ": " + self.handle.before)
91 main.cleanup()
92 main.exit()
93 elif j==2:
Jon Hallf89c8552014-04-02 13:14:06 -070094 main.log.info(self.name + ": ONOS NOT Started, stuck while waiting for it to start ")
95 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -070096 else:
97 main.log.warn(self.name +": Unexpected response in start")
98 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -070099 elif i==1:
Jon Hallae05dc22014-04-16 10:56:28 -0700100 main.log.error(self.name + ": ONOS Failed to start")
adminbae64d82013-08-01 10:50:15 -0700101 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700102 elif i==2:
103 main.log.error(self.name + ": EOF exception found")
104 main.log.error(self.name + ": " + self.handle.before)
105 main.cleanup()
106 main.exit()
107 elif i==3:
108 main.log.error(self.name + ": ONOS timedout while starting")
109 return main.FALSE
110 else:
111 main.log.error(self.name + ": ONOS start expect script missed something... ")
adminbae64d82013-08-01 10:50:15 -0700112 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700113 except pexpect.EOF:
114 main.log.error(self.name + ": EOF exception found")
115 main.log.error(self.name + ": " + self.handle.before)
116 main.cleanup()
117 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700118 except:
119 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
120 main.log.error( traceback.print_exc() )
121 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
122 main.cleanup()
123 main.exit()
admine0ae8202013-08-28 11:51:43 -0700124
adminbae64d82013-08-01 10:50:15 -0700125 def start_rest(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700126 '''
127 Starts the rest server on ONOS.
128 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700129 try:
130 response = self.execute(cmd= self.home + "/start-rest.sh start",prompt="\$",timeout=10)
131 if re.search("admin",response):
132 main.log.info(self.name + ": Rest Server Started Successfully")
133 time.sleep(5)
134 return main.TRUE
135 else :
James Leec9cacaf2014-04-08 09:17:39 -0700136 main.log.warn(self.name + ": Failed to start Rest Server")
137 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700138 except pexpect.EOF:
139 main.log.error(self.name + ": EOF exception found")
140 main.log.error(self.name + ": " + self.handle.before)
141 main.cleanup()
142 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700143 except:
144 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
145 main.log.error( traceback.print_exc() )
146 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
147 main.cleanup()
148 main.exit()
149
adminbae64d82013-08-01 10:50:15 -0700150 def status(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700151 '''
152 Called onos.sh core status and returns TRUE/FALSE accordingly
153 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700154 try:
155 self.execute(cmd="\n",prompt="\$",timeout=10)
adminf939f8b2014-04-03 17:22:56 -0700156 response = self.execute(cmd= self.home + "/onos.sh core status ",prompt="\d+\sinstance\sof\sonos\srunning",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700157 self.execute(cmd="\n",prompt="\$",timeout=10)
158 if re.search("1\sinstance\sof\sonos\srunning",response):
159 return main.TRUE
160 elif re.search("0\sinstance\sof\sonos\srunning",response):
161 return main.FALSE
162 else :
163 main.log.info( self.name + " WARNING: status recieved unknown response")
164 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700165 except pexpect.EOF:
166 main.log.error(self.name + ": EOF exception found")
167 main.log.error(self.name + ": " + self.handle.before)
168 main.cleanup()
169 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700170 except:
171 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
172 main.log.error( traceback.print_exc() )
173 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
174 main.cleanup()
175 main.exit()
176
adminbae64d82013-08-01 10:50:15 -0700177
178 def isup(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700179 '''
180 A more complete check to see if ONOS is up and running properly.
181 First, it checks if the process is up.
182 Second, it reads the logs for "Exception: Connection refused"
183 Third, it makes sure the logs are actually moving.
184 returns TRUE/FALSE accordingly.
185 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700186 try:
187 self.execute(cmd="\n",prompt="\$",timeout=10)
adminf939f8b2014-04-03 17:22:56 -0700188 response = self.execute(cmd= self.home + "/onos.sh core status ",prompt="running",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700189 self.execute(cmd="\n",prompt="\$",timeout=10)
190 tail1 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
191 time.sleep(30)
192 self.execute(cmd="\n",prompt="\$",timeout=10)
193 tail2 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
194 pattern = '(.*)1 instance(.*)'
195 pattern2 = '(.*)Exception: Connection refused(.*)'
196 # if utilities.assert_matches(expect=pattern,actual=response,onpass="ONOS process is running...",onfail="ONOS process not running..."):
197
198 if re.search(pattern, response):
199 main.log.info(self.name + ": ONOS process is running...")
200 if tail1 == tail2:
201 main.log.error(self.name + ": ONOS is frozen...")#logs aren't moving
202 return main.FALSE
203 elif re.search( pattern2,tail1 ):
James Leec9cacaf2014-04-08 09:17:39 -0700204 main.log.info(self.name + ": Connection Refused found in onos log")
Jon Hallf89c8552014-04-02 13:14:06 -0700205 return main.FALSE
206 elif re.search( pattern2,tail2 ):
James Leec9cacaf2014-04-08 09:17:39 -0700207 main.log.info(self.name + ": Connection Refused found in onos log")
Jon Hallf89c8552014-04-02 13:14:06 -0700208 return main.FALSE
209 else:
210 main.log.info(self.name + ": Onos log is moving! It's looking good!")
211 return main.TRUE
adminbae64d82013-08-01 10:50:15 -0700212 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700213 main.log.error(self.name + ": ONOS process not running...")
214 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700215 except pexpect.EOF:
216 main.log.error(self.name + ": EOF exception found")
217 main.log.error(self.name + ": " + self.handle.before)
218 main.cleanup()
219 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700220 except:
221 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
222 main.log.error( traceback.print_exc() )
223 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
224 main.cleanup()
225 main.exit()
adminbae64d82013-08-01 10:50:15 -0700226
Jon Hallf89c8552014-04-02 13:14:06 -0700227
228
James Leec9cacaf2014-04-08 09:17:39 -0700229 def rest_status(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700230 '''
231 Checks if the rest server is running.
232 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700233 try:
234 response = self.execute(cmd= self.home + "/start-rest.sh status ",prompt="running",timeout=10)
235 if re.search("rest\sserver\sis\srunning",response):
236 main.log.info(self.name + ": Rest Server is running")
Jon Hallae05dc22014-04-16 10:56:28 -0700237 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700238 elif re.search("rest\sserver\sis\snot\srunning",response):
239 main.log.warn(self.name + ": Rest Server is not Running")
Jon Hallae05dc22014-04-16 10:56:28 -0700240 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700241 else :
242 main.log.error(self.name + ": No response" +response)
Jon Hallae05dc22014-04-16 10:56:28 -0700243 return main.FALSE
244 except pexpect.EOF:
245 main.log.error(self.name + ": EOF exception found")
246 main.log.error(self.name + ": " + self.handle.before)
247 main.cleanup()
248 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700249 except:
250 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
251 main.log.error( traceback.print_exc() )
252 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
253 main.cleanup()
254 main.exit()
255
256
adminbae64d82013-08-01 10:50:15 -0700257 def stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700258 '''
259 Runs ./onos.sh core stop to stop ONOS
260 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700261 try:
262 self.handle.sendline("")
Jon Hallae05dc22014-04-16 10:56:28 -0700263 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
adminf939f8b2014-04-03 17:22:56 -0700264 self.handle.sendline(self.home + "/onos.sh core stop")
Jon Hallae05dc22014-04-16 10:56:28 -0700265 i=self.handle.expect(["Stop",pexpect.EOF,pexpect.TIMEOUT])
266 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], 60)
James Leec9cacaf2014-04-08 09:17:39 -0700267 result = self.handle.before
Jon Hallf89c8552014-04-02 13:14:06 -0700268 if re.search("Killed", result):
269 main.log.info(self.name + ": ONOS Killed Successfully")
270 return main.TRUE
271 else :
272 main.log.warn(self.name + ": ONOS wasn't running")
273 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700274 except pexpect.EOF:
275 main.log.error(self.name + ": EOF exception found")
276 main.log.error(self.name + ": " + self.handle.before)
277 main.cleanup()
278 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700279 except:
280 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
281 main.log.error( traceback.print_exc() )
282 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
283 main.cleanup()
284 main.exit()
285
adminbae64d82013-08-01 10:50:15 -0700286
287 def rest_stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700288 '''
289 Runs ./start-rest.sh stop to stop ONOS rest server
290 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700291 try:
292 response = self.execute(cmd= self.home + "/start-rest.sh stop ",prompt="killing",timeout=10)
293 self.execute(cmd="\n",prompt="\$",timeout=10)
294 if re.search("killing", response):
295 main.log.info(self.name + ": Rest Server Stopped")
296 return main.TRUE
297 else :
298 main.log.error(self.name + ": Failed to Stop, Rest Server is not Running")
299 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700300 except pexpect.EOF:
301 main.log.error(self.name + ": EOF exception found")
302 main.log.error(self.name + ": " + self.handle.before)
303 main.cleanup()
304 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700305 except:
306 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
307 main.log.error( traceback.print_exc() )
308 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
309 main.cleanup()
310 main.exit()
311
312
adminbae64d82013-08-01 10:50:15 -0700313 def disconnect(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700314 '''
315 Called when Test is complete to disconnect the ONOS handle.
316 '''
adminaeedddd2013-08-02 15:14:15 -0700317 response = ''
318 try:
adminbae64d82013-08-01 10:50:15 -0700319 self.handle.sendline("exit")
adminaeedddd2013-08-02 15:14:15 -0700320 self.handle.expect("closed")
Jon Hallae05dc22014-04-16 10:56:28 -0700321 except pexpect.EOF:
322 main.log.error(self.name + ": EOF exception found")
323 main.log.error(self.name + ": " + self.handle.before)
324 main.cleanup()
325 main.exit()
James Leec9cacaf2014-04-08 09:17:39 -0700326 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700327 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700328 response = main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700329 return response
330
adminbae64d82013-08-01 10:50:15 -0700331 def get_version(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700332 '''
333 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
334 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700335 try:
336 self.handle.sendline("export TERM=xterm-256color")
337 self.handle.expect("xterm-256color")
James Leec9cacaf2014-04-08 09:17:39 -0700338 self.handle.expect("\$")
adminf939f8b2014-04-03 17:22:56 -0700339 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
Jon Hallf89c8552014-04-02 13:14:06 -0700340 self.handle.expect("cd ..")
341 self.handle.expect("\$")
admine0eeea22014-04-14 10:22:46 -0700342 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
343 main.log.report(response)
344 lines=response.splitlines()
345 for line in lines:
346 print line
347 return lines[2]
Jon Hallae05dc22014-04-16 10:56:28 -0700348 except pexpect.EOF:
349 main.log.error(self.name + ": EOF exception found")
350 main.log.error(self.name + ": " + self.handle.before)
351 main.cleanup()
352 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700353 except:
354 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
355 main.log.error( traceback.print_exc() )
356 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
357 main.cleanup()
358 main.exit()
adminbae64d82013-08-01 10:50:15 -0700359
Jon Hallf89c8552014-04-02 13:14:06 -0700360 def add_flow(self, testONip, user, password, flowDef):
Jon Halld8dc5772014-04-08 16:26:29 -0700361 '''
362 Copies the flowdef file from TestStation -> ONOS machine
363 Then runs ./add_flow.py to add the flows to ONOS
364 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700365 try:
366 main.log.info("Adding Flows...")
367 self.handle.sendline("scp %s@%s:%s /tmp/flowtmp" %(user,testONip,flowDef))
368 i=self.handle.expect(['[pP]assword:', '100%', pexpect.TIMEOUT],30)
369 if(i==0):
370 self.handle.sendline("%s" %(password))
371 self.handle.sendline("")
372 self.handle.expect("100%")
373 self.handle.expect("\$", 30)
374 self.handle.sendline(self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
375 self.handle.expect("\$", 1000)
376 main.log.info("Flows added")
377 return main.TRUE
378
379 elif(i==1):
380 self.handle.sendline("")
381 self.handle.expect("\$", 10)
382 self.handle.sendline( self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
383 self.handle.expect("\$", 1000)
384 main.log.info("Flows added")
385 return main.TRUE
386
387 elif(i==2):
388 main.log.error("Flow Def file SCP Timed out...")
389 return main.FALSE
390
391 else:
392 main.log.error("Failed to add flows...")
James Leec9cacaf2014-04-08 09:17:39 -0700393 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700394 except pexpect.EOF:
395 main.log.error(self.name + ": EOF exception found")
396 main.log.error(self.name + ": " + self.handle.before)
397 main.cleanup()
398 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700399 except:
400 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
401 main.log.error( traceback.print_exc() )
402 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
403 main.cleanup()
404 main.exit()
405
adminbae64d82013-08-01 10:50:15 -0700406
407 def delete_flow(self, *delParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700408 '''
409 Deletes a specific flow, a range of flows, or all flows.
410 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700411 try:
412 if len(delParams)==1:
413 if str(delParams[0])=="all":
414 main.log.info(self.name + ": Deleting ALL flows...")
415 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh all",prompt="done",timeout=150)
416 self.handle.sendline(self.home + "/web/delete_flow.py all")
417 self.handle.expect("delete_flow")
418 self.handle.expect("\$",1000)
419 main.log.info(self.name + ": Flows deleted")
420 else:
421 main.log.info(self.name + ": Deleting flow "+str(delParams[0])+"...")
422 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0]),prompt="done",timeout=150)
423 #self.execute(cmd="\n",prompt="\$",timeout=60)
424 self.handle.sendline(self.home +"/web/delete_flow.py %d" % int(delParams[0]))
425 self.handle.expect("delete_flow")
426 self.handle.expect("\$",60)
427 main.log.info(self.name + ": Flow deleted")
428 elif len(delParams)==2:
429 main.log.info(self.name + ": Deleting flows "+str(delParams[0])+" through "+str(delParams[1])+"...")
430 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0])+" "+str(delParams[1]),prompt="done",timeout=150)
431 #self.execute(cmd="\n",prompt="\$",timeout=60)
432 self.handle.sendline(self.home + "/web/delete_flow.py %d %d" % (int(delParams[0]), int(delParams[1])))
433 self.handle.expect("delete_flow")
434 self.handle.expect("\$",600)
435 main.log.info(self.name + ": Flows deleted")
Jon Hallae05dc22014-04-16 10:56:28 -0700436 except pexpect.EOF:
437 main.log.error(self.name + ": EOF exception found")
438 main.log.error(self.name + ": " + self.handle.before)
439 main.cleanup()
440 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700441 except:
442 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
443 main.log.error( traceback.print_exc() )
444 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
445 main.cleanup()
446 main.exit()
adminbae64d82013-08-01 10:50:15 -0700447
448 def check_flow(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700449 '''
450 Calls the ./get_flow.py all and checks:
451 - If each FlowPath has at least one FlowEntry
452 - That there are no "NOT"s found
453 returns TRUE/FALSE
454 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700455 try:
456 flowEntryDetect = 1
457 count = 0
458 self.handle.sendline("clear")
459 time.sleep(1)
460 self.handle.sendline(self.home + "/web/get_flow.py all")
461 self.handle.expect("get_flow")
462 while 1:
463 i=self.handle.expect(['FlowPath','FlowEntry','NOT','\$',pexpect.TIMEOUT],timeout=180)
464 if i==0:
465 count = count + 1
466 if flowEntryDetect == 0:
467 main.log.info(self.name + ": FlowPath without FlowEntry")
468 return main.FALSE
469 else:
470 flowEntryDetect = 0
471 elif i==1:
472 flowEntryDetect = 1
473 elif i==2:
474 main.log.error(self.name + ": Found a NOT")
adminbae64d82013-08-01 10:50:15 -0700475 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700476 elif i==3:
477 if count == 0:
478 main.log.info(self.name + ": There don't seem to be any flows here...")
479 return main.FALSE
480 else:
481 main.log.info(self.name + ": All flows pass")
482 main.log.info(self.name + ": Number of FlowPaths: "+str(count))
483 return main.TRUE
484 elif i==4:
James Leec9cacaf2014-04-08 09:17:39 -0700485 main.log.error(self.name + ":Check_flow() - Command Timeout!")
Jon Hallf89c8552014-04-02 13:14:06 -0700486 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700487 except pexpect.EOF:
488 main.log.error(self.name + ": EOF exception found")
489 main.log.error(self.name + ": " + self.handle.before)
490 main.cleanup()
491 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700492 except:
493 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
494 main.log.error( traceback.print_exc() )
495 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
496 main.cleanup()
497 main.exit()
adminbae64d82013-08-01 10:50:15 -0700498
499 def get_flow(self, *flowParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700500 '''
501 Returns verbose output of ./get_flow.py
502 '''
503 try:
504 if len(flowParams)==1:
505 if str(flowParams[0])=="all":
506 self.execute(cmd="\n",prompt="\$",timeout=60)
507 main.log.info(self.name + ": Getting all flow data...")
508 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh all",prompt="done",timeout=150)
509 self.execute(cmd="\n",prompt="\$",timeout=60)
510 return data
511 else:
512 main.log.info(self.name + ": Retrieving flow "+str(flowParams[0])+" data...")
513 data = self.execute(cmd=self.home +"/scripts/TestON_get_flow.sh "+str(flowParams[0]),prompt="done",timeout=150)
514 self.execute(cmd="\n",prompt="\$",timeout=60)
515 return data
516 elif len(flowParams)==5:
517 main.log.info(self.name + ": Retrieving flow installer data...")
518 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh "+str(flowParams[0])+" "+str(flowParams[1])+" "+str(flowParams[2])+" "+str(flowParams[3])+" "+str(flowParams[4]),prompt="done",timeout=150)
519 self.execute(cmd="\n",prompt="\$",timeout=60)
520 return data
521 elif len(flowParams)==4:
522 main.log.info(self.name + ": Retrieving flow endpoints...")
523 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh "+str(flowParams[0])+" "+str(flowParams[1])+" "+str(flowParams[2])+" "+str(flowParams[3]),prompt="done",timeout=150)
524 self.execute(cmd="\n",prompt="\$",timeout=60)
525 return data
Jon Hallae05dc22014-04-16 10:56:28 -0700526 except pexpect.EOF:
527 main.log.error(self.name + ": EOF exception found")
528 main.log.error(self.name + ": " + self.handle.before)
529 main.cleanup()
530 main.exit()
Jon Halld8dc5772014-04-08 16:26:29 -0700531 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700532 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
533 main.log.error( traceback.print_exc() )
534 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
535 main.cleanup()
536 main.exit()
adminbae64d82013-08-01 10:50:15 -0700537
538
Jon Hall8060af02014-04-14 14:17:58 -0700539# http://localhost:8080/wm/onos/ng/switches/json
540# http://localhost:8080/wm/onos/ng/links/json
541# http://localhost:8080/wm/onos/registry/controllers/json
542# http://localhost:8080/wm/onos/registry/switches/json"
adminbae64d82013-08-01 10:50:15 -0700543
544 def get_json(self, url):
Jon Halld8dc5772014-04-08 16:26:29 -0700545 '''
546 Helper functions used to parse the json output of a rest call
547 '''
adminbae64d82013-08-01 10:50:15 -0700548 try:
Jon Hallf89c8552014-04-02 13:14:06 -0700549 try:
550 command = "curl -s %s" % (url)
551 result = os.popen(command).read()
552 parsedResult = json.loads(result)
553 except:
554 print "REST IF %s has issue" % command
555 parsedResult = ""
556
557 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
558 print "REST %s returned code %s" % (command, parsedResult['code'])
559 parsedResult = ""
James Leec9cacaf2014-04-08 09:17:39 -0700560 return parsedResult
Jon Hallae05dc22014-04-16 10:56:28 -0700561 except pexpect.EOF:
562 main.log.error(self.name + ": EOF exception found")
563 main.log.error(self.name + ": " + self.handle.before)
564 main.cleanup()
565 main.exit()
adminbae64d82013-08-01 10:50:15 -0700566 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700567 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
568 main.log.error( traceback.print_exc() )
569 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
570 main.cleanup()
571 main.exit()
adminbae64d82013-08-01 10:50:15 -0700572
Jon Hallf89c8552014-04-02 13:14:06 -0700573 def check_switch(self,RestIP,correct_nr_switch, RestPort ="8080" ):
Jon Halld8dc5772014-04-08 16:26:29 -0700574 '''
575 Used by check_status
576 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700577 try:
578 buf = ""
579 retcode = 0
admine0eeea22014-04-14 10:22:46 -0700580 url="http://%s:%s/wm/onos/ng/switches/json" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700581 parsedResult = self.get_json(url)
582 if parsedResult == "":
583 retcode = 1
584 return (retcode, "Rest API has an issue")
585 url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
586 registry = self.get_json(url)
587
588 if registry == "":
589 retcode = 1
590 return (retcode, "Rest API has an issue")
591
592 cnt = 0
593 active = 0
adminbae64d82013-08-01 10:50:15 -0700594
Jon Hallf89c8552014-04-02 13:14:06 -0700595 for s in parsedResult:
596 cnt += 1
James Leec9cacaf2014-04-08 09:17:39 -0700597 if s['state'] == "ACTIVE":
Jon Hallf89c8552014-04-02 13:14:06 -0700598 active += 1
adminbae64d82013-08-01 10:50:15 -0700599
Jon Hallf89c8552014-04-02 13:14:06 -0700600 buf += "switch: network %d : %d switches %d active\n" % (0+1, cnt, active)
601 if correct_nr_switch != cnt:
602 buf += "switch fail: network %d should have %d switches but has %d\n" % (1, correct_nr_switch, cnt)
603 retcode = 1
adminbae64d82013-08-01 10:50:15 -0700604
Jon Hallf89c8552014-04-02 13:14:06 -0700605 if correct_nr_switch != active:
606 buf += "switch fail: network %d should have %d active switches but has %d\n" % (1, correct_nr_switch, active)
607 retcode = 1
608
609 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700610 except pexpect.EOF:
611 main.log.error(self.name + ": EOF exception found")
612 main.log.error(self.name + ": " + self.handle.before)
613 main.cleanup()
614 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700615 except:
616 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
617 main.log.error( traceback.print_exc() )
618 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
619 main.cleanup()
620 main.exit()
adminbae64d82013-08-01 10:50:15 -0700621
Jon Hallf89c8552014-04-02 13:14:06 -0700622 def check_link(self,RestIP, nr_links, RestPort = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700623 '''
624 Used by check_status
625 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700626 try:
627 buf = ""
628 retcode = 0
629
admine0eeea22014-04-14 10:22:46 -0700630 url = "http://%s:%s/wm/onos/ng/links/json" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700631 parsedResult = self.get_json(url)
632
633 if parsedResult == "":
634 retcode = 1
635 return (retcode, "Rest API has an issue")
636
637 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
638 intra = 0
639 interlink=0
640
641 for s in parsedResult:
James Leec9cacaf2014-04-08 09:17:39 -0700642 intra = intra + 1
Jon Hallf89c8552014-04-02 13:14:06 -0700643
644 if intra != nr_links:
645 buf += "link fail\n"
646 retcode = 1
647
648 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700649 except pexpect.EOF:
650 main.log.error(self.name + ": EOF exception found")
651 main.log.error(self.name + ": " + self.handle.before)
652 main.cleanup()
653 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700654 except:
655 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
656 main.log.error( traceback.print_exc() )
657 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
658 main.cleanup()
659 main.exit()
adminbae64d82013-08-01 10:50:15 -0700660
Jon Hallf89c8552014-04-02 13:14:06 -0700661 def check_status_report(self, ip, numoswitch, numolink, port="8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700662 '''
663 Checks the number of swithes & links that ONOS sees against the supplied values.
664 Writes to the report log.
665 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700666 try:
James Leec9cacaf2014-04-08 09:17:39 -0700667 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700668 switch = self.check_switch(ip, int(numoswitch), port)
669 link = self.check_link(ip, int(numolink), port)
670 value = switch[0]
671 value += link[0]
672 main.log.report( self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
673 if value != 0:
674 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700675 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700676 # "PASS"
677 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700678 except pexpect.EOF:
679 main.log.error(self.name + ": EOF exception found")
680 main.log.error(self.name + ": " + self.handle.before)
681 main.cleanup()
682 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700683 except:
684 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
685 main.log.error( traceback.print_exc() )
686 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
687 main.cleanup()
688 main.exit()
adminbae64d82013-08-01 10:50:15 -0700689
Jon Hallf89c8552014-04-02 13:14:06 -0700690 def check_status(self, ip, numoswitch, numolink, port = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700691 '''
692 Checks the number of swithes & links that ONOS sees against the supplied values.
693 Writes to the main log.
694 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700695 try:
James Leec9cacaf2014-04-08 09:17:39 -0700696 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700697 switch = self.check_switch(ip, int(numoswitch), port)
698 link = self.check_link(ip, int(numolink), port)
699 value = switch[0]
700 value += link[0]
701 main.log.info(self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
702 if value != 0:
703 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700704 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700705 # "PASS"
706 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700707 except pexpect.EOF:
708 main.log.error(self.name + ": EOF exception found")
709 main.log.error(self.name + ": " + self.handle.before)
710 main.cleanup()
711 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700712 except:
713 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
714 main.log.error( traceback.print_exc() )
715 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
716 main.cleanup()
717 main.exit()
adminbae64d82013-08-01 10:50:15 -0700718
adminbae64d82013-08-01 10:50:15 -0700719 def check_for_no_exceptions(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700720 '''
721 TODO: Rewrite
722 Used by CassndraCheck.py to scan ONOS logs for Exceptions
723 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700724 try:
725 self.handle.sendline("dsh 'grep Exception ~/ONOS/onos-logs/onos.*.log'")
James Leec9cacaf2014-04-08 09:17:39 -0700726 self.handle.expect("\$ dsh")
Jon Hallf89c8552014-04-02 13:14:06 -0700727 self.handle.expect("\$")
728 output = self.handle.before
James Leec9cacaf2014-04-08 09:17:39 -0700729 main.log.info(self.name + ": " + output )
Jon Hallf89c8552014-04-02 13:14:06 -0700730 if re.search("Exception",output):
731 return main.FALSE
732 else :
733 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700734 except pexpect.EOF:
735 main.log.error(self.name + ": EOF exception found")
736 main.log.error(self.name + ": " + self.handle.before)
737 main.cleanup()
738 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700739 except:
740 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
741 main.log.error( traceback.print_exc() )
742 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
743 main.cleanup()
744 main.exit()
745
746
admine0eeea22014-04-14 10:22:46 -0700747 def git_pull(self, comp1=""):
Jon Halld8dc5772014-04-08 16:26:29 -0700748 '''
Jon Halld8dc5772014-04-08 16:26:29 -0700749 Assumes that "git pull" works without login
admine0eeea22014-04-14 10:22:46 -0700750
751 This function will perform a git pull on the ONOS instance.
752 If used as git_pull("NODE") it will do git pull + NODE. This is
753 for the purpose of pulling from other nodes if necessary.
754
755 Otherwise, this function will perform a git pull in the
756 ONOS repository. If it has any problems, it will return main.ERROR
757 If it successfully does a git_pull, it will return a 1.
758 If it has no updates, it will return a 0.
759
Jon Halld8dc5772014-04-08 16:26:29 -0700760 '''
Jon Hallae05dc22014-04-16 10:56:28 -0700761 try:
762 main.log.info(self.name + ": Stopping ONOS")
763 self.stop()
764 self.handle.sendline("cd " + self.home)
765 self.handle.expect("ONOS\$")
766 if comp1=="":
767 self.handle.sendline("git pull")
768 else:
769 self.handle.sendline("git pull " + comp1)
770
771 uptodate = 0
772 i=self.handle.expect(['fatal','Username\sfor\s(.*):\s','Unpacking\sobjects',pexpect.TIMEOUT,'Already up-to-date','Aborting'],timeout=180)
773 #debug
774 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
775 if i==0:
776 main.log.error(self.name + ": Git pull had some issue...")
777 return main.ERROR
778 elif i==1:
779 main.log.error(self.name + ": Git Pull Asking for username!!! BADD!")
780 return main.ERROR
781 elif i==2:
782 main.log.info(self.name + ": Git Pull - pulling repository now")
783 self.handle.expect("ONOS\$", 120)
784 return 0
785 elif i==3:
786 main.log.error(self.name + ": Git Pull - TIMEOUT")
787 return main.ERROR
788 elif i==4:
789 main.log.info(self.name + ": Git Pull - Already up to date")
790 return 1
791 elif i==5:
792 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
793 return main.ERROR
794 else:
795 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
796 return main.ERROR
797 except pexpect.EOF:
798 main.log.error(self.name + ": EOF exception found")
799 main.log.error(self.name + ": " + self.handle.before)
800 main.cleanup()
801 main.exit()
802 except:
803 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
804 main.log.error( traceback.print_exc() )
805 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
806 main.cleanup()
807 main.exit()
admine0eeea22014-04-14 10:22:46 -0700808#********************************************************
809
810
admin7373a812014-04-16 09:49:02 -0700811 def git_compile(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700812 '''
813 Compiles ONOS
814 First runs mvn clean then mvn compile
815 '''
816 try:
817 main.log.info(self.name + ": mvn clean")
admin8fc02822014-04-16 13:31:23 -0700818 self.handle.sendline("cd " + self.home)
Jon Hallae05dc22014-04-16 10:56:28 -0700819 self.handle.sendline("mvn clean")
820 while 1:
821 i=self.handle.expect(['BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=30)
admin8fc02822014-04-16 13:31:23 -0700822 print(str(self.before))
Jon Hallae05dc22014-04-16 10:56:28 -0700823 if i == 0:
admin8fc02822014-04-16 13:31:23 -0700824 main.log.error(self.name + ": Clean failure!")
Jon Hallae05dc22014-04-16 10:56:28 -0700825 return main.FALSE
826 elif i == 1:
admin8fc02822014-04-16 13:31:23 -0700827 main.log.info(self.name + ": Clean success!")
Jon Hallae05dc22014-04-16 10:56:28 -0700828 elif i == 2:
admin8fc02822014-04-16 13:31:23 -0700829 main.log.info(self.name + ": Clean complete")
Jon Hallae05dc22014-04-16 10:56:28 -0700830 break;
831 elif i == 3:
832 main.log.error(self.name + ": mvn clean TIMEOUT!")
833 return main.FALSE
834
835 main.log.info(self.name + ": mvn compile")
836 self.handle.sendline("mvn compile")
837 while 1:
838 i=self.handle.expect(['BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=60)
839 if i == 0:
840 main.log.error(self.name + ": Build failure!")
841 return main.FALSE
842 elif i == 1:
843 main.log.info(self.name + ": Build success!")
844 return main.TRUE
845 elif i == 2:
846 main.log.info(self.name + ": Build complete")
847 return main.TRUE
848 elif i == 3:
849 main.log.error(self.name + ": mvn compile TIMEOUT!")
850 return main.FALSE
851 else:
852 pass
853 except pexpect.EOF:
854 main.log.error(self.name + ": EOF exception found")
855 main.log.error(self.name + ": " + self.handle.before)
856 main.cleanup()
857 main.exit()
858 except:
859 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
860 main.log.error( traceback.print_exc() )
861 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
862 main.cleanup()
863 main.exit()
admin4a58db92013-09-30 12:04:54 -0700864
Jon Hallf89c8552014-04-02 13:14:06 -0700865 def tcpdump(self, intf = "eth0"):
Jon Hallae05dc22014-04-16 10:56:28 -0700866 '''
867 Runs tpdump on an intferface and saves in onos-logs under the ONOS home directory
868 intf can be specified, or the default eth0 is used
869 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700870 try:
871 self.handle.sendline("")
872 self.handle.expect("\$")
Jon Hall26f316b2014-04-11 11:21:25 -0700873 self.handle.sendline("sudo tcpdump -n -i "+ intf + " -s0 -w " + self.home +"/onos-logs/tcpdump &")
Jon Hallf89c8552014-04-02 13:14:06 -0700874 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT],timeout=10)
875 if i == 0:
876 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
877 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700878 elif i == 1:
Jon Hallf89c8552014-04-02 13:14:06 -0700879 main.log.info(self.name + ": tcpdump started on " + intf)
880 return main.TRUE
James Leec9cacaf2014-04-08 09:17:39 -0700881 elif i == 2:
Jon Hallf89c8552014-04-02 13:14:06 -0700882 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
883 return main.FALSE
884 else:
885 main.log.error(self.name + ": tcpdump - unexpected response")
886 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700887 except pexpect.EOF:
888 main.log.error(self.name + ": EOF exception found")
889 main.log.error(self.name + ": " + self.handle.before)
890 main.cleanup()
891 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700892 except:
893 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
894 main.log.error( traceback.print_exc() )
895 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
896 main.cleanup()
897 main.exit()
898
admin4a58db92013-09-30 12:04:54 -0700899 def kill_tcpdump(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700900 '''
901 Kills any tcpdump processes running
902 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700903 try:
904 self.handle.sendline("")
905 self.handle.expect("\$")
906 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tcpdump -n\" | grep -v grep | awk '{print $2}'`")
Jon Hallae05dc22014-04-16 10:56:28 -0700907 except pexpect.EOF:
908 main.log.error(self.name + ": EOF exception found")
909 main.log.error(self.name + ": " + self.handle.before)
910 main.cleanup()
911 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700912 except:
913 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
914 main.log.error( traceback.print_exc() )
915 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
916 main.cleanup()
917 main.exit()
918
919 def find_host(self,RestIP,RestPort,RestAPI,hostIP):
920 retcode = 0
921 retswitch = []
922 retport = []
923 retmac = []
924 foundIP = []
925 try:
Jon Hall8060af02014-04-14 14:17:58 -0700926 ##### device rest API is: 'host:8080/wm/onos/ng/switches/json' ###
Jon Hallf89c8552014-04-02 13:14:06 -0700927 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
928
929 try:
930 command = "curl -s %s" % (url)
931 result = os.popen(command).read()
932 parsedResult = json.loads(result)
933 # print parsedResult
934 except:
935 print "REST IF %s has issue" % command
936 parsedResult = ""
937
938 if parsedResult == "":
Jon Hallae05dc22014-04-16 10:56:28 -0700939 return (retcode, "Rest API has an error", retport, retmac)
Jon Hallf89c8552014-04-02 13:14:06 -0700940 else:
941 for switch in enumerate(parsedResult):
942 for port in enumerate(switch[1]['ports']):
943 if ( port[1]['devices'] != [] ):
944 try:
James Leec9cacaf2014-04-08 09:17:39 -0700945 foundIP = port[1]['devices'][0]['ipv4addresses'][0]['ipv4']
Jon Hallf89c8552014-04-02 13:14:06 -0700946 except:
947 print "Error in detecting IP address."
948 if foundIP == hostIP:
949 retswitch.append(switch[1]['dpid'])
950 retport.append(port[1]['desc'])
951 retmac.append(port[1]['devices'][0]['mac'])
952 retcode = retcode +1
953 foundIP =''
954 return(retcode, retswitch, retport, retmac)
Jon Hallae05dc22014-04-16 10:56:28 -0700955 except pexpect.EOF:
956 main.log.error(self.name + ": EOF exception found")
957 main.log.error(self.name + ": " + self.handle.before)
958 main.cleanup()
959 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700960 except:
961 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
962 main.log.error( traceback.print_exc() )
963 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
964 main.cleanup()
965 main.exit()