blob: d7495b5606ebea5c26cb72f28e3a4b1ed47a07a4 [file] [log] [blame]
Steve McIntyre844bfd42014-11-27 16:58:31 +00001#! /usr/bin/python
2
3# Copyright 2014 Linaro Limited
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18# MA 02110-1301, USA.
19#
20# VLANd admin interface
21#
22
Steve McIntyre5694f6d2014-12-18 16:43:30 +000023import os, sys
Steve McIntyre844bfd42014-11-27 16:58:31 +000024import optparse
25
26vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
27sys.path.insert(0, vlandpath)
28
Steve McIntyre5694f6d2014-12-18 16:43:30 +000029from errors import InputError, SocketError
Steve McIntyre21f02702014-12-16 18:19:47 +000030from config.config import VlanConfig
31from ipc.ipc import VlanIpc
Steve McIntyre844bfd42014-11-27 16:58:31 +000032
Steve McIntyre9d897182014-12-22 17:51:08 +000033version = "0.1"
Steve McIntyre844bfd42014-11-27 16:58:31 +000034banner = "Linaro VLANd admin interface, version %s" % version
35
Steve McIntyreae95fd62014-12-05 16:51:41 +000036def is_positive(text):
Steve McIntyrec6895fc2014-12-19 15:01:08 +000037 if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000038 return True
Steve McIntyrec6895fc2014-12-19 15:01:08 +000039 elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000040 return False
41 else:
42 raise InputError("Cannot parse \"%s\" as True or False" % text)
43
Steve McIntyrea132c362014-12-05 15:53:21 +000044def dump_switch(switch):
45 print switch
46
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000047def dump_port(port):
48 print port
49
Steve McIntyree41e3f32014-12-05 18:08:21 +000050def dump_vlan(vlan):
51 print vlan
52
Steve McIntyref1c04f92014-12-16 18:23:15 +000053def call_vland(msgtype, msg):
54 ipc = VlanIpc()
55 ipc.client_connect('localhost', config.vland.port)
56 msg['client_name'] = 'admin.py'
57 msg['type'] = msgtype
58# print 'Sending:'
59# print msg
60 ipc.client_send(msg)
61 ret = ipc.client_recv_and_close()
62 if 'response' not in ret:
63 raise SocketError("Badly-formed response from VLANd server")
64 if ret['response'] == "ERROR":
65 raise InputError("VLANd server said: %s" % ret['error'])
66# print 'VLANd said in reply:'
67# print ret
68 return ret['data']
69
Steve McIntyre844bfd42014-11-27 16:58:31 +000070#print '%s' % banner
71
Steve McIntyrec12f6312014-12-15 15:00:12 +000072config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000073
Steve McIntyre844bfd42014-11-27 16:58:31 +000074usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000075parser = optparse.OptionParser(usage=usage, description=banner)
76
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000077# System commands
78system_group = optparse.OptionGroup(parser, "System commands")
79system_group.add_option("--status",
80 dest="status",
81 action = "store_true",
82 default = False,
83 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +000084system_group.add_option("--statistics",
85 dest="statistics",
86 action = "store_true",
87 default = False,
88 help = "Print some system statistics")
89system_group.add_option("--version",
90 dest="version",
91 action = "store_true",
92 default = False,
93 help = "Describe the version of this admin interface")
94system_group.add_option("--vland_version",
95 dest="vland_version",
96 action = "store_true",
97 default = False,
98 help = "Describe the version of the running VLANd")
Steve McIntyre6c562602014-12-22 16:10:54 +000099system_group.add_option("--auto-import-switch",
100 dest = "auto_import_switch",
101 action = "store",
102 type = "string",
103 help = "Attempt to import a switch's configuration into the VLANd system",
104 nargs = 1,
105 metavar = "<switch name>")
Steve McIntyredeff7952014-12-23 13:45:59 +0000106system_group.add_option("--probe-switches",
107 dest = "probe_switches",
108 action = "store_true",
109 default = False,
110 help = "Attempt to import a switch's configuration into the VLANd system")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000111parser.add_option_group(system_group)
112# May add shutdown, self-check etc. later
113
Steve McIntyre844bfd42014-11-27 16:58:31 +0000114# Switch commands
115switch_group = optparse.OptionGroup(parser, "Switch commands")
116switch_group.add_option("--list_all_switches",
117 dest = "list_all_switches",
118 action = "store_true",
119 default = False,
120 help = "List all the existing switches in the system")
121switch_group.add_option("--create_switch",
122 dest = "create_switch",
123 action = "store",
124 type = "string",
125 help = "Add a new switch to the system",
126 nargs = 1,
127 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000128switch_group.add_option("--delete_switch",
129 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000130 action = "store",
131 type = "int",
132 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000133 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000134 nargs = 1,
135 metavar = "<switch_id>")
136switch_group.add_option("--show_switch",
137 dest = "show_switch",
138 action = "store",
139 type = "int",
140 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000141 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000142 nargs = 1,
143 metavar = "<switch_id>")
144switch_group.add_option("--lookup_switch_by_name",
145 dest = "lookup_switch_by_name",
146 action = "store",
147 type = "string",
148 help = "Lookup a switch ID by name",
149 nargs = 1,
150 metavar = "<name>")
151switch_group.add_option("--list_switch_ports",
152 dest = "list_switch_ports",
153 action = "store",
154 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000155 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000156 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000157 nargs = 1,
158 metavar = "<switch_id>")
159parser.add_option_group(switch_group)
160
161# Port commands
162port_group = optparse.OptionGroup(parser, "Port commands")
163port_group.add_option("--list_all_ports",
164 dest = "list_all_ports",
165 action = "store_true",
166 default = False,
167 help = "List all the existing ports in the system")
168port_group.add_option("--create_port",
169 dest = "create_port",
170 action = "store",
171 type = "string",
172 help = "Add a new port to the system",
173 nargs = 2,
174 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000175port_group.add_option("--delete_port",
176 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000177 action = "store",
178 type = "int",
179 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000180 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000181 nargs = 1,
182 metavar = "<port_id>")
183port_group.add_option("--show_port",
184 dest = "show_port",
185 action = "store",
186 type = "int",
187 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000188 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000189 nargs = 1,
190 metavar = "<port_id>")
191port_group.add_option("--lookup_port_by_switch_and_name",
192 dest = "lookup_port_by_switch_and_name",
193 action = "store",
194 type = "string",
195 help = "Lookup a port ID by switch and port name",
196 nargs = 2,
197 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000198port_group.add_option("--set_port_mode",
199 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000200 action = "store",
201 type = "string",
202 help = "Set the mode of a port to 'trunk' or 'access'",
203 nargs = 2,
204 metavar = "<port_id> <mode>")
205port_group.add_option("--lock_port",
206 dest = "lock_port",
207 action = "store",
208 type = "string",
209 help = "Lock the settings on a port",
210 nargs = 1,
211 metavar = "<port_id>")
212port_group.add_option("--unlock_port",
213 dest = "unlock_port",
214 action = "store",
215 type = "string",
216 help = "Unock the settings on a port",
217 nargs = 1,
218 metavar = "<port_id>")
219port_group.add_option("--set_port_current_vlan",
220 dest = "set_port_current_vlan",
221 action = "store",
222 type = "int",
223 help = "Set the current VLAN assignment for a port",
224 nargs = 2,
225 metavar = "<port_id> <vlan_id>")
226port_group.add_option("--get_port_current_vlan",
227 dest = "get_port_current_vlan",
228 action = "store",
229 type = "int",
230 help = "Get the current VLAN assignment for a port",
231 nargs = 1,
232 metavar = "<port_id>")
233port_group.add_option("--set_port_base_vlan",
234 dest = "set_port_base_vlan",
235 action = "store",
236 type = "int",
237 help = "Set the base VLAN assignment for a port",
238 nargs = 2,
239 metavar = "<port_id> <vlan_id>")
240port_group.add_option("--get_port_base_vlan",
241 dest = "get_port_base_vlan",
242 action = "store",
243 type = "int",
244 help = "Get the base VLAN assignment for a port",
245 nargs = 1,
246 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000247port_group.add_option("--restore_port_to_base_vlan",
248 dest = "restore_port_to_base_vlan",
249 action = "store",
250 type = "int",
251 help = "Reset the port back to its base VLAN",
252 nargs = 1,
253 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000254parser.add_option_group(port_group)
255
256# VLAN commands
257vlan_group = optparse.OptionGroup(parser, "VLAN commands")
258vlan_group.add_option("--list_all_vlans",
259 dest = "list_all_vlans",
260 action = "store_true",
261 default = False,
262 help = "List all the existing vlans in the system")
263vlan_group.add_option("--create_vlan",
264 dest = "create_vlan",
265 action = "store",
266 type = "string",
267 help = "Add a new vlan to the system",
268 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000269 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000270vlan_group.add_option("--delete_vlan",
271 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000272 action = "store",
273 type = "int",
274 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000275 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000276 nargs = 1,
277 metavar = "<vlan_id>")
278vlan_group.add_option("--show_vlan",
279 dest = "show_vlan",
280 action = "store",
281 type = "int",
282 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000283 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000284 nargs = 1,
285 metavar = "<vlan_id>")
286parser.add_option_group(vlan_group)
287
288(opts, args) = parser.parse_args()
289
Steve McIntyre844bfd42014-11-27 16:58:31 +0000290if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000291 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000292 count = 0
Steve McIntyre844bfd42014-11-27 16:58:31 +0000293 for line in result:
294 print line
295 count += 1
296 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000297elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000298 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000299 count = 0
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000300 for line in result:
301 print line
302 count += 1
303 print '%d entries' % count
304elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000305 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000306 count = 0
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000307 for line in result:
308 print line
309 count += 1
310 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000311elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000312 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000313 switch_id = call_vland('db_update',
314 {'command':'db.create_switch',
315 'data':
316 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000317 print 'Created switch_id %d' % switch_id
318 except InputError as inst:
319 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000320elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000321 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000322 port_id = call_vland('db_update',
323 {'command':'db.create_port',
324 'data':
325 {'switch_id': opts.create_port[0],
326 'name': opts.create_port[1]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000327 print 'Created port_id %d' % port_id
328 except InputError as inst:
329 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000330elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000331 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000332 vlan_id = call_vland('vlan_update',
333 {'command':'api.create_vlan',
334 'data':
335 {'name': opts.create_vlan[0],
336 'tag': opts.create_vlan[1],
Steve McIntyreeb2cd202014-12-18 16:44:49 +0000337 'is_base_vlan': is_positive(opts.create_vlan[2])}})
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000338 print 'Created vlan_id %d' % vlan_id
339 except InputError as inst:
340 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000341elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000342 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000343 switch_id = call_vland('db_update',
344 {'command':'db.delete_switch',
345 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000346 print 'Deleted switch_id %d' % switch_id
347 except InputError as inst:
348 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000349elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000350 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000351 port_id = call_vland('db_update',
352 {'command':'db.delete_port',
353 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000354 except InputError as inst:
355 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000356elif opts.delete_vlan is not None:
357 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000358 vlan_id = call_vland('vlan_update',
359 {'command':'api.delete_vlan',
360 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000361 print 'Deleted vlan_id %d' % vlan_id
362 except InputError as inst:
363 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000364elif opts.lookup_switch_by_name is not None:
365 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000366 switch_id = call_vland('db_query',
367 {'command':'db.get_switch_id_by_name',
368 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000369 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000370 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000371 else:
372 print 'No switch found for name %s' % opts.lookup_switch_by_name
373 except InputError as inst:
374 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000375elif opts.show_switch is not None:
376 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000377 this_switch = call_vland('db_query',
378 {'command':'db.get_switch_by_id',
379 'data':
380 {'switch_id': opts.show_switch}})
381 if this_switch is not None:
382 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000383 else:
384 print 'No switch found for switch_id %d' % opts.show_switch
385 except InputError as inst:
386 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000387elif opts.list_switch_ports is not None:
388 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000389 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000390 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000391 'data':
392 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000393 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000394 for p in ports:
395 dump_port(p)
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000396 else:
397 print 'No ports found for switch_id %d' % opts.list_switch_ports
398 except InputError as inst:
399 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000400elif opts.show_port is not None:
401 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000402 this_port = call_vland('db_query',
403 {'command':'db.get_port_by_id',
404 'data':
405 {'port_id': opts.show_port}})
406 if this_port is not None:
407 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000408 else:
409 print 'No port found for port_id %d' % opts.show_port
410 except InputError as inst:
411 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000412elif opts.lookup_port_by_switch_and_name is not None:
413 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000414 p = call_vland('db_query',
415 {'command':'db.get_port_by_switch_and_name',
416 'data':
417 {'switch_id': opts.lookup_port_by_switch_and_name[0],
418 'name': opts.lookup_port_by_switch_and_name[1]}})
419 if p is not None:
420 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000421 else:
422 print 'No port found for switch_id %d, name %s' % (int(opts.lookup_port_by_switch_and_name[0]), opts.lookup_port_by_switch_and_name[1])
423 except InputError as inst:
424 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000425elif opts.set_port_mode is not None:
426 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000427 port_id = call_vland('vlan_update',
428 {'command':'api.set_port_mode',
429 'data':
430 {'port_id': opts.set_port_mode[0],
431 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000432 print "Updated mode for port_id %d" % port_id
433 except InputError as inst:
434 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000435elif opts.lock_port is not None:
436 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000437 port_id = call_vland('db_update',
438 {'command':'db.set_port_is_locked',
439 'data':
440 {'port_id': opts.lock_port,
441 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000442 print "Locked port_id %d" % port_id
443 except InputError as inst:
444 print 'Failed: %s' % inst
445elif opts.unlock_port is not None:
446 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000447 port_id = call_vland('db_update',
448 {'command':'db.set_port_is_locked',
449 'data':
450 {'port_id': opts.unlock_port,
451 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000452 print "Unlocked port_id %d" % port_id
453 except InputError as inst:
454 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000455elif opts.set_port_current_vlan is not None:
456 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000457 port_id = call_vland('vlan_update',
458 {'command':'api.set_current_vlan',
459 'data':
460 {'port_id': opts.set_current_vlan[0],
461 'vlan_id': opts.set_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000462 print "Set current VLAN on port_id %d" % port_id
463 except InputError as inst:
464 print 'Failed: %s' % inst
465elif opts.get_port_current_vlan is not None:
466 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000467 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000468 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000469 'data':
470 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000471 if vlan_id is not None:
472 print vlan_id
473 else:
474 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
475 except InputError as inst:
476 print 'Failed: %s' % inst
477elif opts.set_port_base_vlan is not None:
478 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000479 port_id = call_vland('db_update',
480 {'command':'db.set_base_vlan',
481 'data':
482 {'port_id': opts.set_base_vlan[0],
483 'base_vlan_id': opts.set_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000484 print "Set base VLAN on port_id %d" % port_id
485 except InputError as inst:
486 print 'Failed: %s' % inst
487elif opts.get_port_base_vlan is not None:
488 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000489 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000490 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000491 'data':
492 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000493 if vlan_id is not None:
494 print vlan_id
495 else:
496 print "No base_vlan_id found for port_id %d" % port_id
497 except InputError as inst:
498 print 'Failed: %s' % inst
499elif opts.restore_port_to_base_vlan is not None:
500 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000501 port_id = call_vland('vlan_update',
502 {'command': 'api.restore_base_vlan',
503 'data':
504 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000505 print "Restored port_id %d back to base VLAN" % port_id
506 except InputError as inst:
507 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000508elif opts.show_vlan is not None:
509 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000510 v = call_vland('db_query',
511 {'command':'db.get_vlan_by_id',
512 'data':
513 {'vlan_id': opts.show_vlan}})
514 if v is not None:
515 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000516 else:
517 print 'No vlan found for vlan_id %d' % opts.show_vlan
518 except InputError as inst:
519 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000520elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000521 print 'Config:'
522 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000523 default_vlan_id = call_vland('db_query',
524 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000525 'data':
526 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000527 print 'The default vlan tag (%d) is vlan_id %d' % (config.vland.default_vlan_tag, default_vlan_id)
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000528 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
529 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000530 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000531 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000532 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000533 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000534 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000535 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000536 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000537elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000538 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
539 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000540elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000541 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
542 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000543elif opts.version:
544 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000545elif opts.auto_import_switch:
546 print 'Attempting to import switch %s' % opts.auto_import_switch
547 if opts.auto_import_switch not in config.switches:
548 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
549 ret = call_vland('vlan_update',
550 {'command':'api.auto_import_switch',
551 'data':
552 {'switch': opts.auto_import_switch}})
Steve McIntyrea79ba8f2014-12-22 17:17:16 +0000553 print 'VLANd imported switch %s successfully: new switch_id %d, %d ports, %d VLANs' % (opts.auto_import_switch, ret['switch_id'], ret['num_ports_added'], ret['num_vlans_added'])
Steve McIntyredeff7952014-12-23 13:45:59 +0000554elif opts.probe_switches:
555 print 'Asking VLANd to probe all the configured switches'
556 ret = call_vland('daemon_query',{'command':'daemon.probe_switches', 'data': None})
557 print 'VLANd says:'
558 for field in ret:
559 print '%s: %s' % (field, ret['field'])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000560else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000561 print 'No recognised command given. Try -h for help'