blob: 542092c691c0b75ab426083382d877f4aeb1b6e4 [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)
James Leec9cacaf2014-04-08 09:17:39 -0700324 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700325 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700326 response = main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700327 return response
328
adminbae64d82013-08-01 10:50:15 -0700329 def get_version(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700330 '''
331 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
332 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700333 try:
334 self.handle.sendline("export TERM=xterm-256color")
335 self.handle.expect("xterm-256color")
James Leec9cacaf2014-04-08 09:17:39 -0700336 self.handle.expect("\$")
adminf939f8b2014-04-03 17:22:56 -0700337 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
Jon Hallf89c8552014-04-02 13:14:06 -0700338 self.handle.expect("cd ..")
339 self.handle.expect("\$")
admine0eeea22014-04-14 10:22:46 -0700340 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
341 main.log.report(response)
342 lines=response.splitlines()
343 for line in lines:
344 print line
345 return lines[2]
Jon Hallae05dc22014-04-16 10:56:28 -0700346 except pexpect.EOF:
347 main.log.error(self.name + ": EOF exception found")
348 main.log.error(self.name + ": " + self.handle.before)
349 main.cleanup()
350 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700351 except:
352 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
353 main.log.error( traceback.print_exc() )
354 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
355 main.cleanup()
356 main.exit()
adminbae64d82013-08-01 10:50:15 -0700357
Jon Hallf89c8552014-04-02 13:14:06 -0700358 def add_flow(self, testONip, user, password, flowDef):
Jon Halld8dc5772014-04-08 16:26:29 -0700359 '''
360 Copies the flowdef file from TestStation -> ONOS machine
361 Then runs ./add_flow.py to add the flows to ONOS
362 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700363 try:
364 main.log.info("Adding Flows...")
365 self.handle.sendline("scp %s@%s:%s /tmp/flowtmp" %(user,testONip,flowDef))
366 i=self.handle.expect(['[pP]assword:', '100%', pexpect.TIMEOUT],30)
367 if(i==0):
368 self.handle.sendline("%s" %(password))
369 self.handle.sendline("")
370 self.handle.expect("100%")
371 self.handle.expect("\$", 30)
372 self.handle.sendline(self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
373 self.handle.expect("\$", 1000)
374 main.log.info("Flows added")
375 return main.TRUE
376
377 elif(i==1):
378 self.handle.sendline("")
379 self.handle.expect("\$", 10)
380 self.handle.sendline( self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
381 self.handle.expect("\$", 1000)
382 main.log.info("Flows added")
383 return main.TRUE
384
385 elif(i==2):
386 main.log.error("Flow Def file SCP Timed out...")
387 return main.FALSE
388
389 else:
390 main.log.error("Failed to add flows...")
James Leec9cacaf2014-04-08 09:17:39 -0700391 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700392 except pexpect.EOF:
393 main.log.error(self.name + ": EOF exception found")
394 main.log.error(self.name + ": " + self.handle.before)
395 main.cleanup()
396 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700397 except:
398 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
399 main.log.error( traceback.print_exc() )
400 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
401 main.cleanup()
402 main.exit()
403
adminbae64d82013-08-01 10:50:15 -0700404
405 def delete_flow(self, *delParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700406 '''
407 Deletes a specific flow, a range of flows, or all flows.
408 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700409 try:
410 if len(delParams)==1:
411 if str(delParams[0])=="all":
412 main.log.info(self.name + ": Deleting ALL flows...")
413 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh all",prompt="done",timeout=150)
414 self.handle.sendline(self.home + "/web/delete_flow.py all")
415 self.handle.expect("delete_flow")
416 self.handle.expect("\$",1000)
417 main.log.info(self.name + ": Flows deleted")
418 else:
419 main.log.info(self.name + ": Deleting flow "+str(delParams[0])+"...")
420 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0]),prompt="done",timeout=150)
421 #self.execute(cmd="\n",prompt="\$",timeout=60)
422 self.handle.sendline(self.home +"/web/delete_flow.py %d" % int(delParams[0]))
423 self.handle.expect("delete_flow")
424 self.handle.expect("\$",60)
425 main.log.info(self.name + ": Flow deleted")
426 elif len(delParams)==2:
427 main.log.info(self.name + ": Deleting flows "+str(delParams[0])+" through "+str(delParams[1])+"...")
428 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0])+" "+str(delParams[1]),prompt="done",timeout=150)
429 #self.execute(cmd="\n",prompt="\$",timeout=60)
430 self.handle.sendline(self.home + "/web/delete_flow.py %d %d" % (int(delParams[0]), int(delParams[1])))
431 self.handle.expect("delete_flow")
432 self.handle.expect("\$",600)
433 main.log.info(self.name + ": Flows deleted")
Jon Hallae05dc22014-04-16 10:56:28 -0700434 except pexpect.EOF:
435 main.log.error(self.name + ": EOF exception found")
436 main.log.error(self.name + ": " + self.handle.before)
437 main.cleanup()
438 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700439 except:
440 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
441 main.log.error( traceback.print_exc() )
442 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
443 main.cleanup()
444 main.exit()
adminbae64d82013-08-01 10:50:15 -0700445
446 def check_flow(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700447 '''
448 Calls the ./get_flow.py all and checks:
449 - If each FlowPath has at least one FlowEntry
450 - That there are no "NOT"s found
451 returns TRUE/FALSE
452 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700453 try:
454 flowEntryDetect = 1
455 count = 0
456 self.handle.sendline("clear")
457 time.sleep(1)
458 self.handle.sendline(self.home + "/web/get_flow.py all")
459 self.handle.expect("get_flow")
460 while 1:
461 i=self.handle.expect(['FlowPath','FlowEntry','NOT','\$',pexpect.TIMEOUT],timeout=180)
462 if i==0:
463 count = count + 1
464 if flowEntryDetect == 0:
465 main.log.info(self.name + ": FlowPath without FlowEntry")
466 return main.FALSE
467 else:
468 flowEntryDetect = 0
469 elif i==1:
470 flowEntryDetect = 1
471 elif i==2:
472 main.log.error(self.name + ": Found a NOT")
adminbae64d82013-08-01 10:50:15 -0700473 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700474 elif i==3:
475 if count == 0:
476 main.log.info(self.name + ": There don't seem to be any flows here...")
477 return main.FALSE
478 else:
479 main.log.info(self.name + ": All flows pass")
480 main.log.info(self.name + ": Number of FlowPaths: "+str(count))
481 return main.TRUE
482 elif i==4:
James Leec9cacaf2014-04-08 09:17:39 -0700483 main.log.error(self.name + ":Check_flow() - Command Timeout!")
Jon Hallf89c8552014-04-02 13:14:06 -0700484 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700485 except pexpect.EOF:
486 main.log.error(self.name + ": EOF exception found")
487 main.log.error(self.name + ": " + self.handle.before)
488 main.cleanup()
489 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700490 except:
491 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
492 main.log.error( traceback.print_exc() )
493 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
494 main.cleanup()
495 main.exit()
adminbae64d82013-08-01 10:50:15 -0700496
497 def get_flow(self, *flowParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700498 '''
499 Returns verbose output of ./get_flow.py
500 '''
501 try:
502 if len(flowParams)==1:
503 if str(flowParams[0])=="all":
504 self.execute(cmd="\n",prompt="\$",timeout=60)
505 main.log.info(self.name + ": Getting all flow data...")
506 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh all",prompt="done",timeout=150)
507 self.execute(cmd="\n",prompt="\$",timeout=60)
508 return data
509 else:
510 main.log.info(self.name + ": Retrieving flow "+str(flowParams[0])+" data...")
511 data = self.execute(cmd=self.home +"/scripts/TestON_get_flow.sh "+str(flowParams[0]),prompt="done",timeout=150)
512 self.execute(cmd="\n",prompt="\$",timeout=60)
513 return data
514 elif len(flowParams)==5:
515 main.log.info(self.name + ": Retrieving flow installer data...")
516 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)
517 self.execute(cmd="\n",prompt="\$",timeout=60)
518 return data
519 elif len(flowParams)==4:
520 main.log.info(self.name + ": Retrieving flow endpoints...")
521 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)
522 self.execute(cmd="\n",prompt="\$",timeout=60)
523 return data
Jon Hallae05dc22014-04-16 10:56:28 -0700524 except pexpect.EOF:
525 main.log.error(self.name + ": EOF exception found")
526 main.log.error(self.name + ": " + self.handle.before)
527 main.cleanup()
528 main.exit()
Jon Halld8dc5772014-04-08 16:26:29 -0700529 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700530 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
531 main.log.error( traceback.print_exc() )
532 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
533 main.cleanup()
534 main.exit()
adminbae64d82013-08-01 10:50:15 -0700535
536
Jon Hall8060af02014-04-14 14:17:58 -0700537# http://localhost:8080/wm/onos/ng/switches/json
538# http://localhost:8080/wm/onos/ng/links/json
539# http://localhost:8080/wm/onos/registry/controllers/json
540# http://localhost:8080/wm/onos/registry/switches/json"
adminbae64d82013-08-01 10:50:15 -0700541
542 def get_json(self, url):
Jon Halld8dc5772014-04-08 16:26:29 -0700543 '''
544 Helper functions used to parse the json output of a rest call
545 '''
adminbae64d82013-08-01 10:50:15 -0700546 try:
Jon Hallf89c8552014-04-02 13:14:06 -0700547 try:
548 command = "curl -s %s" % (url)
549 result = os.popen(command).read()
550 parsedResult = json.loads(result)
551 except:
552 print "REST IF %s has issue" % command
553 parsedResult = ""
554
555 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
556 print "REST %s returned code %s" % (command, parsedResult['code'])
557 parsedResult = ""
James Leec9cacaf2014-04-08 09:17:39 -0700558 return parsedResult
Jon Hallae05dc22014-04-16 10:56:28 -0700559 except pexpect.EOF:
560 main.log.error(self.name + ": EOF exception found")
561 main.log.error(self.name + ": " + self.handle.before)
562 main.cleanup()
563 main.exit()
adminbae64d82013-08-01 10:50:15 -0700564 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700565 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
566 main.log.error( traceback.print_exc() )
567 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
568 main.cleanup()
569 main.exit()
adminbae64d82013-08-01 10:50:15 -0700570
Jon Hallf89c8552014-04-02 13:14:06 -0700571 def check_switch(self,RestIP,correct_nr_switch, RestPort ="8080" ):
Jon Halld8dc5772014-04-08 16:26:29 -0700572 '''
573 Used by check_status
574 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700575 try:
576 buf = ""
577 retcode = 0
admine0eeea22014-04-14 10:22:46 -0700578 url="http://%s:%s/wm/onos/ng/switches/json" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700579 parsedResult = self.get_json(url)
580 if parsedResult == "":
581 retcode = 1
582 return (retcode, "Rest API has an issue")
583 url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
584 registry = self.get_json(url)
585
586 if registry == "":
587 retcode = 1
588 return (retcode, "Rest API has an issue")
589
590 cnt = 0
591 active = 0
adminbae64d82013-08-01 10:50:15 -0700592
Jon Hallf89c8552014-04-02 13:14:06 -0700593 for s in parsedResult:
594 cnt += 1
James Leec9cacaf2014-04-08 09:17:39 -0700595 if s['state'] == "ACTIVE":
Jon Hallf89c8552014-04-02 13:14:06 -0700596 active += 1
adminbae64d82013-08-01 10:50:15 -0700597
Jon Hallf89c8552014-04-02 13:14:06 -0700598 buf += "switch: network %d : %d switches %d active\n" % (0+1, cnt, active)
599 if correct_nr_switch != cnt:
600 buf += "switch fail: network %d should have %d switches but has %d\n" % (1, correct_nr_switch, cnt)
601 retcode = 1
adminbae64d82013-08-01 10:50:15 -0700602
Jon Hallf89c8552014-04-02 13:14:06 -0700603 if correct_nr_switch != active:
604 buf += "switch fail: network %d should have %d active switches but has %d\n" % (1, correct_nr_switch, active)
605 retcode = 1
606
607 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700608 except pexpect.EOF:
609 main.log.error(self.name + ": EOF exception found")
610 main.log.error(self.name + ": " + self.handle.before)
611 main.cleanup()
612 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700613 except:
614 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
615 main.log.error( traceback.print_exc() )
616 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
617 main.cleanup()
618 main.exit()
adminbae64d82013-08-01 10:50:15 -0700619
Jon Hallf89c8552014-04-02 13:14:06 -0700620 def check_link(self,RestIP, nr_links, RestPort = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700621 '''
622 Used by check_status
623 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700624 try:
625 buf = ""
626 retcode = 0
627
admine0eeea22014-04-14 10:22:46 -0700628 url = "http://%s:%s/wm/onos/ng/links/json" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700629 parsedResult = self.get_json(url)
630
631 if parsedResult == "":
632 retcode = 1
633 return (retcode, "Rest API has an issue")
634
635 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
636 intra = 0
637 interlink=0
638
639 for s in parsedResult:
James Leec9cacaf2014-04-08 09:17:39 -0700640 intra = intra + 1
Jon Hallf89c8552014-04-02 13:14:06 -0700641
642 if intra != nr_links:
643 buf += "link fail\n"
644 retcode = 1
645
646 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700647 except pexpect.EOF:
648 main.log.error(self.name + ": EOF exception found")
649 main.log.error(self.name + ": " + self.handle.before)
650 main.cleanup()
651 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700652 except:
653 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
654 main.log.error( traceback.print_exc() )
655 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
656 main.cleanup()
657 main.exit()
adminbae64d82013-08-01 10:50:15 -0700658
Jon Hallf89c8552014-04-02 13:14:06 -0700659 def check_status_report(self, ip, numoswitch, numolink, port="8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700660 '''
661 Checks the number of swithes & links that ONOS sees against the supplied values.
662 Writes to the report log.
663 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700664 try:
James Leec9cacaf2014-04-08 09:17:39 -0700665 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700666 switch = self.check_switch(ip, int(numoswitch), port)
667 link = self.check_link(ip, int(numolink), port)
668 value = switch[0]
669 value += link[0]
670 main.log.report( self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
671 if value != 0:
672 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700673 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700674 # "PASS"
675 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700676 except pexpect.EOF:
677 main.log.error(self.name + ": EOF exception found")
678 main.log.error(self.name + ": " + self.handle.before)
679 main.cleanup()
680 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700681 except:
682 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
683 main.log.error( traceback.print_exc() )
684 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
685 main.cleanup()
686 main.exit()
adminbae64d82013-08-01 10:50:15 -0700687
Jon Hallf89c8552014-04-02 13:14:06 -0700688 def check_status(self, ip, numoswitch, numolink, port = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700689 '''
690 Checks the number of swithes & links that ONOS sees against the supplied values.
691 Writes to the main log.
692 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700693 try:
James Leec9cacaf2014-04-08 09:17:39 -0700694 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700695 switch = self.check_switch(ip, int(numoswitch), port)
696 link = self.check_link(ip, int(numolink), port)
697 value = switch[0]
698 value += link[0]
699 main.log.info(self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
700 if value != 0:
701 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700702 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700703 # "PASS"
704 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700705 except pexpect.EOF:
706 main.log.error(self.name + ": EOF exception found")
707 main.log.error(self.name + ": " + self.handle.before)
708 main.cleanup()
709 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700710 except:
711 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
712 main.log.error( traceback.print_exc() )
713 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
714 main.cleanup()
715 main.exit()
adminbae64d82013-08-01 10:50:15 -0700716
adminbae64d82013-08-01 10:50:15 -0700717 def check_for_no_exceptions(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700718 '''
719 TODO: Rewrite
720 Used by CassndraCheck.py to scan ONOS logs for Exceptions
721 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700722 try:
723 self.handle.sendline("dsh 'grep Exception ~/ONOS/onos-logs/onos.*.log'")
James Leec9cacaf2014-04-08 09:17:39 -0700724 self.handle.expect("\$ dsh")
Jon Hallf89c8552014-04-02 13:14:06 -0700725 self.handle.expect("\$")
726 output = self.handle.before
James Leec9cacaf2014-04-08 09:17:39 -0700727 main.log.info(self.name + ": " + output )
Jon Hallf89c8552014-04-02 13:14:06 -0700728 if re.search("Exception",output):
729 return main.FALSE
730 else :
731 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700732 except pexpect.EOF:
733 main.log.error(self.name + ": EOF exception found")
734 main.log.error(self.name + ": " + self.handle.before)
735 main.cleanup()
736 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700737 except:
738 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
739 main.log.error( traceback.print_exc() )
740 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
741 main.cleanup()
742 main.exit()
743
744
admine0eeea22014-04-14 10:22:46 -0700745 def git_pull(self, comp1=""):
Jon Halld8dc5772014-04-08 16:26:29 -0700746 '''
Jon Halld8dc5772014-04-08 16:26:29 -0700747 Assumes that "git pull" works without login
admine0eeea22014-04-14 10:22:46 -0700748
749 This function will perform a git pull on the ONOS instance.
750 If used as git_pull("NODE") it will do git pull + NODE. This is
751 for the purpose of pulling from other nodes if necessary.
752
753 Otherwise, this function will perform a git pull in the
754 ONOS repository. If it has any problems, it will return main.ERROR
755 If it successfully does a git_pull, it will return a 1.
756 If it has no updates, it will return a 0.
757
Jon Halld8dc5772014-04-08 16:26:29 -0700758 '''
Jon Hallae05dc22014-04-16 10:56:28 -0700759 try:
760 main.log.info(self.name + ": Stopping ONOS")
761 self.stop()
762 self.handle.sendline("cd " + self.home)
763 self.handle.expect("ONOS\$")
764 if comp1=="":
765 self.handle.sendline("git pull")
766 else:
767 self.handle.sendline("git pull " + comp1)
768
769 uptodate = 0
SeanCorcoran20bd6fc2014-04-16 17:10:30 -0700770 i=self.handle.expect(['fatal','Username\sfor\s(.*):\s','Unpacking\sobjects',pexpect.TIMEOUT,'Already up-to-date','Aborting'],timeout=1800)
Jon Hallae05dc22014-04-16 10:56:28 -0700771 #debug
772 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
773 if i==0:
774 main.log.error(self.name + ": Git pull had some issue...")
775 return main.ERROR
776 elif i==1:
777 main.log.error(self.name + ": Git Pull Asking for username!!! BADD!")
778 return main.ERROR
779 elif i==2:
780 main.log.info(self.name + ": Git Pull - pulling repository now")
781 self.handle.expect("ONOS\$", 120)
782 return 0
783 elif i==3:
784 main.log.error(self.name + ": Git Pull - TIMEOUT")
785 return main.ERROR
786 elif i==4:
787 main.log.info(self.name + ": Git Pull - Already up to date")
788 return 1
789 elif i==5:
790 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
791 return main.ERROR
792 else:
793 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
794 return main.ERROR
795 except pexpect.EOF:
796 main.log.error(self.name + ": EOF exception found")
797 main.log.error(self.name + ": " + self.handle.before)
798 main.cleanup()
799 main.exit()
800 except:
801 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
802 main.log.error( traceback.print_exc() )
803 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
804 main.cleanup()
805 main.exit()
admine0eeea22014-04-14 10:22:46 -0700806#********************************************************
807
808
admin7373a812014-04-16 09:49:02 -0700809 def git_compile(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700810 '''
811 Compiles ONOS
812 First runs mvn clean then mvn compile
813 '''
814 try:
815 main.log.info(self.name + ": mvn clean")
admin8fc02822014-04-16 13:31:23 -0700816 self.handle.sendline("cd " + self.home)
Jon Hallae05dc22014-04-16 10:56:28 -0700817 self.handle.sendline("mvn clean")
818 while 1:
819 i=self.handle.expect(['BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=30)
admin8fc02822014-04-16 13:31:23 -0700820 print(str(self.before))
Jon Hallae05dc22014-04-16 10:56:28 -0700821 if i == 0:
admin8fc02822014-04-16 13:31:23 -0700822 main.log.error(self.name + ": Clean failure!")
Jon Hallae05dc22014-04-16 10:56:28 -0700823 return main.FALSE
824 elif i == 1:
admin8fc02822014-04-16 13:31:23 -0700825 main.log.info(self.name + ": Clean success!")
Jon Hallae05dc22014-04-16 10:56:28 -0700826 elif i == 2:
admin8fc02822014-04-16 13:31:23 -0700827 main.log.info(self.name + ": Clean complete")
Jon Hallae05dc22014-04-16 10:56:28 -0700828 break;
829 elif i == 3:
830 main.log.error(self.name + ": mvn clean TIMEOUT!")
831 return main.FALSE
832
833 main.log.info(self.name + ": mvn compile")
834 self.handle.sendline("mvn compile")
835 while 1:
836 i=self.handle.expect(['BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=60)
837 if i == 0:
838 main.log.error(self.name + ": Build failure!")
839 return main.FALSE
840 elif i == 1:
841 main.log.info(self.name + ": Build success!")
842 return main.TRUE
843 elif i == 2:
844 main.log.info(self.name + ": Build complete")
845 return main.TRUE
846 elif i == 3:
847 main.log.error(self.name + ": mvn compile TIMEOUT!")
848 return main.FALSE
849 else:
850 pass
851 except pexpect.EOF:
852 main.log.error(self.name + ": EOF exception found")
853 main.log.error(self.name + ": " + self.handle.before)
854 main.cleanup()
855 main.exit()
856 except:
857 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
858 main.log.error( traceback.print_exc() )
859 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
860 main.cleanup()
861 main.exit()
admin4a58db92013-09-30 12:04:54 -0700862
Jon Hallf89c8552014-04-02 13:14:06 -0700863 def tcpdump(self, intf = "eth0"):
Jon Hallae05dc22014-04-16 10:56:28 -0700864 '''
865 Runs tpdump on an intferface and saves in onos-logs under the ONOS home directory
866 intf can be specified, or the default eth0 is used
867 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700868 try:
869 self.handle.sendline("")
870 self.handle.expect("\$")
Jon Hall26f316b2014-04-11 11:21:25 -0700871 self.handle.sendline("sudo tcpdump -n -i "+ intf + " -s0 -w " + self.home +"/onos-logs/tcpdump &")
Jon Hallf89c8552014-04-02 13:14:06 -0700872 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT],timeout=10)
873 if i == 0:
874 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
875 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700876 elif i == 1:
Jon Hallf89c8552014-04-02 13:14:06 -0700877 main.log.info(self.name + ": tcpdump started on " + intf)
878 return main.TRUE
James Leec9cacaf2014-04-08 09:17:39 -0700879 elif i == 2:
Jon Hallf89c8552014-04-02 13:14:06 -0700880 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
881 return main.FALSE
882 else:
883 main.log.error(self.name + ": tcpdump - unexpected response")
884 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700885 except pexpect.EOF:
886 main.log.error(self.name + ": EOF exception found")
887 main.log.error(self.name + ": " + self.handle.before)
888 main.cleanup()
889 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700890 except:
891 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
892 main.log.error( traceback.print_exc() )
893 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
894 main.cleanup()
895 main.exit()
896
admin4a58db92013-09-30 12:04:54 -0700897 def kill_tcpdump(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700898 '''
899 Kills any tcpdump processes running
900 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700901 try:
902 self.handle.sendline("")
903 self.handle.expect("\$")
904 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tcpdump -n\" | grep -v grep | awk '{print $2}'`")
Jon Hallae05dc22014-04-16 10:56:28 -0700905 except pexpect.EOF:
906 main.log.error(self.name + ": EOF exception found")
907 main.log.error(self.name + ": " + self.handle.before)
908 main.cleanup()
909 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700910 except:
911 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
912 main.log.error( traceback.print_exc() )
913 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
914 main.cleanup()
915 main.exit()
916
917 def find_host(self,RestIP,RestPort,RestAPI,hostIP):
918 retcode = 0
919 retswitch = []
920 retport = []
921 retmac = []
922 foundIP = []
923 try:
Jon Hall8060af02014-04-14 14:17:58 -0700924 ##### device rest API is: 'host:8080/wm/onos/ng/switches/json' ###
Jon Hallf89c8552014-04-02 13:14:06 -0700925 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
926
927 try:
928 command = "curl -s %s" % (url)
929 result = os.popen(command).read()
930 parsedResult = json.loads(result)
931 # print parsedResult
932 except:
933 print "REST IF %s has issue" % command
934 parsedResult = ""
935
936 if parsedResult == "":
Jon Hallae05dc22014-04-16 10:56:28 -0700937 return (retcode, "Rest API has an error", retport, retmac)
Jon Hallf89c8552014-04-02 13:14:06 -0700938 else:
939 for switch in enumerate(parsedResult):
940 for port in enumerate(switch[1]['ports']):
941 if ( port[1]['devices'] != [] ):
942 try:
James Leec9cacaf2014-04-08 09:17:39 -0700943 foundIP = port[1]['devices'][0]['ipv4addresses'][0]['ipv4']
Jon Hallf89c8552014-04-02 13:14:06 -0700944 except:
945 print "Error in detecting IP address."
946 if foundIP == hostIP:
947 retswitch.append(switch[1]['dpid'])
948 retport.append(port[1]['desc'])
949 retmac.append(port[1]['devices'][0]['mac'])
950 retcode = retcode +1
951 foundIP =''
952 return(retcode, retswitch, retport, retmac)
Jon Hallae05dc22014-04-16 10:56:28 -0700953 except pexpect.EOF:
954 main.log.error(self.name + ": EOF exception found")
955 main.log.error(self.name + ": " + self.handle.before)
956 main.cleanup()
957 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700958 except:
959 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
960 main.log.error( traceback.print_exc() )
961 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
962 main.cleanup()
963 main.exit()