blob: b3186b0937d4be7983577f84e75970723c300057 [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
admin2a9548d2014-06-17 14:08:07 -070028import urllib2
29from urllib2 import URLError, HTTPError
adminbae64d82013-08-01 10:50:15 -070030sys.path.append("../")
31from drivers.common.clidriver import CLI
32
33class OnosCliDriver(CLI):
34
35 def __init__(self):
36 super(CLI, self).__init__()
37
38 def connect(self,**connectargs):
Jon Halld8dc5772014-04-08 16:26:29 -070039 '''
40 Creates ssh handle for ONOS.
41 '''
Jon Hallf89c8552014-04-02 13:14:06 -070042 try:
43 for key in connectargs:
admine0eeea22014-04-14 10:22:46 -070044 vars(self)[key] = connectargs[key]
Jon Hallf89c8552014-04-02 13:14:06 -070045 self.home = "~/ONOS"
46 for key in self.options:
admine0eeea22014-04-14 10:22:46 -070047 if key == "home":
48 self.home = self.options['home']
49 break
adminbae64d82013-08-01 10:50:15 -070050
Jon Hallf89c8552014-04-02 13:14:06 -070051
52 self.name = self.options['name']
53 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 -070054
Jon Hallf89c8552014-04-02 13:14:06 -070055 if self.handle:
Jon Hallf89c8552014-04-02 13:14:06 -070056 return self.handle
57 else :
58 main.log.info("NO ONOS HANDLE")
59 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -070060 except pexpect.EOF:
61 main.log.error(self.name + ": EOF exception found")
62 main.log.error(self.name + ": " + self.handle.before)
63 main.cleanup()
64 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -070065 except:
66 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
67 main.log.error( traceback.print_exc() )
68 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
69 main.cleanup()
70 main.exit()
adminbae64d82013-08-01 10:50:15 -070071
72 def start(self):
Jon Halld8dc5772014-04-08 16:26:29 -070073 '''
74 Starts ONOS on remote machine.
75 Returns false if any errors were encountered.
76 '''
James Leec9cacaf2014-04-08 09:17:39 -070077 try:
Jon Hallf89c8552014-04-02 13:14:06 -070078 self.handle.sendline("")
Jon Hallae05dc22014-04-16 10:56:28 -070079 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
Jon Hall4a2b0482014-04-18 16:29:26 -070080 self.handle.sendline("cd "+self.home)
81 self.handle.sendline("./onos.sh core start")
Jon Hallae05dc22014-04-16 10:56:28 -070082 i=self.handle.expect(["STARTED","FAILED",pexpect.EOF,pexpect.TIMEOUT])
83 response = self.handle.before + str(self.handle.after)
Jon Hallf89c8552014-04-02 13:14:06 -070084 if i==0:
Jon Hallae05dc22014-04-16 10:56:28 -070085 j = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], timeout=60)
86 if re.search("Killed",response):
87 main.log.warn(self.name + ": Killed existing process")
88 if j==0:
Jon Hallf89c8552014-04-02 13:14:06 -070089 main.log.info(self.name + ": ONOS Started ")
Jon Hallae05dc22014-04-16 10:56:28 -070090 return main.TRUE
91 elif j==1:
92 main.log.error(self.name + ": EOF exception found")
93 main.log.error(self.name + ": " + self.handle.before)
94 main.cleanup()
95 main.exit()
96 elif j==2:
Jon Hallf89c8552014-04-02 13:14:06 -070097 main.log.info(self.name + ": ONOS NOT Started, stuck while waiting for it to start ")
98 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -070099 else:
100 main.log.warn(self.name +": Unexpected response in start")
101 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700102 elif i==1:
Jon Hallae05dc22014-04-16 10:56:28 -0700103 main.log.error(self.name + ": ONOS Failed to start")
adminbae64d82013-08-01 10:50:15 -0700104 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700105 elif i==2:
106 main.log.error(self.name + ": EOF exception found")
107 main.log.error(self.name + ": " + self.handle.before)
108 main.cleanup()
109 main.exit()
110 elif i==3:
111 main.log.error(self.name + ": ONOS timedout while starting")
112 return main.FALSE
113 else:
114 main.log.error(self.name + ": ONOS start expect script missed something... ")
adminbae64d82013-08-01 10:50:15 -0700115 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700116 except pexpect.EOF:
117 main.log.error(self.name + ": EOF exception found")
118 main.log.error(self.name + ": " + self.handle.before)
119 main.cleanup()
120 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700121 except:
122 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
123 main.log.error( traceback.print_exc() )
124 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
125 main.cleanup()
126 main.exit()
Jon Halle80ef8c2014-04-29 15:29:13 -0700127
128 def start_all(self):
129 '''
130 starts ZK, RC, and ONOS
131 '''
132 self.handle.sendline("cd "+self.home)
133 self.handle.sendline("./onos.sh start")
134 self.handle.expect("./onos.sh start")
135 self.handle.expect(["\$",pexpect.TIMEOUT])
136
137
138
adminbae64d82013-08-01 10:50:15 -0700139 def start_rest(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700140 '''
141 Starts the rest server on ONOS.
142 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700143 try:
Jon Hall4a2b0482014-04-18 16:29:26 -0700144 self.handle.sendline("cd "+self.home)
145 response = self.execute(cmd= "./start-rest.sh start",prompt="\$",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700146 if re.search("admin",response):
147 main.log.info(self.name + ": Rest Server Started Successfully")
148 time.sleep(5)
149 return main.TRUE
150 else :
James Leec9cacaf2014-04-08 09:17:39 -0700151 main.log.warn(self.name + ": Failed to start Rest Server")
152 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700153 except pexpect.EOF:
154 main.log.error(self.name + ": EOF exception found")
155 main.log.error(self.name + ": " + self.handle.before)
156 main.cleanup()
157 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700158 except:
159 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
160 main.log.error( traceback.print_exc() )
161 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
162 main.cleanup()
163 main.exit()
164
adminbae64d82013-08-01 10:50:15 -0700165 def status(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700166 '''
167 Called onos.sh core status and returns TRUE/FALSE accordingly
168 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700169 try:
170 self.execute(cmd="\n",prompt="\$",timeout=10)
Jon Hall4a2b0482014-04-18 16:29:26 -0700171 self.handle.sendline("cd "+self.home)
172 response = self.execute(cmd="./onos.sh core status ",prompt="\d+\sinstance\sof\sonos\srunning",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700173 self.execute(cmd="\n",prompt="\$",timeout=10)
174 if re.search("1\sinstance\sof\sonos\srunning",response):
175 return main.TRUE
176 elif re.search("0\sinstance\sof\sonos\srunning",response):
177 return main.FALSE
178 else :
179 main.log.info( self.name + " WARNING: status recieved unknown response")
180 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700181 except pexpect.EOF:
182 main.log.error(self.name + ": EOF exception found")
183 main.log.error(self.name + ": " + self.handle.before)
184 main.cleanup()
185 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700186 except:
187 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
188 main.log.error( traceback.print_exc() )
189 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
190 main.cleanup()
191 main.exit()
192
adminbae64d82013-08-01 10:50:15 -0700193
194 def isup(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700195 '''
196 A more complete check to see if ONOS is up and running properly.
197 First, it checks if the process is up.
198 Second, it reads the logs for "Exception: Connection refused"
199 Third, it makes sure the logs are actually moving.
200 returns TRUE/FALSE accordingly.
201 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700202 try:
203 self.execute(cmd="\n",prompt="\$",timeout=10)
Jon Hall4a2b0482014-04-18 16:29:26 -0700204 self.handle.sendline("cd "+self.home)
205 response = self.execute(cmd= "./onos.sh core status ",prompt="running",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700206 self.execute(cmd="\n",prompt="\$",timeout=10)
207 tail1 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
admine8c47d02014-06-03 11:59:16 -0700208 time.sleep(10)
Jon Hallf89c8552014-04-02 13:14:06 -0700209 self.execute(cmd="\n",prompt="\$",timeout=10)
210 tail2 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
211 pattern = '(.*)1 instance(.*)'
212 pattern2 = '(.*)Exception: Connection refused(.*)'
213 # if utilities.assert_matches(expect=pattern,actual=response,onpass="ONOS process is running...",onfail="ONOS process not running..."):
admin2a9548d2014-06-17 14:08:07 -0700214 running = self.execute(cmd="cat "+self.home+"/onos-logs/onos.*.log | grep 'Sending LLDP out on all ports'",prompt="\$",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700215 if re.search(pattern, response):
admin2a9548d2014-06-17 14:08:07 -0700216 if running != "":
Jon Halle80ef8c2014-04-29 15:29:13 -0700217 main.log.info(self.name + ": ONOS process is running...")
218 if tail1 == tail2:
219 main.log.error(self.name + ": ONOS is frozen...")#logs aren't moving
220 return main.FALSE
221 elif re.search( pattern2,tail1 ):
222 main.log.info(self.name + ": Connection Refused found in onos log")
223 return main.FALSE
224 elif re.search( pattern2,tail2 ):
225 main.log.info(self.name + ": Connection Refused found in onos log")
226 return main.FALSE
227 else:
228 main.log.info(self.name + ": Onos log is moving! It's looking good!")
229 return main.TRUE
Jon Hall55c79662014-05-19 15:03:40 -0700230 else:
231 main.log.info(self.name + ": ONOS not yet sending out LLDP messages")
232 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700233 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700234 main.log.error(self.name + ": ONOS process not running...")
235 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700236 except pexpect.EOF:
237 main.log.error(self.name + ": EOF exception found")
238 main.log.error(self.name + ": " + self.handle.before)
239 main.cleanup()
240 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700241 except:
242 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
243 main.log.error( traceback.print_exc() )
244 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
245 main.cleanup()
246 main.exit()
adminbae64d82013-08-01 10:50:15 -0700247
Jon Hallf89c8552014-04-02 13:14:06 -0700248
249
James Leec9cacaf2014-04-08 09:17:39 -0700250 def rest_status(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700251 '''
252 Checks if the rest server is running.
253 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700254 try:
255 response = self.execute(cmd= self.home + "/start-rest.sh status ",prompt="running",timeout=10)
256 if re.search("rest\sserver\sis\srunning",response):
257 main.log.info(self.name + ": Rest Server is running")
Jon Hallae05dc22014-04-16 10:56:28 -0700258 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700259 elif re.search("rest\sserver\sis\snot\srunning",response):
260 main.log.warn(self.name + ": Rest Server is not Running")
Jon Hallae05dc22014-04-16 10:56:28 -0700261 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700262 else :
263 main.log.error(self.name + ": No response" +response)
Jon Hallae05dc22014-04-16 10:56:28 -0700264 return main.FALSE
265 except pexpect.EOF:
266 main.log.error(self.name + ": EOF exception found")
267 main.log.error(self.name + ": " + self.handle.before)
268 main.cleanup()
269 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700270 except:
271 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
272 main.log.error( traceback.print_exc() )
273 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
274 main.cleanup()
275 main.exit()
admine8c47d02014-06-03 11:59:16 -0700276
277 def stop_all(self):
278 '''
279 Runs ./onos.sh stop
280 '''
281 try:
282 self.handle.sendline("")
283 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
284 self.handle.sendline("cd "+self.home)
285 self.handle.sendline("./onos.sh stop")
286 i=self.handle.expect(["Stop",pexpect.EOF,pexpect.TIMEOUT])
287 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], 60)
288 result = self.handle.before
289 if re.search("Killed", result):
290 main.log.info(self.name + ": ONOS Killed Successfully")
291 return main.TRUE
292 else :
293 main.log.warn(self.name + ": ONOS wasn't running")
294 return main.FALSE
295 except pexpect.EOF:
296 main.log.error(self.name + ": EOF exception found")
297 main.log.error(self.name + ": " + self.handle.before)
298 main.cleanup()
299 main.exit()
300 except:
301 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
302 main.log.error( traceback.print_exc() )
303 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
304 main.cleanup()
305 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700306
307
adminbae64d82013-08-01 10:50:15 -0700308 def stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700309 '''
310 Runs ./onos.sh core stop to stop ONOS
311 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700312 try:
313 self.handle.sendline("")
Jon Hallae05dc22014-04-16 10:56:28 -0700314 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
Jon Hall4a2b0482014-04-18 16:29:26 -0700315 self.handle.sendline("cd "+self.home)
316 self.handle.sendline("./onos.sh core stop")
Jon Hallae05dc22014-04-16 10:56:28 -0700317 i=self.handle.expect(["Stop",pexpect.EOF,pexpect.TIMEOUT])
318 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], 60)
James Leec9cacaf2014-04-08 09:17:39 -0700319 result = self.handle.before
Jon Hallf89c8552014-04-02 13:14:06 -0700320 if re.search("Killed", result):
321 main.log.info(self.name + ": ONOS Killed Successfully")
322 return main.TRUE
323 else :
324 main.log.warn(self.name + ": ONOS wasn't running")
325 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700326 except pexpect.EOF:
327 main.log.error(self.name + ": EOF exception found")
328 main.log.error(self.name + ": " + self.handle.before)
329 main.cleanup()
330 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700331 except:
332 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
333 main.log.error( traceback.print_exc() )
334 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
335 main.cleanup()
336 main.exit()
337
adminbae64d82013-08-01 10:50:15 -0700338
339 def rest_stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700340 '''
341 Runs ./start-rest.sh stop to stop ONOS rest server
342 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700343 try:
344 response = self.execute(cmd= self.home + "/start-rest.sh stop ",prompt="killing",timeout=10)
345 self.execute(cmd="\n",prompt="\$",timeout=10)
346 if re.search("killing", response):
347 main.log.info(self.name + ": Rest Server Stopped")
348 return main.TRUE
349 else :
350 main.log.error(self.name + ": Failed to Stop, Rest Server is not Running")
351 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700352 except pexpect.EOF:
353 main.log.error(self.name + ": EOF exception found")
354 main.log.error(self.name + ": " + self.handle.before)
355 main.cleanup()
356 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700357 except:
358 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
359 main.log.error( traceback.print_exc() )
360 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
361 main.cleanup()
362 main.exit()
363
364
adminbae64d82013-08-01 10:50:15 -0700365 def disconnect(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700366 '''
367 Called when Test is complete to disconnect the ONOS handle.
368 '''
adminaeedddd2013-08-02 15:14:15 -0700369 response = ''
370 try:
adminbae64d82013-08-01 10:50:15 -0700371 self.handle.sendline("exit")
adminaeedddd2013-08-02 15:14:15 -0700372 self.handle.expect("closed")
Jon Hallae05dc22014-04-16 10:56:28 -0700373 except pexpect.EOF:
374 main.log.error(self.name + ": EOF exception found")
375 main.log.error(self.name + ": " + self.handle.before)
James Leec9cacaf2014-04-08 09:17:39 -0700376 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700377 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700378 response = main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700379 return response
380
Jon Hall76f2c462014-04-17 11:37:15 -0700381 def print_version(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700382 '''
383 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
384 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700385 try:
386 self.handle.sendline("export TERM=xterm-256color")
387 self.handle.expect("xterm-256color")
James Leec9cacaf2014-04-08 09:17:39 -0700388 self.handle.expect("\$")
adminf939f8b2014-04-03 17:22:56 -0700389 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
Jon Hallf89c8552014-04-02 13:14:06 -0700390 self.handle.expect("cd ..")
391 self.handle.expect("\$")
admine0eeea22014-04-14 10:22:46 -0700392 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
393 main.log.report(response)
Jon Hall76f2c462014-04-17 11:37:15 -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()
399 except:
400 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
401 main.log.error( traceback.print_exc() )
402 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
403 main.cleanup()
404 def get_version(self):
405 '''
406 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
407 '''
408 try:
409 self.handle.sendline("export TERM=xterm-256color")
410 self.handle.expect("xterm-256color")
411 self.handle.expect("\$")
412 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
413 self.handle.expect("cd ..")
414 self.handle.expect("\$")
415 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
admine0eeea22014-04-14 10:22:46 -0700416 lines=response.splitlines()
417 for line in lines:
418 print line
419 return lines[2]
Jon Hallae05dc22014-04-16 10:56:28 -0700420 except pexpect.EOF:
421 main.log.error(self.name + ": EOF exception found")
422 main.log.error(self.name + ": " + self.handle.before)
423 main.cleanup()
424 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700425 except:
426 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
427 main.log.error( traceback.print_exc() )
428 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
429 main.cleanup()
430 main.exit()
adminbae64d82013-08-01 10:50:15 -0700431
admin2a9548d2014-06-17 14:08:07 -0700432
433 def add_intent(self, intent_id,src_dpid,dst_dpid,src_mac,dst_mac,intentIP,intentPort=8080,intentURL="wm/onos/intent/high" , intent_type = 'SHORTEST_PATH', static_path=False, src_port=1,dst_port=1):
434 "CLI command callback: set intent"
435
436 intents = []
437 oper = {}
438 # Create the POST payload
439 oper['intentId'] = intent_id
440 oper['intentType'] = intent_type # XXX: Hardcoded
441 oper['staticPath'] = static_path # XXX: Hardcoded
442 oper['srcSwitchDpid'] = src_dpid
443 oper['srcSwitchPort'] = src_port
444 oper['dstSwitchDpid'] = dst_dpid
445 oper['dstSwitchPort'] = dst_port
446 oper['matchSrcMac'] = src_mac
447 oper['matchDstMac'] = dst_mac
448 intents.append(oper)
449 url = "http://%s:%s/%s"%(intentIP,intentPort,intentURL)
450 parsed_result = []
451 data_json = json.dumps(intents)
452 try:
453 request = urllib2.Request(url,data_json)
454 request.add_header("Content-Type", "application/json")
455 response=urllib2.urlopen(request)
456 result = response.read()
457 response.close()
458 if len(result) != 0:
459 parsed_result = json.loads(result)
460 except HTTPError as exc:
461 print "ERROR:"
462 print " REST GET URL: %s" % url
463 # NOTE: exc.fp contains the object with the response payload
464 error_payload = json.loads(exc.fp.read())
465 print " REST Error Code: %s" % (error_payload['code'])
466 print " REST Error Summary: %s" % (error_payload['summary'])
467 print " REST Error Description: %s" % (error_payload['formattedDescription'])
468 print " HTTP Error Code: %s" % exc.code
469 print " HTTP Error Reason: %s" % exc.reason
470 except URLError as exc:
471 print "ERROR:"
472 print " REST GET URL: %s" % url
473 print " URL Error Reason: %s" % exc.reason
474 return parsed_result
475
476
477
478
479 def add_intents(self):
480 main.log.info("Sending new intents to ONOS")
adminc6cfc1c2014-04-21 13:55:21 -0700481 self.handle.sendline("cd "+self.home+ "/scripts")
482 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700483 main.log.info("Adding intents")
adminc6cfc1c2014-04-21 13:55:21 -0700484 self.handle.sendline("./pyintents.py")
Jon Hall9c6e2c02014-05-02 10:18:53 -0700485 self.handle.expect(["$",pexpect.EOF,pexpect.TIMEOUT])
486 response = self.handle.before
adminc6cfc1c2014-04-21 13:55:21 -0700487 self.handle.sendline("cd "+self.home)
488 return main.TRUE
489
admin2a9548d2014-06-17 14:08:07 -0700490 def rm_intents(self):
491 main.log.info("Deleteing Intents from ONOS")
adminc6cfc1c2014-04-21 13:55:21 -0700492 self.handle.sendline("cd "+self.home+ "/scripts")
493 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700494 main.log.info("Deleting Intnents")
adminc6cfc1c2014-04-21 13:55:21 -0700495 self.handle.sendline("./rmpyintents.py")
Jon Hall9c6e2c02014-05-02 10:18:53 -0700496 self.handle.expect(["$",pexpect.EOF,pexpect.TIMEOUT])
497 response = self.handle.before
adminc6cfc1c2014-04-21 13:55:21 -0700498 self.handle.sendline("cd "+self.home)
499 return main.TRUE
500
admin2a9548d2014-06-17 14:08:07 -0700501 def purge_intents(self):
Jon Hall265149f2014-04-25 13:39:52 -0700502 main.log.info("Purging dead intents")
503 self.handle.sendline("cd "+self.home+ "/scripts")
504 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700505 main.log.info("Sending Purge Intent Rest call to ONOS")
Jon Hall265149f2014-04-25 13:39:52 -0700506 self.handle.sendline("./purgeintents.py")
507 self.handle.sendline("cd "+self.home)
508 return main.TRUE
adminc6cfc1c2014-04-21 13:55:21 -0700509
510
511
Jon Hall4a2b0482014-04-18 16:29:26 -0700512 def add_flow(self, testONip, user = "admin", password = "", flowDef = "/flowdef.txt"):
Jon Halld8dc5772014-04-08 16:26:29 -0700513 '''
514 Copies the flowdef file from TestStation -> ONOS machine
515 Then runs ./add_flow.py to add the flows to ONOS
516 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700517 try:
518 main.log.info("Adding Flows...")
519 self.handle.sendline("scp %s@%s:%s /tmp/flowtmp" %(user,testONip,flowDef))
520 i=self.handle.expect(['[pP]assword:', '100%', pexpect.TIMEOUT],30)
521 if(i==0):
522 self.handle.sendline("%s" %(password))
523 self.handle.sendline("")
524 self.handle.expect("100%")
525 self.handle.expect("\$", 30)
526 self.handle.sendline(self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
527 self.handle.expect("\$", 1000)
528 main.log.info("Flows added")
529 return main.TRUE
530
531 elif(i==1):
532 self.handle.sendline("")
533 self.handle.expect("\$", 10)
534 self.handle.sendline( self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
535 self.handle.expect("\$", 1000)
536 main.log.info("Flows added")
537 return main.TRUE
538
539 elif(i==2):
540 main.log.error("Flow Def file SCP Timed out...")
541 return main.FALSE
542
543 else:
544 main.log.error("Failed to add flows...")
James Leec9cacaf2014-04-08 09:17:39 -0700545 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700546 except pexpect.EOF:
547 main.log.error(self.name + ": EOF exception found")
548 main.log.error(self.name + ": " + self.handle.before)
549 main.cleanup()
550 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700551 except:
552 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
553 main.log.error( traceback.print_exc() )
554 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
555 main.cleanup()
556 main.exit()
557
adminbae64d82013-08-01 10:50:15 -0700558
559 def delete_flow(self, *delParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700560 '''
561 Deletes a specific flow, a range of flows, or all flows.
562 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700563 try:
564 if len(delParams)==1:
565 if str(delParams[0])=="all":
566 main.log.info(self.name + ": Deleting ALL flows...")
567 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh all",prompt="done",timeout=150)
568 self.handle.sendline(self.home + "/web/delete_flow.py all")
569 self.handle.expect("delete_flow")
570 self.handle.expect("\$",1000)
571 main.log.info(self.name + ": Flows deleted")
572 else:
573 main.log.info(self.name + ": Deleting flow "+str(delParams[0])+"...")
574 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0]),prompt="done",timeout=150)
575 #self.execute(cmd="\n",prompt="\$",timeout=60)
576 self.handle.sendline(self.home +"/web/delete_flow.py %d" % int(delParams[0]))
577 self.handle.expect("delete_flow")
578 self.handle.expect("\$",60)
579 main.log.info(self.name + ": Flow deleted")
580 elif len(delParams)==2:
581 main.log.info(self.name + ": Deleting flows "+str(delParams[0])+" through "+str(delParams[1])+"...")
582 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0])+" "+str(delParams[1]),prompt="done",timeout=150)
583 #self.execute(cmd="\n",prompt="\$",timeout=60)
584 self.handle.sendline(self.home + "/web/delete_flow.py %d %d" % (int(delParams[0]), int(delParams[1])))
585 self.handle.expect("delete_flow")
586 self.handle.expect("\$",600)
587 main.log.info(self.name + ": Flows deleted")
Jon Hallae05dc22014-04-16 10:56:28 -0700588 except pexpect.EOF:
589 main.log.error(self.name + ": EOF exception found")
590 main.log.error(self.name + ": " + self.handle.before)
591 main.cleanup()
592 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700593 except:
594 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
595 main.log.error( traceback.print_exc() )
596 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
597 main.cleanup()
598 main.exit()
adminbae64d82013-08-01 10:50:15 -0700599
600 def check_flow(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700601 '''
602 Calls the ./get_flow.py all and checks:
603 - If each FlowPath has at least one FlowEntry
604 - That there are no "NOT"s found
605 returns TRUE/FALSE
606 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700607 try:
608 flowEntryDetect = 1
609 count = 0
610 self.handle.sendline("clear")
611 time.sleep(1)
612 self.handle.sendline(self.home + "/web/get_flow.py all")
613 self.handle.expect("get_flow")
Jon Hall3bd7c792014-05-30 09:05:21 -0700614 for x in range(15):
Jon Hallf89c8552014-04-02 13:14:06 -0700615 i=self.handle.expect(['FlowPath','FlowEntry','NOT','\$',pexpect.TIMEOUT],timeout=180)
616 if i==0:
617 count = count + 1
618 if flowEntryDetect == 0:
619 main.log.info(self.name + ": FlowPath without FlowEntry")
620 return main.FALSE
621 else:
622 flowEntryDetect = 0
623 elif i==1:
624 flowEntryDetect = 1
625 elif i==2:
626 main.log.error(self.name + ": Found a NOT")
adminbae64d82013-08-01 10:50:15 -0700627 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700628 elif i==3:
629 if count == 0:
630 main.log.info(self.name + ": There don't seem to be any flows here...")
631 return main.FALSE
632 else:
633 main.log.info(self.name + ": All flows pass")
634 main.log.info(self.name + ": Number of FlowPaths: "+str(count))
635 return main.TRUE
636 elif i==4:
James Leec9cacaf2014-04-08 09:17:39 -0700637 main.log.error(self.name + ":Check_flow() - Command Timeout!")
Jon Hallf89c8552014-04-02 13:14:06 -0700638 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700639 except pexpect.EOF:
640 main.log.error(self.name + ": EOF exception found")
641 main.log.error(self.name + ": " + self.handle.before)
642 main.cleanup()
643 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700644 except:
645 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
646 main.log.error( traceback.print_exc() )
647 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
648 main.cleanup()
649 main.exit()
adminbae64d82013-08-01 10:50:15 -0700650
651 def get_flow(self, *flowParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700652 '''
653 Returns verbose output of ./get_flow.py
654 '''
655 try:
656 if len(flowParams)==1:
657 if str(flowParams[0])=="all":
658 self.execute(cmd="\n",prompt="\$",timeout=60)
659 main.log.info(self.name + ": Getting all flow data...")
660 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh all",prompt="done",timeout=150)
661 self.execute(cmd="\n",prompt="\$",timeout=60)
662 return data
663 else:
664 main.log.info(self.name + ": Retrieving flow "+str(flowParams[0])+" data...")
665 data = self.execute(cmd=self.home +"/scripts/TestON_get_flow.sh "+str(flowParams[0]),prompt="done",timeout=150)
666 self.execute(cmd="\n",prompt="\$",timeout=60)
667 return data
668 elif len(flowParams)==5:
669 main.log.info(self.name + ": Retrieving flow installer data...")
670 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)
671 self.execute(cmd="\n",prompt="\$",timeout=60)
672 return data
673 elif len(flowParams)==4:
674 main.log.info(self.name + ": Retrieving flow endpoints...")
675 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)
676 self.execute(cmd="\n",prompt="\$",timeout=60)
677 return data
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 Halld8dc5772014-04-08 16:26:29 -0700683 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700684 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
690
Jon Hall2f42a672014-05-28 10:13:18 -0700691# http://localhost:8080/wm/onos/topology/switches
692# http://localhost:8080/wm/onos/topology/links
Jon Hall8060af02014-04-14 14:17:58 -0700693# http://localhost:8080/wm/onos/registry/controllers/json
694# http://localhost:8080/wm/onos/registry/switches/json"
adminbae64d82013-08-01 10:50:15 -0700695
696 def get_json(self, url):
Jon Halld8dc5772014-04-08 16:26:29 -0700697 '''
698 Helper functions used to parse the json output of a rest call
699 '''
adminbae64d82013-08-01 10:50:15 -0700700 try:
Jon Hallf89c8552014-04-02 13:14:06 -0700701 try:
702 command = "curl -s %s" % (url)
703 result = os.popen(command).read()
704 parsedResult = json.loads(result)
705 except:
706 print "REST IF %s has issue" % command
707 parsedResult = ""
708
709 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
710 print "REST %s returned code %s" % (command, parsedResult['code'])
711 parsedResult = ""
James Leec9cacaf2014-04-08 09:17:39 -0700712 return parsedResult
Jon Hallae05dc22014-04-16 10:56:28 -0700713 except pexpect.EOF:
714 main.log.error(self.name + ": EOF exception found")
715 main.log.error(self.name + ": " + self.handle.before)
716 main.cleanup()
717 main.exit()
adminbae64d82013-08-01 10:50:15 -0700718 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700719 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
720 main.log.error( traceback.print_exc() )
721 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
722 main.cleanup()
723 main.exit()
adminbae64d82013-08-01 10:50:15 -0700724
Jon Hallf89c8552014-04-02 13:14:06 -0700725 def check_switch(self,RestIP,correct_nr_switch, RestPort ="8080" ):
Jon Halld8dc5772014-04-08 16:26:29 -0700726 '''
727 Used by check_status
728 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700729 try:
730 buf = ""
731 retcode = 0
admin2e131ab2014-05-28 10:03:42 -0700732 url="http://%s:%s/wm/onos/topology/switches" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700733 parsedResult = self.get_json(url)
734 if parsedResult == "":
735 retcode = 1
736 return (retcode, "Rest API has an issue")
737 url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
738 registry = self.get_json(url)
739
740 if registry == "":
741 retcode = 1
742 return (retcode, "Rest API has an issue")
743
744 cnt = 0
745 active = 0
adminbae64d82013-08-01 10:50:15 -0700746
Jon Hallf89c8552014-04-02 13:14:06 -0700747 for s in parsedResult:
748 cnt += 1
James Leec9cacaf2014-04-08 09:17:39 -0700749 if s['state'] == "ACTIVE":
Jon Hallf89c8552014-04-02 13:14:06 -0700750 active += 1
adminbae64d82013-08-01 10:50:15 -0700751
Jon Hallf89c8552014-04-02 13:14:06 -0700752 buf += "switch: network %d : %d switches %d active\n" % (0+1, cnt, active)
753 if correct_nr_switch != cnt:
754 buf += "switch fail: network %d should have %d switches but has %d\n" % (1, correct_nr_switch, cnt)
755 retcode = 1
adminbae64d82013-08-01 10:50:15 -0700756
Jon Hallf89c8552014-04-02 13:14:06 -0700757 if correct_nr_switch != active:
758 buf += "switch fail: network %d should have %d active switches but has %d\n" % (1, correct_nr_switch, active)
759 retcode = 1
760
761 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700762 except pexpect.EOF:
763 main.log.error(self.name + ": EOF exception found")
764 main.log.error(self.name + ": " + self.handle.before)
765 main.cleanup()
766 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700767 except:
768 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
769 main.log.error( traceback.print_exc() )
770 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
771 main.cleanup()
772 main.exit()
adminbae64d82013-08-01 10:50:15 -0700773
Jon Hallf89c8552014-04-02 13:14:06 -0700774 def check_link(self,RestIP, nr_links, RestPort = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700775 '''
776 Used by check_status
777 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700778 try:
779 buf = ""
780 retcode = 0
781
admin2e131ab2014-05-28 10:03:42 -0700782 url = "http://%s:%s/wm/onos/topology/links" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700783 parsedResult = self.get_json(url)
784
785 if parsedResult == "":
786 retcode = 1
787 return (retcode, "Rest API has an issue")
788
789 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
790 intra = 0
791 interlink=0
792
793 for s in parsedResult:
James Leec9cacaf2014-04-08 09:17:39 -0700794 intra = intra + 1
Jon Hallf89c8552014-04-02 13:14:06 -0700795
796 if intra != nr_links:
797 buf += "link fail\n"
798 retcode = 1
799
800 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700801 except pexpect.EOF:
802 main.log.error(self.name + ": EOF exception found")
803 main.log.error(self.name + ": " + self.handle.before)
804 main.cleanup()
805 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700806 except:
807 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
808 main.log.error( traceback.print_exc() )
809 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
810 main.cleanup()
811 main.exit()
adminbae64d82013-08-01 10:50:15 -0700812
Jon Hallf89c8552014-04-02 13:14:06 -0700813 def check_status_report(self, ip, numoswitch, numolink, port="8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700814 '''
815 Checks the number of swithes & links that ONOS sees against the supplied values.
816 Writes to the report log.
817 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700818 try:
James Leec9cacaf2014-04-08 09:17:39 -0700819 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700820 switch = self.check_switch(ip, int(numoswitch), port)
821 link = self.check_link(ip, int(numolink), port)
822 value = switch[0]
823 value += link[0]
824 main.log.report( self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
825 if value != 0:
826 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700827 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700828 # "PASS"
829 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700830 except pexpect.EOF:
831 main.log.error(self.name + ": EOF exception found")
832 main.log.error(self.name + ": " + self.handle.before)
833 main.cleanup()
834 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700835 except:
836 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
837 main.log.error( traceback.print_exc() )
838 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
839 main.cleanup()
840 main.exit()
adminbae64d82013-08-01 10:50:15 -0700841
Jon Hallf89c8552014-04-02 13:14:06 -0700842 def check_status(self, ip, numoswitch, numolink, port = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700843 '''
844 Checks the number of swithes & links that ONOS sees against the supplied values.
845 Writes to the main log.
846 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700847 try:
James Leec9cacaf2014-04-08 09:17:39 -0700848 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700849 switch = self.check_switch(ip, int(numoswitch), port)
850 link = self.check_link(ip, int(numolink), port)
851 value = switch[0]
852 value += link[0]
853 main.log.info(self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
854 if value != 0:
855 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700856 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700857 # "PASS"
858 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700859 except pexpect.EOF:
860 main.log.error(self.name + ": EOF exception found")
861 main.log.error(self.name + ": " + self.handle.before)
862 main.cleanup()
863 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700864 except:
865 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
866 main.log.error( traceback.print_exc() )
867 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
868 main.cleanup()
869 main.exit()
adminbae64d82013-08-01 10:50:15 -0700870
adminbae64d82013-08-01 10:50:15 -0700871 def check_for_no_exceptions(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700872 '''
873 TODO: Rewrite
874 Used by CassndraCheck.py to scan ONOS logs for Exceptions
875 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700876 try:
877 self.handle.sendline("dsh 'grep Exception ~/ONOS/onos-logs/onos.*.log'")
James Leec9cacaf2014-04-08 09:17:39 -0700878 self.handle.expect("\$ dsh")
Jon Hallf89c8552014-04-02 13:14:06 -0700879 self.handle.expect("\$")
880 output = self.handle.before
James Leec9cacaf2014-04-08 09:17:39 -0700881 main.log.info(self.name + ": " + output )
Jon Hallf89c8552014-04-02 13:14:06 -0700882 if re.search("Exception",output):
883 return main.FALSE
884 else :
885 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700886 except pexpect.EOF:
887 main.log.error(self.name + ": EOF exception found")
888 main.log.error(self.name + ": " + self.handle.before)
889 main.cleanup()
890 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700891 except:
892 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
893 main.log.error( traceback.print_exc() )
894 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
895 main.cleanup()
896 main.exit()
897
898
admine0eeea22014-04-14 10:22:46 -0700899 def git_pull(self, comp1=""):
Jon Halld8dc5772014-04-08 16:26:29 -0700900 '''
Jon Halld8dc5772014-04-08 16:26:29 -0700901 Assumes that "git pull" works without login
admine0eeea22014-04-14 10:22:46 -0700902
903 This function will perform a git pull on the ONOS instance.
904 If used as git_pull("NODE") it will do git pull + NODE. This is
905 for the purpose of pulling from other nodes if necessary.
906
907 Otherwise, this function will perform a git pull in the
908 ONOS repository. If it has any problems, it will return main.ERROR
909 If it successfully does a git_pull, it will return a 1.
910 If it has no updates, it will return a 0.
911
Jon Halld8dc5772014-04-08 16:26:29 -0700912 '''
Jon Hallae05dc22014-04-16 10:56:28 -0700913 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700914 # main.log.info(self.name + ": Stopping ONOS")
915 #self.stop()
Jon Hallae05dc22014-04-16 10:56:28 -0700916 self.handle.sendline("cd " + self.home)
917 self.handle.expect("ONOS\$")
918 if comp1=="":
919 self.handle.sendline("git pull")
920 else:
921 self.handle.sendline("git pull " + comp1)
922
923 uptodate = 0
Jon Hall5a8aac62014-06-03 09:30:21 -0700924 i=self.handle.expect(['fatal','Username\sfor\s(.*):\s','Unpacking\sobjects',pexpect.TIMEOUT,'Already up-to-date','Aborting','You\sare\snot\scurrently\son\sa\sbranch'],timeout=1700)
Jon Hallae05dc22014-04-16 10:56:28 -0700925 #debug
926 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
927 if i==0:
928 main.log.error(self.name + ": Git pull had some issue...")
929 return main.ERROR
930 elif i==1:
931 main.log.error(self.name + ": Git Pull Asking for username!!! BADD!")
932 return main.ERROR
933 elif i==2:
934 main.log.info(self.name + ": Git Pull - pulling repository now")
935 self.handle.expect("ONOS\$", 120)
936 return 0
937 elif i==3:
938 main.log.error(self.name + ": Git Pull - TIMEOUT")
939 return main.ERROR
940 elif i==4:
941 main.log.info(self.name + ": Git Pull - Already up to date")
942 return 1
943 elif i==5:
944 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
945 return main.ERROR
Jon Hall5a8aac62014-06-03 09:30:21 -0700946 elif i==6:
947 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
948 return main.ERROR
Jon Hallae05dc22014-04-16 10:56:28 -0700949 else:
950 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
951 return main.ERROR
952 except pexpect.EOF:
953 main.log.error(self.name + ": EOF exception found")
954 main.log.error(self.name + ": " + self.handle.before)
955 main.cleanup()
956 main.exit()
957 except:
958 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
959 main.log.error( traceback.print_exc() )
960 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
961 main.cleanup()
962 main.exit()
admine0eeea22014-04-14 10:22:46 -0700963#********************************************************
964
965
admin7373a812014-04-16 09:49:02 -0700966 def git_compile(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700967 '''
968 Compiles ONOS
969 First runs mvn clean then mvn compile
970 '''
971 try:
972 main.log.info(self.name + ": mvn clean")
admin8fc02822014-04-16 13:31:23 -0700973 self.handle.sendline("cd " + self.home)
Jon Hallae05dc22014-04-16 10:56:28 -0700974 self.handle.sendline("mvn clean")
975 while 1:
Jon Hall1636f942014-04-17 10:07:23 -0700976 i=self.handle.expect(['There\sis\sinsufficient\smemory\sfor\sthe\sJava\sRuntime\sEnvironment\sto\scontinue','BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=30)
Jon Hallae05dc22014-04-16 10:56:28 -0700977 if i == 0:
Jon Hall1636f942014-04-17 10:07:23 -0700978 main.log.error(self.name + ":There is insufficient memory for the Java Runtime Environment to continue.")
Jon Hallae05dc22014-04-16 10:56:28 -0700979 return main.FALSE
980 elif i == 1:
Jon Hall1636f942014-04-17 10:07:23 -0700981 main.log.error(self.name + ": Clean failure!")
982 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700983 elif i == 2:
Jon Hall1636f942014-04-17 10:07:23 -0700984 main.log.info(self.name + ": Clean success!")
985 elif i == 3:
admin8fc02822014-04-16 13:31:23 -0700986 main.log.info(self.name + ": Clean complete")
Jon Hallae05dc22014-04-16 10:56:28 -0700987 break;
Jon Hall1636f942014-04-17 10:07:23 -0700988 elif i == 4:
Jon Hallae05dc22014-04-16 10:56:28 -0700989 main.log.error(self.name + ": mvn clean TIMEOUT!")
990 return main.FALSE
Jon Hall1636f942014-04-17 10:07:23 -0700991 else:
992 main.log.error(self.name + ": unexpected response from mvn clean")
993 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700994
995 main.log.info(self.name + ": mvn compile")
996 self.handle.sendline("mvn compile")
997 while 1:
Jon Hall1636f942014-04-17 10:07:23 -0700998 i=self.handle.expect(['There\sis\sinsufficient\smemory\sfor\sthe\sJava\sRuntime\sEnvironment\sto\scontinue','BUILD\sFAILURE','BUILD\sSUCCESS','ONOS\$',pexpect.TIMEOUT],timeout=60)
Jon Hallae05dc22014-04-16 10:56:28 -0700999 if i == 0:
Jon Hall1636f942014-04-17 10:07:23 -07001000 main.log.error(self.name + ":There is insufficient memory for the Java Runtime Environment to continue.")
1001 return main.FALSE
1002 if i == 1:
Jon Hallae05dc22014-04-16 10:56:28 -07001003 main.log.error(self.name + ": Build failure!")
1004 return main.FALSE
Jon Hall1636f942014-04-17 10:07:23 -07001005 elif i == 2:
Jon Hallae05dc22014-04-16 10:56:28 -07001006 main.log.info(self.name + ": Build success!")
Jon Hall1636f942014-04-17 10:07:23 -07001007 elif i == 3:
Jon Hallae05dc22014-04-16 10:56:28 -07001008 main.log.info(self.name + ": Build complete")
Jon Hall010f5412014-05-21 15:10:12 -07001009 self.handle.expect("\$", timeout=60)
Jon Hallae05dc22014-04-16 10:56:28 -07001010 return main.TRUE
Jon Hall1636f942014-04-17 10:07:23 -07001011 elif i == 4:
Jon Hallae05dc22014-04-16 10:56:28 -07001012 main.log.error(self.name + ": mvn compile TIMEOUT!")
1013 return main.FALSE
1014 else:
Jon Hall1636f942014-04-17 10:07:23 -07001015 main.log.error(self.name + ": unexpected response from mvn compile")
1016 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -07001017 except pexpect.EOF:
1018 main.log.error(self.name + ": EOF exception found")
1019 main.log.error(self.name + ": " + self.handle.before)
1020 main.cleanup()
1021 main.exit()
1022 except:
1023 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1024 main.log.error( traceback.print_exc() )
1025 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1026 main.cleanup()
1027 main.exit()
admin4a58db92013-09-30 12:04:54 -07001028
Jon Hallf89c8552014-04-02 13:14:06 -07001029 def tcpdump(self, intf = "eth0"):
Jon Hallae05dc22014-04-16 10:56:28 -07001030 '''
1031 Runs tpdump on an intferface and saves in onos-logs under the ONOS home directory
1032 intf can be specified, or the default eth0 is used
1033 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001034 try:
1035 self.handle.sendline("")
1036 self.handle.expect("\$")
Jon Hall26f316b2014-04-11 11:21:25 -07001037 self.handle.sendline("sudo tcpdump -n -i "+ intf + " -s0 -w " + self.home +"/onos-logs/tcpdump &")
Jon Hallf89c8552014-04-02 13:14:06 -07001038 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT],timeout=10)
1039 if i == 0:
1040 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
1041 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -07001042 elif i == 1:
Jon Hallf89c8552014-04-02 13:14:06 -07001043 main.log.info(self.name + ": tcpdump started on " + intf)
1044 return main.TRUE
James Leec9cacaf2014-04-08 09:17:39 -07001045 elif i == 2:
Jon Hallf89c8552014-04-02 13:14:06 -07001046 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
1047 return main.FALSE
1048 else:
1049 main.log.error(self.name + ": tcpdump - unexpected response")
1050 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -07001051 except pexpect.EOF:
1052 main.log.error(self.name + ": EOF exception found")
1053 main.log.error(self.name + ": " + self.handle.before)
1054 main.cleanup()
1055 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001056 except:
1057 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1058 main.log.error( traceback.print_exc() )
1059 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1060 main.cleanup()
1061 main.exit()
1062
admin4a58db92013-09-30 12:04:54 -07001063 def kill_tcpdump(self):
Jon Hallae05dc22014-04-16 10:56:28 -07001064 '''
1065 Kills any tcpdump processes running
1066 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001067 try:
1068 self.handle.sendline("")
1069 self.handle.expect("\$")
1070 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tcpdump -n\" | grep -v grep | awk '{print $2}'`")
Jon Hallae05dc22014-04-16 10:56:28 -07001071 except pexpect.EOF:
1072 main.log.error(self.name + ": EOF exception found")
1073 main.log.error(self.name + ": " + self.handle.before)
1074 main.cleanup()
1075 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001076 except:
1077 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1078 main.log.error( traceback.print_exc() )
1079 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1080 main.cleanup()
1081 main.exit()
1082
Jon Hallf15f7852014-05-20 10:37:23 -07001083 def find_host(self,RestIP,RestPort,RestAPI,hostMAC):
admin2a9548d2014-06-17 14:08:07 -07001084 retcode = 0 # number of hosts found with given MAC
1085 retswitch = [] # Switch DPID's of hosts found with MAC
1086 retport = [] # Switch port connected to to hosts found with MAC
Jon Hallf15f7852014-05-20 10:37:23 -07001087 foundHost = []
Jon Hallf89c8552014-04-02 13:14:06 -07001088 try:
Jon Hall2f42a672014-05-28 10:13:18 -07001089 ##### device rest API is: 'host:8080/wm/onos/topology/switches' ###
Jon Hallf89c8552014-04-02 13:14:06 -07001090 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
1091
1092 try:
1093 command = "curl -s %s" % (url)
1094 result = os.popen(command).read()
1095 parsedResult = json.loads(result)
1096 # print parsedResult
1097 except:
1098 print "REST IF %s has issue" % command
1099 parsedResult = ""
1100
1101 if parsedResult == "":
Jon Hallf15f7852014-05-20 10:37:23 -07001102 return (retcode, "Rest API has an error", retport)
Jon Hallf89c8552014-04-02 13:14:06 -07001103 else:
Jon Hallf15f7852014-05-20 10:37:23 -07001104 for host in enumerate(parsedResult):
1105 print host
1106 if (host[1] != []):
1107 try:
1108 foundHost = host[1]['mac']
1109 except:
1110 print "Error in detecting MAC address."
1111 print foundHost
1112 print hostMAC
1113 if foundHost == hostMAC:
1114 for switch in enumerate(host[1]['attachmentPoints']):
1115 retswitch.append(switch[1]['dpid'])
1116 retport.append(switch[1]['port'])
1117 retcode = retcode +1
1118 foundHost =''
1119 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001120 for switch in enumerate(parsedResult):
1121 for port in enumerate(switch[1]['ports']):
admin2a9548d2014-06-17 14:08:07 -07001122 if ( port[1]['hosts'] != [] ):
Jon Hallf89c8552014-04-02 13:14:06 -07001123 try:
admin2a9548d2014-06-17 14:08:07 -07001124 foundHost = port[1]['hosts'][0]['ipv4addresses'][0]['ipv4']
Jon Hallf89c8552014-04-02 13:14:06 -07001125 except:
Jon Hallf15f7852014-05-20 10:37:23 -07001126 print "Error in detecting MAC address."
1127 if foundHost == hostMAC:
Jon Hallf89c8552014-04-02 13:14:06 -07001128 retswitch.append(switch[1]['dpid'])
1129 retport.append(port[1]['desc'])
admin2a9548d2014-06-17 14:08:07 -07001130 retmac.append(port[1]['hosts'][0]['mac'])
Jon Hallf89c8552014-04-02 13:14:06 -07001131 retcode = retcode +1
Jon Hallf15f7852014-05-20 10:37:23 -07001132 foundHost =''
1133 '''
1134 return(retcode, retswitch, retport)
Jon Hallae05dc22014-04-16 10:56:28 -07001135 except pexpect.EOF:
1136 main.log.error(self.name + ": EOF exception found")
1137 main.log.error(self.name + ": " + self.handle.before)
1138 main.cleanup()
1139 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001140 except:
1141 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1142 main.log.error( traceback.print_exc() )
1143 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1144 main.cleanup()
1145 main.exit()
SeanCorcoran29b70542014-05-14 14:53:42 -07001146
1147 def check_exceptions(self):
1148 '''
1149 Greps the logs for "xception"
1150 '''
1151 try:
1152 output = ''
1153 self.handle.sendline("")
Jon Halla78cf9a2014-05-16 10:49:30 -07001154 i = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
1155 #main.log.warn("first expect response: " +str(i))
SeanCorcoran29b70542014-05-14 14:53:42 -07001156 self.handle.sendline("cd "+self.home+"/onos-logs")
Jon Halla78cf9a2014-05-16 10:49:30 -07001157 self.handle.sendline("grep \"xception\" *")
1158 i = self.handle.expect(["\*",pexpect.EOF,pexpect.TIMEOUT])
1159 #main.log.warn("second expect response: " +str(i))
1160 i = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
1161 #main.log.warn("third expect response: " +str(i))
SeanCorcoran29b70542014-05-14 14:53:42 -07001162 response = self.handle.before
Jon Halla78cf9a2014-05-16 10:49:30 -07001163 count = 0
SeanCorcoran29b70542014-05-14 14:53:42 -07001164 for line in response.splitlines():
1165 if re.search("log:", line):
1166 output +="Exceptions found in " + line + "\n"
Jon Halla78cf9a2014-05-16 10:49:30 -07001167 count +=1
SeanCorcoran29b70542014-05-14 14:53:42 -07001168 elif re.search("std...:",line):
1169 output+="Exceptions found in " + line + "\n"
Jon Halla78cf9a2014-05-16 10:49:30 -07001170 count +=1
SeanCorcoran29b70542014-05-14 14:53:42 -07001171 else:
1172 pass
1173 #these should be the old logs
admin1723f1c2014-05-19 16:08:39 -07001174 main.log.report(str(count) + " Exceptions were found on "+self.name)
SeanCorcoran29b70542014-05-14 14:53:42 -07001175 return output
1176 except pexpect.TIMEOUT:
1177 main.log.error(self.name + ": Timeout exception found in check_exceptions function")
1178 except pexpect.EOF:
1179 main.log.error(self.name + ": EOF exception found")
1180 main.log.error(self.name + ": " + self.handle.before)
1181 main.cleanup()
1182 main.exit()
1183 except:
1184 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1185 main.log.error( traceback.print_exc() )
1186 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1187 main.cleanup()
1188 main.exit()