blob: 4b412ebd18e3b716bce9527bdcfa3436323e551c [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
Jon Hallf0a494f2014-06-23 15:37:40 -070072 def start(self, env = ''):
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)
Jon Hallf0a494f2014-06-23 15:37:40 -070081 self.handle.sendline(env + "./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 :
Jon Hallf0a494f2014-06-23 15:37:40 -0700179 main.log.warn(self.name + " WARNING: status recieved unknown response")
180 main.log.warn(response)
Jon Hallf89c8552014-04-02 13:14:06 -0700181 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700182 except pexpect.EOF:
183 main.log.error(self.name + ": EOF exception found")
184 main.log.error(self.name + ": " + self.handle.before)
185 main.cleanup()
186 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700187 except:
188 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
189 main.log.error( traceback.print_exc() )
190 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
191 main.cleanup()
192 main.exit()
193
adminbae64d82013-08-01 10:50:15 -0700194
195 def isup(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700196 '''
197 A more complete check to see if ONOS is up and running properly.
198 First, it checks if the process is up.
199 Second, it reads the logs for "Exception: Connection refused"
200 Third, it makes sure the logs are actually moving.
201 returns TRUE/FALSE accordingly.
202 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700203 try:
204 self.execute(cmd="\n",prompt="\$",timeout=10)
Jon Hall4a2b0482014-04-18 16:29:26 -0700205 self.handle.sendline("cd "+self.home)
206 response = self.execute(cmd= "./onos.sh core status ",prompt="running",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700207 self.execute(cmd="\n",prompt="\$",timeout=10)
208 tail1 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
admine8c47d02014-06-03 11:59:16 -0700209 time.sleep(10)
Jon Hallf89c8552014-04-02 13:14:06 -0700210 self.execute(cmd="\n",prompt="\$",timeout=10)
211 tail2 = self.execute(cmd="tail " + self.home + "/onos-logs/onos.*.log",prompt="\$",timeout=10)
212 pattern = '(.*)1 instance(.*)'
213 pattern2 = '(.*)Exception: Connection refused(.*)'
214 # if utilities.assert_matches(expect=pattern,actual=response,onpass="ONOS process is running...",onfail="ONOS process not running..."):
admin2a9548d2014-06-17 14:08:07 -0700215 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 -0700216 if re.search(pattern, response):
admin2a9548d2014-06-17 14:08:07 -0700217 if running != "":
Jon Halle80ef8c2014-04-29 15:29:13 -0700218 main.log.info(self.name + ": ONOS process is running...")
219 if tail1 == tail2:
220 main.log.error(self.name + ": ONOS is frozen...")#logs aren't moving
221 return main.FALSE
222 elif re.search( pattern2,tail1 ):
223 main.log.info(self.name + ": Connection Refused found in onos log")
224 return main.FALSE
225 elif re.search( pattern2,tail2 ):
226 main.log.info(self.name + ": Connection Refused found in onos log")
227 return main.FALSE
228 else:
229 main.log.info(self.name + ": Onos log is moving! It's looking good!")
230 return main.TRUE
Jon Hall55c79662014-05-19 15:03:40 -0700231 else:
232 main.log.info(self.name + ": ONOS not yet sending out LLDP messages")
233 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700234 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700235 main.log.error(self.name + ": ONOS process not running...")
236 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700237 except pexpect.EOF:
238 main.log.error(self.name + ": EOF exception found")
239 main.log.error(self.name + ": " + self.handle.before)
240 main.cleanup()
241 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700242 except:
243 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
244 main.log.error( traceback.print_exc() )
245 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
246 main.cleanup()
247 main.exit()
adminbae64d82013-08-01 10:50:15 -0700248
Jon Hallf89c8552014-04-02 13:14:06 -0700249
250
James Leec9cacaf2014-04-08 09:17:39 -0700251 def rest_status(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700252 '''
253 Checks if the rest server is running.
254 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700255 try:
256 response = self.execute(cmd= self.home + "/start-rest.sh status ",prompt="running",timeout=10)
257 if re.search("rest\sserver\sis\srunning",response):
258 main.log.info(self.name + ": Rest Server is running")
Jon Hallae05dc22014-04-16 10:56:28 -0700259 return main.TRUE
Jon Hallf89c8552014-04-02 13:14:06 -0700260 elif re.search("rest\sserver\sis\snot\srunning",response):
261 main.log.warn(self.name + ": Rest Server is not Running")
Jon Hallae05dc22014-04-16 10:56:28 -0700262 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700263 else :
264 main.log.error(self.name + ": No response" +response)
Jon Hallae05dc22014-04-16 10:56:28 -0700265 return main.FALSE
266 except pexpect.EOF:
267 main.log.error(self.name + ": EOF exception found")
268 main.log.error(self.name + ": " + self.handle.before)
269 main.cleanup()
270 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700271 except:
272 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
273 main.log.error( traceback.print_exc() )
274 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
275 main.cleanup()
276 main.exit()
admine8c47d02014-06-03 11:59:16 -0700277
278 def stop_all(self):
279 '''
280 Runs ./onos.sh stop
281 '''
282 try:
283 self.handle.sendline("")
284 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
285 self.handle.sendline("cd "+self.home)
286 self.handle.sendline("./onos.sh stop")
287 i=self.handle.expect(["Stop",pexpect.EOF,pexpect.TIMEOUT])
288 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], 60)
289 result = self.handle.before
290 if re.search("Killed", result):
291 main.log.info(self.name + ": ONOS Killed Successfully")
292 return main.TRUE
293 else :
294 main.log.warn(self.name + ": ONOS wasn't running")
295 return main.FALSE
296 except pexpect.EOF:
297 main.log.error(self.name + ": EOF exception found")
298 main.log.error(self.name + ": " + self.handle.before)
299 main.cleanup()
300 main.exit()
301 except:
302 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
303 main.log.error( traceback.print_exc() )
304 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
305 main.cleanup()
306 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700307
308
adminbae64d82013-08-01 10:50:15 -0700309 def stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700310 '''
311 Runs ./onos.sh core stop to stop ONOS
312 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700313 try:
314 self.handle.sendline("")
Jon Hallae05dc22014-04-16 10:56:28 -0700315 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
Jon Hall4a2b0482014-04-18 16:29:26 -0700316 self.handle.sendline("cd "+self.home)
317 self.handle.sendline("./onos.sh core stop")
Jon Hallae05dc22014-04-16 10:56:28 -0700318 i=self.handle.expect(["Stop",pexpect.EOF,pexpect.TIMEOUT])
319 self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT], 60)
James Leec9cacaf2014-04-08 09:17:39 -0700320 result = self.handle.before
Jon Hallf89c8552014-04-02 13:14:06 -0700321 if re.search("Killed", result):
322 main.log.info(self.name + ": ONOS Killed Successfully")
323 return main.TRUE
324 else :
325 main.log.warn(self.name + ": ONOS wasn't running")
326 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700327 except pexpect.EOF:
328 main.log.error(self.name + ": EOF exception found")
329 main.log.error(self.name + ": " + self.handle.before)
330 main.cleanup()
331 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700332 except:
333 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
334 main.log.error( traceback.print_exc() )
335 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
336 main.cleanup()
337 main.exit()
338
adminbae64d82013-08-01 10:50:15 -0700339
340 def rest_stop(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700341 '''
342 Runs ./start-rest.sh stop to stop ONOS rest server
343 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700344 try:
345 response = self.execute(cmd= self.home + "/start-rest.sh stop ",prompt="killing",timeout=10)
346 self.execute(cmd="\n",prompt="\$",timeout=10)
347 if re.search("killing", response):
348 main.log.info(self.name + ": Rest Server Stopped")
349 return main.TRUE
350 else :
351 main.log.error(self.name + ": Failed to Stop, Rest Server is not Running")
352 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700353 except pexpect.EOF:
354 main.log.error(self.name + ": EOF exception found")
355 main.log.error(self.name + ": " + self.handle.before)
356 main.cleanup()
357 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700358 except:
359 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
360 main.log.error( traceback.print_exc() )
361 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
362 main.cleanup()
363 main.exit()
364
365
adminbae64d82013-08-01 10:50:15 -0700366 def disconnect(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700367 '''
368 Called when Test is complete to disconnect the ONOS handle.
369 '''
adminaeedddd2013-08-02 15:14:15 -0700370 response = ''
371 try:
adminbae64d82013-08-01 10:50:15 -0700372 self.handle.sendline("exit")
adminaeedddd2013-08-02 15:14:15 -0700373 self.handle.expect("closed")
Jon Hallae05dc22014-04-16 10:56:28 -0700374 except pexpect.EOF:
375 main.log.error(self.name + ": EOF exception found")
376 main.log.error(self.name + ": " + self.handle.before)
James Leec9cacaf2014-04-08 09:17:39 -0700377 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700378 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700379 response = main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700380 return response
381
Jon Hall76f2c462014-04-17 11:37:15 -0700382 def print_version(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700383 '''
384 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
385 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700386 try:
387 self.handle.sendline("export TERM=xterm-256color")
388 self.handle.expect("xterm-256color")
James Leec9cacaf2014-04-08 09:17:39 -0700389 self.handle.expect("\$")
adminf939f8b2014-04-03 17:22:56 -0700390 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
Jon Hallf89c8552014-04-02 13:14:06 -0700391 self.handle.expect("cd ..")
392 self.handle.expect("\$")
admine0eeea22014-04-14 10:22:46 -0700393 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
394 main.log.report(response)
Jon Hall76f2c462014-04-17 11:37:15 -0700395 except pexpect.EOF:
396 main.log.error(self.name + ": EOF exception found")
397 main.log.error(self.name + ": " + self.handle.before)
398 main.cleanup()
399 main.exit()
400 except:
401 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
402 main.log.error( traceback.print_exc() )
403 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
404 main.cleanup()
405 def get_version(self):
406 '''
407 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
408 '''
409 try:
410 self.handle.sendline("export TERM=xterm-256color")
411 self.handle.expect("xterm-256color")
412 self.handle.expect("\$")
413 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller | grep -A 5 \"commit\"; cd \.\.")
414 self.handle.expect("cd ..")
415 self.handle.expect("\$")
416 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
admine0eeea22014-04-14 10:22:46 -0700417 lines=response.splitlines()
418 for line in lines:
419 print line
420 return lines[2]
Jon Hallae05dc22014-04-16 10:56:28 -0700421 except pexpect.EOF:
422 main.log.error(self.name + ": EOF exception found")
423 main.log.error(self.name + ": " + self.handle.before)
424 main.cleanup()
425 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700426 except:
427 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
428 main.log.error( traceback.print_exc() )
429 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
430 main.cleanup()
431 main.exit()
adminbae64d82013-08-01 10:50:15 -0700432
admin2a9548d2014-06-17 14:08:07 -0700433
434 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):
435 "CLI command callback: set intent"
436
437 intents = []
438 oper = {}
439 # Create the POST payload
440 oper['intentId'] = intent_id
441 oper['intentType'] = intent_type # XXX: Hardcoded
442 oper['staticPath'] = static_path # XXX: Hardcoded
443 oper['srcSwitchDpid'] = src_dpid
444 oper['srcSwitchPort'] = src_port
445 oper['dstSwitchDpid'] = dst_dpid
446 oper['dstSwitchPort'] = dst_port
447 oper['matchSrcMac'] = src_mac
448 oper['matchDstMac'] = dst_mac
449 intents.append(oper)
450 url = "http://%s:%s/%s"%(intentIP,intentPort,intentURL)
451 parsed_result = []
452 data_json = json.dumps(intents)
453 try:
454 request = urllib2.Request(url,data_json)
455 request.add_header("Content-Type", "application/json")
456 response=urllib2.urlopen(request)
457 result = response.read()
458 response.close()
459 if len(result) != 0:
460 parsed_result = json.loads(result)
461 except HTTPError as exc:
462 print "ERROR:"
463 print " REST GET URL: %s" % url
464 # NOTE: exc.fp contains the object with the response payload
465 error_payload = json.loads(exc.fp.read())
466 print " REST Error Code: %s" % (error_payload['code'])
467 print " REST Error Summary: %s" % (error_payload['summary'])
468 print " REST Error Description: %s" % (error_payload['formattedDescription'])
469 print " HTTP Error Code: %s" % exc.code
470 print " HTTP Error Reason: %s" % exc.reason
471 except URLError as exc:
472 print "ERROR:"
473 print " REST GET URL: %s" % url
474 print " URL Error Reason: %s" % exc.reason
475 return parsed_result
476
477
478
479
480 def add_intents(self):
481 main.log.info("Sending new intents to ONOS")
adminc6cfc1c2014-04-21 13:55:21 -0700482 self.handle.sendline("cd "+self.home+ "/scripts")
483 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700484 main.log.info("Adding intents")
adminc6cfc1c2014-04-21 13:55:21 -0700485 self.handle.sendline("./pyintents.py")
Jon Hall9c6e2c02014-05-02 10:18:53 -0700486 self.handle.expect(["$",pexpect.EOF,pexpect.TIMEOUT])
487 response = self.handle.before
adminc6cfc1c2014-04-21 13:55:21 -0700488 self.handle.sendline("cd "+self.home)
489 return main.TRUE
490
admin2a9548d2014-06-17 14:08:07 -0700491 def rm_intents(self):
492 main.log.info("Deleteing Intents from ONOS")
adminc6cfc1c2014-04-21 13:55:21 -0700493 self.handle.sendline("cd "+self.home+ "/scripts")
494 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700495 main.log.info("Deleting Intnents")
adminc6cfc1c2014-04-21 13:55:21 -0700496 self.handle.sendline("./rmpyintents.py")
Jon Hall9c6e2c02014-05-02 10:18:53 -0700497 self.handle.expect(["$",pexpect.EOF,pexpect.TIMEOUT])
498 response = self.handle.before
adminc6cfc1c2014-04-21 13:55:21 -0700499 self.handle.sendline("cd "+self.home)
500 return main.TRUE
501
admin2a9548d2014-06-17 14:08:07 -0700502 def purge_intents(self):
Jon Hall265149f2014-04-25 13:39:52 -0700503 main.log.info("Purging dead intents")
504 self.handle.sendline("cd "+self.home+ "/scripts")
505 self.handle.expect("scripts")
admin2a9548d2014-06-17 14:08:07 -0700506 main.log.info("Sending Purge Intent Rest call to ONOS")
Jon Hall265149f2014-04-25 13:39:52 -0700507 self.handle.sendline("./purgeintents.py")
508 self.handle.sendline("cd "+self.home)
509 return main.TRUE
adminc6cfc1c2014-04-21 13:55:21 -0700510
511
512
Jon Hall4a2b0482014-04-18 16:29:26 -0700513 def add_flow(self, testONip, user = "admin", password = "", flowDef = "/flowdef.txt"):
Jon Halld8dc5772014-04-08 16:26:29 -0700514 '''
515 Copies the flowdef file from TestStation -> ONOS machine
516 Then runs ./add_flow.py to add the flows to ONOS
517 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700518 try:
519 main.log.info("Adding Flows...")
520 self.handle.sendline("scp %s@%s:%s /tmp/flowtmp" %(user,testONip,flowDef))
521 i=self.handle.expect(['[pP]assword:', '100%', pexpect.TIMEOUT],30)
522 if(i==0):
523 self.handle.sendline("%s" %(password))
524 self.handle.sendline("")
525 self.handle.expect("100%")
526 self.handle.expect("\$", 30)
527 self.handle.sendline(self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
528 self.handle.expect("\$", 1000)
529 main.log.info("Flows added")
530 return main.TRUE
531
532 elif(i==1):
533 self.handle.sendline("")
534 self.handle.expect("\$", 10)
535 self.handle.sendline( self.home + "/web/add_flow.py -m onos -f /tmp/flowtmp")
536 self.handle.expect("\$", 1000)
537 main.log.info("Flows added")
538 return main.TRUE
539
540 elif(i==2):
541 main.log.error("Flow Def file SCP Timed out...")
542 return main.FALSE
543
544 else:
545 main.log.error("Failed to add flows...")
James Leec9cacaf2014-04-08 09:17:39 -0700546 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700547 except pexpect.EOF:
548 main.log.error(self.name + ": EOF exception found")
549 main.log.error(self.name + ": " + self.handle.before)
550 main.cleanup()
551 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700552 except:
553 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
554 main.log.error( traceback.print_exc() )
555 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
556 main.cleanup()
557 main.exit()
558
adminbae64d82013-08-01 10:50:15 -0700559
560 def delete_flow(self, *delParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700561 '''
562 Deletes a specific flow, a range of flows, or all flows.
563 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700564 try:
565 if len(delParams)==1:
566 if str(delParams[0])=="all":
567 main.log.info(self.name + ": Deleting ALL flows...")
568 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh all",prompt="done",timeout=150)
569 self.handle.sendline(self.home + "/web/delete_flow.py all")
570 self.handle.expect("delete_flow")
571 self.handle.expect("\$",1000)
572 main.log.info(self.name + ": Flows deleted")
573 else:
574 main.log.info(self.name + ": Deleting flow "+str(delParams[0])+"...")
575 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0]),prompt="done",timeout=150)
576 #self.execute(cmd="\n",prompt="\$",timeout=60)
577 self.handle.sendline(self.home +"/web/delete_flow.py %d" % int(delParams[0]))
578 self.handle.expect("delete_flow")
579 self.handle.expect("\$",60)
580 main.log.info(self.name + ": Flow deleted")
581 elif len(delParams)==2:
582 main.log.info(self.name + ": Deleting flows "+str(delParams[0])+" through "+str(delParams[1])+"...")
583 #self.execute(cmd="~/ONOS/scripts/TestON_delete_flow.sh "+str(delParams[0])+" "+str(delParams[1]),prompt="done",timeout=150)
584 #self.execute(cmd="\n",prompt="\$",timeout=60)
585 self.handle.sendline(self.home + "/web/delete_flow.py %d %d" % (int(delParams[0]), int(delParams[1])))
586 self.handle.expect("delete_flow")
587 self.handle.expect("\$",600)
588 main.log.info(self.name + ": Flows deleted")
Jon Hallae05dc22014-04-16 10:56:28 -0700589 except pexpect.EOF:
590 main.log.error(self.name + ": EOF exception found")
591 main.log.error(self.name + ": " + self.handle.before)
592 main.cleanup()
593 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700594 except:
595 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
596 main.log.error( traceback.print_exc() )
597 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
598 main.cleanup()
599 main.exit()
adminbae64d82013-08-01 10:50:15 -0700600
601 def check_flow(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700602 '''
603 Calls the ./get_flow.py all and checks:
604 - If each FlowPath has at least one FlowEntry
605 - That there are no "NOT"s found
606 returns TRUE/FALSE
607 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700608 try:
609 flowEntryDetect = 1
610 count = 0
611 self.handle.sendline("clear")
612 time.sleep(1)
613 self.handle.sendline(self.home + "/web/get_flow.py all")
614 self.handle.expect("get_flow")
Jon Hall3bd7c792014-05-30 09:05:21 -0700615 for x in range(15):
Jon Hallf89c8552014-04-02 13:14:06 -0700616 i=self.handle.expect(['FlowPath','FlowEntry','NOT','\$',pexpect.TIMEOUT],timeout=180)
617 if i==0:
618 count = count + 1
619 if flowEntryDetect == 0:
620 main.log.info(self.name + ": FlowPath without FlowEntry")
621 return main.FALSE
622 else:
623 flowEntryDetect = 0
624 elif i==1:
625 flowEntryDetect = 1
626 elif i==2:
627 main.log.error(self.name + ": Found a NOT")
adminbae64d82013-08-01 10:50:15 -0700628 return main.FALSE
Jon Hallf89c8552014-04-02 13:14:06 -0700629 elif i==3:
630 if count == 0:
631 main.log.info(self.name + ": There don't seem to be any flows here...")
632 return main.FALSE
633 else:
634 main.log.info(self.name + ": All flows pass")
635 main.log.info(self.name + ": Number of FlowPaths: "+str(count))
636 return main.TRUE
637 elif i==4:
James Leec9cacaf2014-04-08 09:17:39 -0700638 main.log.error(self.name + ":Check_flow() - Command Timeout!")
Jon Hallf89c8552014-04-02 13:14:06 -0700639 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700640 except pexpect.EOF:
641 main.log.error(self.name + ": EOF exception found")
642 main.log.error(self.name + ": " + self.handle.before)
643 main.cleanup()
644 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700645 except:
646 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
647 main.log.error( traceback.print_exc() )
648 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
649 main.cleanup()
650 main.exit()
adminbae64d82013-08-01 10:50:15 -0700651
652 def get_flow(self, *flowParams):
Jon Halld8dc5772014-04-08 16:26:29 -0700653 '''
654 Returns verbose output of ./get_flow.py
655 '''
656 try:
657 if len(flowParams)==1:
658 if str(flowParams[0])=="all":
659 self.execute(cmd="\n",prompt="\$",timeout=60)
660 main.log.info(self.name + ": Getting all flow data...")
661 data = self.execute(cmd=self.home + "/scripts/TestON_get_flow.sh all",prompt="done",timeout=150)
662 self.execute(cmd="\n",prompt="\$",timeout=60)
663 return data
664 else:
665 main.log.info(self.name + ": Retrieving flow "+str(flowParams[0])+" data...")
666 data = self.execute(cmd=self.home +"/scripts/TestON_get_flow.sh "+str(flowParams[0]),prompt="done",timeout=150)
667 self.execute(cmd="\n",prompt="\$",timeout=60)
668 return data
669 elif len(flowParams)==5:
670 main.log.info(self.name + ": Retrieving flow installer data...")
671 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)
672 self.execute(cmd="\n",prompt="\$",timeout=60)
673 return data
674 elif len(flowParams)==4:
675 main.log.info(self.name + ": Retrieving flow endpoints...")
676 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)
677 self.execute(cmd="\n",prompt="\$",timeout=60)
678 return data
Jon Hallae05dc22014-04-16 10:56:28 -0700679 except pexpect.EOF:
680 main.log.error(self.name + ": EOF exception found")
681 main.log.error(self.name + ": " + self.handle.before)
682 main.cleanup()
683 main.exit()
Jon Halld8dc5772014-04-08 16:26:29 -0700684 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700685 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
686 main.log.error( traceback.print_exc() )
687 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
688 main.cleanup()
689 main.exit()
adminbae64d82013-08-01 10:50:15 -0700690
691
Jon Hall2f42a672014-05-28 10:13:18 -0700692# http://localhost:8080/wm/onos/topology/switches
693# http://localhost:8080/wm/onos/topology/links
Jon Hall8060af02014-04-14 14:17:58 -0700694# http://localhost:8080/wm/onos/registry/controllers/json
695# http://localhost:8080/wm/onos/registry/switches/json"
adminbae64d82013-08-01 10:50:15 -0700696
697 def get_json(self, url):
Jon Halld8dc5772014-04-08 16:26:29 -0700698 '''
699 Helper functions used to parse the json output of a rest call
700 '''
adminbae64d82013-08-01 10:50:15 -0700701 try:
Jon Hallf89c8552014-04-02 13:14:06 -0700702 try:
703 command = "curl -s %s" % (url)
704 result = os.popen(command).read()
705 parsedResult = json.loads(result)
706 except:
707 print "REST IF %s has issue" % command
708 parsedResult = ""
709
710 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
711 print "REST %s returned code %s" % (command, parsedResult['code'])
712 parsedResult = ""
James Leec9cacaf2014-04-08 09:17:39 -0700713 return parsedResult
Jon Hallae05dc22014-04-16 10:56:28 -0700714 except pexpect.EOF:
715 main.log.error(self.name + ": EOF exception found")
716 main.log.error(self.name + ": " + self.handle.before)
717 main.cleanup()
718 main.exit()
adminbae64d82013-08-01 10:50:15 -0700719 except:
Jon Hallf89c8552014-04-02 13:14:06 -0700720 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
721 main.log.error( traceback.print_exc() )
722 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
723 main.cleanup()
724 main.exit()
adminbae64d82013-08-01 10:50:15 -0700725
Jon Hallf89c8552014-04-02 13:14:06 -0700726 def check_switch(self,RestIP,correct_nr_switch, RestPort ="8080" ):
Jon Halld8dc5772014-04-08 16:26:29 -0700727 '''
728 Used by check_status
729 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700730 try:
731 buf = ""
732 retcode = 0
admin2e131ab2014-05-28 10:03:42 -0700733 url="http://%s:%s/wm/onos/topology/switches" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700734 parsedResult = self.get_json(url)
735 if parsedResult == "":
736 retcode = 1
737 return (retcode, "Rest API has an issue")
738 url = "http://%s:%s/wm/onos/registry/switches/json" % (RestIP, RestPort)
739 registry = self.get_json(url)
740
741 if registry == "":
742 retcode = 1
743 return (retcode, "Rest API has an issue")
744
745 cnt = 0
746 active = 0
adminbae64d82013-08-01 10:50:15 -0700747
Jon Hallf89c8552014-04-02 13:14:06 -0700748 for s in parsedResult:
749 cnt += 1
James Leec9cacaf2014-04-08 09:17:39 -0700750 if s['state'] == "ACTIVE":
Jon Hallf89c8552014-04-02 13:14:06 -0700751 active += 1
adminbae64d82013-08-01 10:50:15 -0700752
Jon Hallf89c8552014-04-02 13:14:06 -0700753 buf += "switch: network %d : %d switches %d active\n" % (0+1, cnt, active)
754 if correct_nr_switch != cnt:
755 buf += "switch fail: network %d should have %d switches but has %d\n" % (1, correct_nr_switch, cnt)
756 retcode = 1
adminbae64d82013-08-01 10:50:15 -0700757
Jon Hallf89c8552014-04-02 13:14:06 -0700758 if correct_nr_switch != active:
759 buf += "switch fail: network %d should have %d active switches but has %d\n" % (1, correct_nr_switch, active)
760 retcode = 1
761
762 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700763 except pexpect.EOF:
764 main.log.error(self.name + ": EOF exception found")
765 main.log.error(self.name + ": " + self.handle.before)
766 main.cleanup()
767 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700768 except:
769 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
770 main.log.error( traceback.print_exc() )
771 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
772 main.cleanup()
773 main.exit()
adminbae64d82013-08-01 10:50:15 -0700774
Jon Hallf89c8552014-04-02 13:14:06 -0700775 def check_link(self,RestIP, nr_links, RestPort = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700776 '''
777 Used by check_status
778 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700779 try:
780 buf = ""
781 retcode = 0
782
admin2e131ab2014-05-28 10:03:42 -0700783 url = "http://%s:%s/wm/onos/topology/links" % (RestIP, RestPort)
Jon Hallf89c8552014-04-02 13:14:06 -0700784 parsedResult = self.get_json(url)
785
786 if parsedResult == "":
787 retcode = 1
788 return (retcode, "Rest API has an issue")
789
790 buf += "link: total %d links (correct : %d)\n" % (len(parsedResult), nr_links)
791 intra = 0
792 interlink=0
793
794 for s in parsedResult:
James Leec9cacaf2014-04-08 09:17:39 -0700795 intra = intra + 1
Jon Hallf89c8552014-04-02 13:14:06 -0700796
797 if intra != nr_links:
798 buf += "link fail\n"
799 retcode = 1
800
801 return (retcode, buf)
Jon Hallae05dc22014-04-16 10:56:28 -0700802 except pexpect.EOF:
803 main.log.error(self.name + ": EOF exception found")
804 main.log.error(self.name + ": " + self.handle.before)
805 main.cleanup()
806 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700807 except:
808 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
809 main.log.error( traceback.print_exc() )
810 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
811 main.cleanup()
812 main.exit()
adminbae64d82013-08-01 10:50:15 -0700813
Jon Hallf89c8552014-04-02 13:14:06 -0700814 def check_status_report(self, ip, numoswitch, numolink, port="8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700815 '''
816 Checks the number of swithes & links that ONOS sees against the supplied values.
817 Writes to the report log.
818 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700819 try:
James Leec9cacaf2014-04-08 09:17:39 -0700820 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700821 switch = self.check_switch(ip, int(numoswitch), port)
822 link = self.check_link(ip, int(numolink), port)
823 value = switch[0]
824 value += link[0]
825 main.log.report( self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
826 if value != 0:
827 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700828 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700829 # "PASS"
830 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700831 except pexpect.EOF:
832 main.log.error(self.name + ": EOF exception found")
833 main.log.error(self.name + ": " + self.handle.before)
834 main.cleanup()
835 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700836 except:
837 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
838 main.log.error( traceback.print_exc() )
839 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
840 main.cleanup()
841 main.exit()
adminbae64d82013-08-01 10:50:15 -0700842
Jon Hallf89c8552014-04-02 13:14:06 -0700843 def check_status(self, ip, numoswitch, numolink, port = "8080"):
Jon Halld8dc5772014-04-08 16:26:29 -0700844 '''
845 Checks the number of swithes & links that ONOS sees against the supplied values.
846 Writes to the main log.
847 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700848 try:
James Leec9cacaf2014-04-08 09:17:39 -0700849 main.log.info(self.name + ": Making some rest calls...")
Jon Hallf89c8552014-04-02 13:14:06 -0700850 switch = self.check_switch(ip, int(numoswitch), port)
851 link = self.check_link(ip, int(numolink), port)
852 value = switch[0]
853 value += link[0]
854 main.log.info(self.name + ": \n-----\n%s%s-----\n" % ( switch[1], link[1]) )
855 if value != 0:
856 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -0700857 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700858 # "PASS"
859 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700860 except pexpect.EOF:
861 main.log.error(self.name + ": EOF exception found")
862 main.log.error(self.name + ": " + self.handle.before)
863 main.cleanup()
864 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700865 except:
866 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
867 main.log.error( traceback.print_exc() )
868 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
869 main.cleanup()
870 main.exit()
adminbae64d82013-08-01 10:50:15 -0700871
adminbae64d82013-08-01 10:50:15 -0700872 def check_for_no_exceptions(self):
Jon Halld8dc5772014-04-08 16:26:29 -0700873 '''
874 TODO: Rewrite
875 Used by CassndraCheck.py to scan ONOS logs for Exceptions
876 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700877 try:
878 self.handle.sendline("dsh 'grep Exception ~/ONOS/onos-logs/onos.*.log'")
James Leec9cacaf2014-04-08 09:17:39 -0700879 self.handle.expect("\$ dsh")
Jon Hallf89c8552014-04-02 13:14:06 -0700880 self.handle.expect("\$")
881 output = self.handle.before
James Leec9cacaf2014-04-08 09:17:39 -0700882 main.log.info(self.name + ": " + output )
Jon Hallf89c8552014-04-02 13:14:06 -0700883 if re.search("Exception",output):
884 return main.FALSE
885 else :
886 return main.TRUE
Jon Hallae05dc22014-04-16 10:56:28 -0700887 except pexpect.EOF:
888 main.log.error(self.name + ": EOF exception found")
889 main.log.error(self.name + ": " + self.handle.before)
890 main.cleanup()
891 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -0700892 except:
893 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
894 main.log.error( traceback.print_exc() )
895 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
896 main.cleanup()
897 main.exit()
898
899
admine0eeea22014-04-14 10:22:46 -0700900 def git_pull(self, comp1=""):
Jon Halld8dc5772014-04-08 16:26:29 -0700901 '''
Jon Halld8dc5772014-04-08 16:26:29 -0700902 Assumes that "git pull" works without login
admine0eeea22014-04-14 10:22:46 -0700903
904 This function will perform a git pull on the ONOS instance.
905 If used as git_pull("NODE") it will do git pull + NODE. This is
906 for the purpose of pulling from other nodes if necessary.
907
908 Otherwise, this function will perform a git pull in the
909 ONOS repository. If it has any problems, it will return main.ERROR
910 If it successfully does a git_pull, it will return a 1.
911 If it has no updates, it will return a 0.
912
Jon Halld8dc5772014-04-08 16:26:29 -0700913 '''
Jon Hallae05dc22014-04-16 10:56:28 -0700914 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700915 # main.log.info(self.name + ": Stopping ONOS")
916 #self.stop()
Jon Hallae05dc22014-04-16 10:56:28 -0700917 self.handle.sendline("cd " + self.home)
918 self.handle.expect("ONOS\$")
919 if comp1=="":
920 self.handle.sendline("git pull")
921 else:
922 self.handle.sendline("git pull " + comp1)
923
924 uptodate = 0
Jon Hall5a8aac62014-06-03 09:30:21 -0700925 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 -0700926 #debug
927 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
928 if i==0:
929 main.log.error(self.name + ": Git pull had some issue...")
930 return main.ERROR
931 elif i==1:
932 main.log.error(self.name + ": Git Pull Asking for username!!! BADD!")
933 return main.ERROR
934 elif i==2:
935 main.log.info(self.name + ": Git Pull - pulling repository now")
936 self.handle.expect("ONOS\$", 120)
937 return 0
938 elif i==3:
939 main.log.error(self.name + ": Git Pull - TIMEOUT")
940 return main.ERROR
941 elif i==4:
942 main.log.info(self.name + ": Git Pull - Already up to date")
943 return 1
944 elif i==5:
945 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
946 return main.ERROR
Jon Hall5a8aac62014-06-03 09:30:21 -0700947 elif i==6:
948 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
949 return main.ERROR
Jon Hallae05dc22014-04-16 10:56:28 -0700950 else:
951 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
952 return main.ERROR
953 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()
958 except:
959 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
960 main.log.error( traceback.print_exc() )
961 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
962 main.cleanup()
963 main.exit()
admine0eeea22014-04-14 10:22:46 -0700964#********************************************************
965
966
admin7373a812014-04-16 09:49:02 -0700967 def git_compile(self):
Jon Hallae05dc22014-04-16 10:56:28 -0700968 '''
969 Compiles ONOS
970 First runs mvn clean then mvn compile
971 '''
972 try:
973 main.log.info(self.name + ": mvn clean")
admin8fc02822014-04-16 13:31:23 -0700974 self.handle.sendline("cd " + self.home)
Jon Hallae05dc22014-04-16 10:56:28 -0700975 self.handle.sendline("mvn clean")
976 while 1:
Jon Hall1636f942014-04-17 10:07:23 -0700977 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 -0700978 if i == 0:
Jon Hall1636f942014-04-17 10:07:23 -0700979 main.log.error(self.name + ":There is insufficient memory for the Java Runtime Environment to continue.")
Jon Hallae05dc22014-04-16 10:56:28 -0700980 return main.FALSE
981 elif i == 1:
Jon Hall1636f942014-04-17 10:07:23 -0700982 main.log.error(self.name + ": Clean failure!")
983 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700984 elif i == 2:
Jon Hall1636f942014-04-17 10:07:23 -0700985 main.log.info(self.name + ": Clean success!")
986 elif i == 3:
admin8fc02822014-04-16 13:31:23 -0700987 main.log.info(self.name + ": Clean complete")
Jon Hallae05dc22014-04-16 10:56:28 -0700988 break;
Jon Hall1636f942014-04-17 10:07:23 -0700989 elif i == 4:
Jon Hallae05dc22014-04-16 10:56:28 -0700990 main.log.error(self.name + ": mvn clean TIMEOUT!")
991 return main.FALSE
Jon Hall1636f942014-04-17 10:07:23 -0700992 else:
993 main.log.error(self.name + ": unexpected response from mvn clean")
994 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -0700995
996 main.log.info(self.name + ": mvn compile")
997 self.handle.sendline("mvn compile")
998 while 1:
Jon Hall1636f942014-04-17 10:07:23 -0700999 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 -07001000 if i == 0:
Jon Hall1636f942014-04-17 10:07:23 -07001001 main.log.error(self.name + ":There is insufficient memory for the Java Runtime Environment to continue.")
1002 return main.FALSE
1003 if i == 1:
Jon Hallae05dc22014-04-16 10:56:28 -07001004 main.log.error(self.name + ": Build failure!")
1005 return main.FALSE
Jon Hall1636f942014-04-17 10:07:23 -07001006 elif i == 2:
Jon Hallae05dc22014-04-16 10:56:28 -07001007 main.log.info(self.name + ": Build success!")
Jon Hall1636f942014-04-17 10:07:23 -07001008 elif i == 3:
Jon Hallae05dc22014-04-16 10:56:28 -07001009 main.log.info(self.name + ": Build complete")
Jon Hall010f5412014-05-21 15:10:12 -07001010 self.handle.expect("\$", timeout=60)
Jon Hallae05dc22014-04-16 10:56:28 -07001011 return main.TRUE
Jon Hall1636f942014-04-17 10:07:23 -07001012 elif i == 4:
Jon Hallae05dc22014-04-16 10:56:28 -07001013 main.log.error(self.name + ": mvn compile TIMEOUT!")
1014 return main.FALSE
1015 else:
Jon Hall1636f942014-04-17 10:07:23 -07001016 main.log.error(self.name + ": unexpected response from mvn compile")
1017 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -07001018 except pexpect.EOF:
1019 main.log.error(self.name + ": EOF exception found")
1020 main.log.error(self.name + ": " + self.handle.before)
1021 main.cleanup()
1022 main.exit()
1023 except:
1024 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1025 main.log.error( traceback.print_exc() )
1026 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1027 main.cleanup()
1028 main.exit()
admin4a58db92013-09-30 12:04:54 -07001029
Jon Hallf89c8552014-04-02 13:14:06 -07001030 def tcpdump(self, intf = "eth0"):
Jon Hallae05dc22014-04-16 10:56:28 -07001031 '''
1032 Runs tpdump on an intferface and saves in onos-logs under the ONOS home directory
1033 intf can be specified, or the default eth0 is used
1034 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001035 try:
1036 self.handle.sendline("")
1037 self.handle.expect("\$")
Jon Hall26f316b2014-04-11 11:21:25 -07001038 self.handle.sendline("sudo tcpdump -n -i "+ intf + " -s0 -w " + self.home +"/onos-logs/tcpdump &")
Jon Hallf89c8552014-04-02 13:14:06 -07001039 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT],timeout=10)
1040 if i == 0:
1041 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
1042 return main.FALSE
James Leec9cacaf2014-04-08 09:17:39 -07001043 elif i == 1:
Jon Hallf89c8552014-04-02 13:14:06 -07001044 main.log.info(self.name + ": tcpdump started on " + intf)
1045 return main.TRUE
James Leec9cacaf2014-04-08 09:17:39 -07001046 elif i == 2:
Jon Hallf89c8552014-04-02 13:14:06 -07001047 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
1048 return main.FALSE
1049 else:
1050 main.log.error(self.name + ": tcpdump - unexpected response")
1051 return main.FALSE
Jon Hallae05dc22014-04-16 10:56:28 -07001052 except pexpect.EOF:
1053 main.log.error(self.name + ": EOF exception found")
1054 main.log.error(self.name + ": " + self.handle.before)
1055 main.cleanup()
1056 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001057 except:
1058 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1059 main.log.error( traceback.print_exc() )
1060 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1061 main.cleanup()
1062 main.exit()
1063
admin4a58db92013-09-30 12:04:54 -07001064 def kill_tcpdump(self):
Jon Hallae05dc22014-04-16 10:56:28 -07001065 '''
1066 Kills any tcpdump processes running
1067 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001068 try:
1069 self.handle.sendline("")
1070 self.handle.expect("\$")
1071 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tcpdump -n\" | grep -v grep | awk '{print $2}'`")
Jon Hallae05dc22014-04-16 10:56:28 -07001072 except pexpect.EOF:
1073 main.log.error(self.name + ": EOF exception found")
1074 main.log.error(self.name + ": " + self.handle.before)
1075 main.cleanup()
1076 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001077 except:
1078 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1079 main.log.error( traceback.print_exc() )
1080 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1081 main.cleanup()
1082 main.exit()
1083
Jon Hallf15f7852014-05-20 10:37:23 -07001084 def find_host(self,RestIP,RestPort,RestAPI,hostMAC):
admin2a9548d2014-06-17 14:08:07 -07001085 retcode = 0 # number of hosts found with given MAC
1086 retswitch = [] # Switch DPID's of hosts found with MAC
1087 retport = [] # Switch port connected to to hosts found with MAC
Jon Hallf15f7852014-05-20 10:37:23 -07001088 foundHost = []
Jon Hallf89c8552014-04-02 13:14:06 -07001089 try:
Jon Hall2f42a672014-05-28 10:13:18 -07001090 ##### device rest API is: 'host:8080/wm/onos/topology/switches' ###
Jon Hallf89c8552014-04-02 13:14:06 -07001091 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
1092
1093 try:
1094 command = "curl -s %s" % (url)
1095 result = os.popen(command).read()
1096 parsedResult = json.loads(result)
1097 # print parsedResult
1098 except:
1099 print "REST IF %s has issue" % command
1100 parsedResult = ""
1101
1102 if parsedResult == "":
Jon Hallf15f7852014-05-20 10:37:23 -07001103 return (retcode, "Rest API has an error", retport)
Jon Hallf89c8552014-04-02 13:14:06 -07001104 else:
Jon Hallf15f7852014-05-20 10:37:23 -07001105 for host in enumerate(parsedResult):
1106 print host
1107 if (host[1] != []):
1108 try:
1109 foundHost = host[1]['mac']
1110 except:
1111 print "Error in detecting MAC address."
1112 print foundHost
1113 print hostMAC
1114 if foundHost == hostMAC:
1115 for switch in enumerate(host[1]['attachmentPoints']):
1116 retswitch.append(switch[1]['dpid'])
1117 retport.append(switch[1]['port'])
1118 retcode = retcode +1
1119 foundHost =''
1120 '''
Jon Hallf89c8552014-04-02 13:14:06 -07001121 for switch in enumerate(parsedResult):
1122 for port in enumerate(switch[1]['ports']):
admin2a9548d2014-06-17 14:08:07 -07001123 if ( port[1]['hosts'] != [] ):
Jon Hallf89c8552014-04-02 13:14:06 -07001124 try:
admin2a9548d2014-06-17 14:08:07 -07001125 foundHost = port[1]['hosts'][0]['ipv4addresses'][0]['ipv4']
Jon Hallf89c8552014-04-02 13:14:06 -07001126 except:
Jon Hallf15f7852014-05-20 10:37:23 -07001127 print "Error in detecting MAC address."
1128 if foundHost == hostMAC:
Jon Hallf89c8552014-04-02 13:14:06 -07001129 retswitch.append(switch[1]['dpid'])
1130 retport.append(port[1]['desc'])
admin2a9548d2014-06-17 14:08:07 -07001131 retmac.append(port[1]['hosts'][0]['mac'])
Jon Hallf89c8552014-04-02 13:14:06 -07001132 retcode = retcode +1
Jon Hallf15f7852014-05-20 10:37:23 -07001133 foundHost =''
1134 '''
1135 return(retcode, retswitch, retport)
Jon Hallae05dc22014-04-16 10:56:28 -07001136 except pexpect.EOF:
1137 main.log.error(self.name + ": EOF exception found")
1138 main.log.error(self.name + ": " + self.handle.before)
1139 main.cleanup()
1140 main.exit()
Jon Hallf89c8552014-04-02 13:14:06 -07001141 except:
1142 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1143 main.log.error( traceback.print_exc() )
1144 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1145 main.cleanup()
1146 main.exit()
SeanCorcoran29b70542014-05-14 14:53:42 -07001147
1148 def check_exceptions(self):
1149 '''
1150 Greps the logs for "xception"
1151 '''
1152 try:
1153 output = ''
1154 self.handle.sendline("")
Jon Halla78cf9a2014-05-16 10:49:30 -07001155 i = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
1156 #main.log.warn("first expect response: " +str(i))
SeanCorcoran29b70542014-05-14 14:53:42 -07001157 self.handle.sendline("cd "+self.home+"/onos-logs")
Jon Hall724a2f42014-06-23 16:04:22 -07001158 self.handle.sendline("zgrep \"xception\" *")
Jon Halla78cf9a2014-05-16 10:49:30 -07001159 i = self.handle.expect(["\*",pexpect.EOF,pexpect.TIMEOUT])
1160 #main.log.warn("second expect response: " +str(i))
Jon Hall724a2f42014-06-23 16:04:22 -07001161 i = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT],timeout=120)
Jon Halla78cf9a2014-05-16 10:49:30 -07001162 #main.log.warn("third expect response: " +str(i))
SeanCorcoran29b70542014-05-14 14:53:42 -07001163 response = self.handle.before
Jon Halla78cf9a2014-05-16 10:49:30 -07001164 count = 0
SeanCorcoran29b70542014-05-14 14:53:42 -07001165 for line in response.splitlines():
1166 if re.search("log:", line):
1167 output +="Exceptions found in " + line + "\n"
Jon Halla78cf9a2014-05-16 10:49:30 -07001168 count +=1
Jon Hall724a2f42014-06-23 16:04:22 -07001169 elif re.search("log\.gz:",line):
1170 output+="Exceptions found in " + line + "\n"
1171 count +=1
SeanCorcoran29b70542014-05-14 14:53:42 -07001172 elif re.search("std...:",line):
1173 output+="Exceptions found in " + line + "\n"
Jon Halla78cf9a2014-05-16 10:49:30 -07001174 count +=1
SeanCorcoran29b70542014-05-14 14:53:42 -07001175 else:
1176 pass
1177 #these should be the old logs
admin1723f1c2014-05-19 16:08:39 -07001178 main.log.report(str(count) + " Exceptions were found on "+self.name)
SeanCorcoran29b70542014-05-14 14:53:42 -07001179 return output
1180 except pexpect.TIMEOUT:
1181 main.log.error(self.name + ": Timeout exception found in check_exceptions function")
1182 except pexpect.EOF:
1183 main.log.error(self.name + ": EOF exception found")
1184 main.log.error(self.name + ": " + self.handle.before)
1185 main.cleanup()
1186 main.exit()
1187 except:
1188 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1189 main.log.error( traceback.print_exc() )
1190 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
1191 main.cleanup()
1192 main.exit()