blob: 1d45f8ad317a04b946c804e9320e06e81f95591e [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
3'''
4This driver enters the onos> prompt to issue commands.
5
6Please follow the coding style demonstrated by existing
7functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
14
15OCT 13 2014
16
17'''
18
19import sys
20import time
21import pexpect
22import re
23import traceback
24import os.path
25import pydoc
26sys.path.append("../")
27from drivers.common.clidriver import CLI
28
29class OnosCliDriver(CLI):
30
31 def __init__(self):
32 '''
33 Initialize client
34 '''
35 super(CLI, self).__init__()
36
37 def connect(self,**connectargs):
38 '''
39 Creates ssh handle for ONOS cli.
40 '''
41 try:
42 for key in connectargs:
43 vars(self)[key] = connectargs[key]
44 self.home = "~/ONOS"
45 for key in self.options:
46 if key == "home":
47 self.home = self.options['home']
48 break
49
50
51 self.name = self.options['name']
52 self.handle = super(OnosCliDriver,self).connect(
53 user_name = self.user_name,
54 ip_address = self.ip_address,
55 port = self.port,
56 pwd = self.pwd,
57 home = self.home)
58
59 self.handle.sendline("cd "+ self.home)
60 self.handle.expect("\$")
61 if self.handle:
62 return self.handle
63 else :
64 main.log.info("NO ONOS HANDLE")
65 return main.FALSE
66 except pexpect.EOF:
67 main.log.error(self.name + ": EOF exception found")
68 main.log.error(self.name + ": " + self.handle.before)
69 main.cleanup()
70 main.exit()
71 except:
72 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
73 main.log.error( traceback.print_exc() )
74 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
75 main.cleanup()
76 main.exit()
77
78 def disconnect(self):
79 '''
80 Called when Test is complete to disconnect the ONOS handle.
81 '''
82 response = ''
83 try:
andrewonlabc2d05aa2014-10-13 16:51:10 -040084 self.handle.sendline("system:shutdown")
85 self.handle.expect("Confirm")
86 self.handle.sendline("Yes")
87 self.handle.expect("\$")
88
andrewonlab95ce8322014-10-13 14:12:04 -040089 except pexpect.EOF:
90 main.log.error(self.name + ": EOF exception found")
91 main.log.error(self.name + ": " + self.handle.before)
92 except:
93 main.log.error(self.name + ": Connection failed to the host")
94 response = main.FALSE
95 return response
96
97 def set_cell(self, cellname):
98 '''
99 Calls 'cell <name>' to set the environment variables on ONOSbench
100
101 Before issuing any cli commands, set the environment variable first.
102 '''
103 try:
104 if not cellname:
105 main.log.error("Must define cellname")
106 main.cleanup()
107 main.exit()
108 else:
109 self.handle.sendline("cell "+str(cellname))
110 #Expect the cellname in the ONOS_CELL variable.
111 #Note that this variable name is subject to change
112 # and that this driver will have to change accordingly
113 self.handle.expect("ONOS_CELL="+str(cellname))
114 handle_before = self.handle.before
115 handle_after = self.handle.after
116 #Get the rest of the handle
117 self.handle.sendline("")
118 self.handle.expect("\$")
119 handle_more = self.handle.before
120
121 main.log.info("Cell call returned: "+handle_before+
122 handle_after + handle_more)
123
124 return main.TRUE
125
126 except pexpect.EOF:
127 main.log.error(self.name + ": EOF exception found")
128 main.log.error(self.name + ": " + self.handle.before)
129 main.cleanup()
130 main.exit()
131 except:
132 main.log.info(self.name+" ::::::")
133 main.log.error( traceback.print_exc())
134 main.log.info(self.name+" ::::::")
135 main.cleanup()
136 main.exit()
137
andrewonlabc2d05aa2014-10-13 16:51:10 -0400138 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400139 try:
140 self.handle.sendline("")
141 self.handle.expect("\$")
142
143 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400144 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab95ce8322014-10-13 14:12:04 -0400145 self.handle.expect("onos>")
146
147 except pexpect.EOF:
148 main.log.error(self.name + ": EOF exception found")
149 main.log.error(self.name + ": " + self.handle.before)
150 main.cleanup()
151 main.exit()
152 except:
153 main.log.info(self.name+" ::::::")
154 main.log.error( traceback.print_exc())
155 main.log.info(self.name+" ::::::")
156 main.cleanup()
157 main.exit()
158
159 #IMPORTANT NOTE:
160 #For all cli commands, naming convention should match
161 #the cli command replacing ':' with '_'.
162 #Ex) onos:topology > onos_topology
163 # onos:links > onos_links
164 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400165
166 def add_node(self, node_id, ONOS_ip, tcp_port=""):
167 '''
168 Adds a new cluster node by ID and address information.
169 Required:
170 * node_id
171 * ONOS_ip
172 Optional:
173 * tcp_port
174 '''
175 try:
176 self.handle.sendline("")
177 self.handle.expect("onos>")
178
179 self.handle.sendline("add-node "+
180 str(node_id)+" "+
181 str(ONOS_ip)+" "+
182 str(tcp_port))
183
184 i = self.handle.expect([
185 "Error",
186 "onos>" ])
187
188 #Clear handle to get previous output
189 self.handle.sendline("")
190 self.handle.expect("onos>")
191
192 handle = self.handle.before
193
194 if i == 0:
195 main.log.error("Error in adding node")
196 main.log.error(handle)
197 return main.FALSE
198 else:
199 main.log.info("Node "+str(ONOS_ip)+" added")
200 return main.TRUE
201
202 except pexpect.EOF:
203 main.log.error(self.name + ": EOF exception found")
204 main.log.error(self.name + ": " + self.handle.before)
205 main.cleanup()
206 main.exit()
207 except:
208 main.log.info(self.name+" ::::::")
209 main.log.error( traceback.print_exc())
210 main.log.info(self.name+" ::::::")
211 main.cleanup()
212 main.exit()
213
andrewonlab86dc3082014-10-13 18:18:38 -0400214 def remove_node(self, node_id):
215 '''
216 Removes a cluster by ID
217 Issues command: 'remove-node [<node-id>]'
218 Required:
219 * node_id
220 '''
221 try:
222 self.handle.sendline("")
223 self.handle.expect("onos>")
224
225 self.handle.sendline("remove-node "+str(node_id))
226 self.handle.expect("onos>")
227
228 return main.TRUE
229
230 except pexpect.EOF:
231 main.log.error(self.name + ": EOF exception found")
232 main.log.error(self.name + ": " + self.handle.before)
233 main.cleanup()
234 main.exit()
235 except:
236 main.log.info(self.name+" ::::::")
237 main.log.error( traceback.print_exc())
238 main.log.info(self.name+" ::::::")
239 main.cleanup()
240 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400241
242 def onos_topology(self):
243 '''
244 Shows the current state of the topology
245 by issusing command: 'onos> onos:topology'
246 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400247 try:
248 self.handle.sendline("")
249 self.handle.expect("onos>")
250 self.handle.sendline("onos:topology")
251 self.handle.expect("onos>")
252
253 handle = self.handle.before
254
255 main.log.info("onos:topology returned: " +
256 str(handle))
257
258 return handle
259
260 except pexpect.EOF:
261 main.log.error(self.name + ": EOF exception found")
262 main.log.error(self.name + ": " + self.handle.before)
263 main.cleanup()
264 main.exit()
265 except:
266 main.log.info(self.name+" ::::::")
267 main.log.error( traceback.print_exc())
268 main.log.info(self.name+" ::::::")
269 main.cleanup()
270 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400271
272 def feature_install(self, feature_str):
273 '''
274 Installs a specified feature
275 by issuing command: 'onos> feature:install <feature_str>'
276 '''
277 try:
278 self.handle.sendline("")
279 self.handle.expect("onos>")
280
281 self.handle.sendline("feature:install "+str(feature_str))
282 self.handle.expect("onos>")
283
284 return main.TRUE
285
286 except pexpect.EOF:
287 main.log.error(self.name + ": EOF exception found")
288 main.log.error(self.name + ": " + self.handle.before)
289 main.cleanup()
290 main.exit()
291 except:
292 main.log.info(self.name+" ::::::")
293 main.log.error( traceback.print_exc())
294 main.log.info(self.name+" ::::::")
295 main.cleanup()
296 main.exit()
297
298 def feature_uninstall(self, feature_str):
299 '''
300 Uninstalls a specified feature
301 by issuing command: 'onos> feature:uninstall <feature_str>'
302 '''
303 try:
304 self.handle.sendline("")
305 self.handle.expect("onos>")
306
307 self.handle.sendline("feature:uninstall "+str(feature_str))
308 self.handle.expect("onos>")
309
310 return main.TRUE
311
312 except pexpect.EOF:
313 main.log.error(self.name + ": EOF exception found")
314 main.log.error(self.name + ": " + self.handle.before)
315 main.cleanup()
316 main.exit()
317 except:
318 main.log.info(self.name+" ::::::")
319 main.log.error( traceback.print_exc())
320 main.log.info(self.name+" ::::::")
321 main.cleanup()
322 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400323
andrewonlab86dc3082014-10-13 18:18:38 -0400324 def devices(self, grep_str=""):
325 '''
326 Lists all infrastructure devices
327 Optional argument:
328 * grep_str - pass in a string to grep
329 '''
330 try:
331 self.handle.sendline("")
332 self.handle.expect("onos>")
333
334 if not grep_str:
335 self.handle.sendline("devices")
336 self.handle.expect("onos>")
337 else:
338 self.handle.sendline("devices | grep '"+
339 str(grep_str)+"'")
340 self.handle.expect("onos>")
341
342 handle = self.handle.before
343 handle += self.handle.after
344
345 self.handle.sendline("")
346 self.handle.expect("onos>")
347
348 handle += self.handle.before
349 handle += self.handle.after
350
351 return handle
352 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()
357 except:
358 main.log.info(self.name+" ::::::")
359 main.log.error( traceback.print_exc())
360 main.log.info(self.name+" ::::::")
361 main.cleanup()
362 main.exit()
363
andrewonlabc2d05aa2014-10-13 16:51:10 -0400364