blob: efd1645d9031d7196e9990b0d71e0d64f0615b40 [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
23import os, sys, types
24import time
25import logging
26import optparse
27
28vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
29sys.path.insert(0, vlandpath)
30
31import drivers
Steve McIntyre21f02702014-12-16 18:19:47 +000032from errors import CriticalError, InputError, ConfigError, SocketError
Steve McIntyre21f02702014-12-16 18:19:47 +000033from config.config import VlanConfig
34from ipc.ipc import VlanIpc
Steve McIntyre844bfd42014-11-27 16:58:31 +000035
36version = "0.0.0-DEV"
37banner = "Linaro VLANd admin interface, version %s" % version
38
Steve McIntyreae95fd62014-12-05 16:51:41 +000039def is_positive(text):
40 if text in ('1', 'y', 'Y', 't', 'T', 'True'):
41 return True
42 elif text in ('0', 'n', 'N', 'f', 'F', 'False'):
43 return False
44 else:
45 raise InputError("Cannot parse \"%s\" as True or False" % text)
46
Steve McIntyrea132c362014-12-05 15:53:21 +000047def dump_switch(switch):
48 print switch
49
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000050def dump_port(port):
51 print port
52
Steve McIntyree41e3f32014-12-05 18:08:21 +000053def dump_vlan(vlan):
54 print vlan
55
Steve McIntyref1c04f92014-12-16 18:23:15 +000056def call_vland(msgtype, msg):
57 ipc = VlanIpc()
58 ipc.client_connect('localhost', config.vland.port)
59 msg['client_name'] = 'admin.py'
60 msg['type'] = msgtype
61# print 'Sending:'
62# print msg
63 ipc.client_send(msg)
64 ret = ipc.client_recv_and_close()
65 if 'response' not in ret:
66 raise SocketError("Badly-formed response from VLANd server")
67 if ret['response'] == "ERROR":
68 raise InputError("VLANd server said: %s" % ret['error'])
69# print 'VLANd said in reply:'
70# print ret
71 return ret['data']
72
Steve McIntyre844bfd42014-11-27 16:58:31 +000073#print '%s' % banner
74
Steve McIntyrec12f6312014-12-15 15:00:12 +000075config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000076
Steve McIntyre844bfd42014-11-27 16:58:31 +000077usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000078parser = optparse.OptionParser(usage=usage, description=banner)
79
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000080# System commands
81system_group = optparse.OptionGroup(parser, "System commands")
82system_group.add_option("--status",
83 dest="status",
84 action = "store_true",
85 default = False,
86 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +000087system_group.add_option("--statistics",
88 dest="statistics",
89 action = "store_true",
90 default = False,
91 help = "Print some system statistics")
92system_group.add_option("--version",
93 dest="version",
94 action = "store_true",
95 default = False,
96 help = "Describe the version of this admin interface")
97system_group.add_option("--vland_version",
98 dest="vland_version",
99 action = "store_true",
100 default = False,
101 help = "Describe the version of the running VLANd")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000102parser.add_option_group(system_group)
103# May add shutdown, self-check etc. later
104
Steve McIntyre844bfd42014-11-27 16:58:31 +0000105# Switch commands
106switch_group = optparse.OptionGroup(parser, "Switch commands")
107switch_group.add_option("--list_all_switches",
108 dest = "list_all_switches",
109 action = "store_true",
110 default = False,
111 help = "List all the existing switches in the system")
112switch_group.add_option("--create_switch",
113 dest = "create_switch",
114 action = "store",
115 type = "string",
116 help = "Add a new switch to the system",
117 nargs = 1,
118 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000119switch_group.add_option("--delete_switch",
120 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000121 action = "store",
122 type = "int",
123 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000124 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000125 nargs = 1,
126 metavar = "<switch_id>")
127switch_group.add_option("--show_switch",
128 dest = "show_switch",
129 action = "store",
130 type = "int",
131 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000132 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000133 nargs = 1,
134 metavar = "<switch_id>")
135switch_group.add_option("--lookup_switch_by_name",
136 dest = "lookup_switch_by_name",
137 action = "store",
138 type = "string",
139 help = "Lookup a switch ID by name",
140 nargs = 1,
141 metavar = "<name>")
142switch_group.add_option("--list_switch_ports",
143 dest = "list_switch_ports",
144 action = "store",
145 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000146 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000147 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000148 nargs = 1,
149 metavar = "<switch_id>")
150parser.add_option_group(switch_group)
151
152# Port commands
153port_group = optparse.OptionGroup(parser, "Port commands")
154port_group.add_option("--list_all_ports",
155 dest = "list_all_ports",
156 action = "store_true",
157 default = False,
158 help = "List all the existing ports in the system")
159port_group.add_option("--create_port",
160 dest = "create_port",
161 action = "store",
162 type = "string",
163 help = "Add a new port to the system",
164 nargs = 2,
165 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000166port_group.add_option("--delete_port",
167 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000168 action = "store",
169 type = "int",
170 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000171 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000172 nargs = 1,
173 metavar = "<port_id>")
174port_group.add_option("--show_port",
175 dest = "show_port",
176 action = "store",
177 type = "int",
178 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000179 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000180 nargs = 1,
181 metavar = "<port_id>")
182port_group.add_option("--lookup_port_by_switch_and_name",
183 dest = "lookup_port_by_switch_and_name",
184 action = "store",
185 type = "string",
186 help = "Lookup a port ID by switch and port name",
187 nargs = 2,
188 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000189port_group.add_option("--set_port_mode",
190 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000191 action = "store",
192 type = "string",
193 help = "Set the mode of a port to 'trunk' or 'access'",
194 nargs = 2,
195 metavar = "<port_id> <mode>")
196port_group.add_option("--lock_port",
197 dest = "lock_port",
198 action = "store",
199 type = "string",
200 help = "Lock the settings on a port",
201 nargs = 1,
202 metavar = "<port_id>")
203port_group.add_option("--unlock_port",
204 dest = "unlock_port",
205 action = "store",
206 type = "string",
207 help = "Unock the settings on a port",
208 nargs = 1,
209 metavar = "<port_id>")
210port_group.add_option("--set_port_current_vlan",
211 dest = "set_port_current_vlan",
212 action = "store",
213 type = "int",
214 help = "Set the current VLAN assignment for a port",
215 nargs = 2,
216 metavar = "<port_id> <vlan_id>")
217port_group.add_option("--get_port_current_vlan",
218 dest = "get_port_current_vlan",
219 action = "store",
220 type = "int",
221 help = "Get the current VLAN assignment for a port",
222 nargs = 1,
223 metavar = "<port_id>")
224port_group.add_option("--set_port_base_vlan",
225 dest = "set_port_base_vlan",
226 action = "store",
227 type = "int",
228 help = "Set the base VLAN assignment for a port",
229 nargs = 2,
230 metavar = "<port_id> <vlan_id>")
231port_group.add_option("--get_port_base_vlan",
232 dest = "get_port_base_vlan",
233 action = "store",
234 type = "int",
235 help = "Get the base VLAN assignment for a port",
236 nargs = 1,
237 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000238port_group.add_option("--restore_port_to_base_vlan",
239 dest = "restore_port_to_base_vlan",
240 action = "store",
241 type = "int",
242 help = "Reset the port back to its base VLAN",
243 nargs = 1,
244 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000245parser.add_option_group(port_group)
246
247# VLAN commands
248vlan_group = optparse.OptionGroup(parser, "VLAN commands")
249vlan_group.add_option("--list_all_vlans",
250 dest = "list_all_vlans",
251 action = "store_true",
252 default = False,
253 help = "List all the existing vlans in the system")
254vlan_group.add_option("--create_vlan",
255 dest = "create_vlan",
256 action = "store",
257 type = "string",
258 help = "Add a new vlan to the system",
259 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000260 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000261vlan_group.add_option("--delete_vlan",
262 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000263 action = "store",
264 type = "int",
265 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000266 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000267 nargs = 1,
268 metavar = "<vlan_id>")
269vlan_group.add_option("--show_vlan",
270 dest = "show_vlan",
271 action = "store",
272 type = "int",
273 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000274 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000275 nargs = 1,
276 metavar = "<vlan_id>")
277parser.add_option_group(vlan_group)
278
279(opts, args) = parser.parse_args()
280
Steve McIntyre844bfd42014-11-27 16:58:31 +0000281if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000282 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000283 count = 0;
284 for line in result:
285 print line
286 count += 1
287 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000288elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000289 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000290 count = 0;
291 for line in result:
292 print line
293 count += 1
294 print '%d entries' % count
295elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000296 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000297 count = 0;
298 for line in result:
299 print line
300 count += 1
301 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000302elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000303 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000304 switch_id = call_vland('db_update',
305 {'command':'db.create_switch',
306 'data':
307 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000308 print 'Created switch_id %d' % switch_id
309 except InputError as inst:
310 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000311elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000312 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000313 port_id = call_vland('db_update',
314 {'command':'db.create_port',
315 'data':
316 {'switch_id': opts.create_port[0],
317 'name': opts.create_port[1]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000318 print 'Created port_id %d' % port_id
319 except InputError as inst:
320 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000321elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000322 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000323 vlan_id = call_vland('vlan_update',
324 {'command':'api.create_vlan',
325 'data':
326 {'name': opts.create_vlan[0],
327 'tag': opts.create_vlan[1],
328 'is_base_vlan': opts.create_vlan[2]}})
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000329 print 'Created vlan_id %d' % vlan_id
330 except InputError as inst:
331 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000332elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000333 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000334 switch_id = call_vland('db_update',
335 {'command':'db.delete_switch',
336 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000337 print 'Deleted switch_id %d' % switch_id
338 except InputError as inst:
339 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000340elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000341 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000342 port_id = call_vland('db_update',
343 {'command':'db.delete_port',
344 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000345 except InputError as inst:
346 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000347elif opts.delete_vlan is not None:
348 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000349 vlan_id = call_vland('vlan_update',
350 {'command':'api.delete_vlan',
351 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000352 print 'Deleted vlan_id %d' % vlan_id
353 except InputError as inst:
354 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000355elif opts.lookup_switch_by_name is not None:
356 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000357 switch_id = call_vland('db_query',
358 {'command':'db.get_switch_id_by_name',
359 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000360 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000361 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000362 else:
363 print 'No switch found for name %s' % opts.lookup_switch_by_name
364 except InputError as inst:
365 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000366elif opts.show_switch is not None:
367 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000368 switch = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000369 {'command':'db.get_switch_by_id',
370 'data':{'switch_id': opts.show_switch}})
Steve McIntyrea132c362014-12-05 15:53:21 +0000371 if switch is not None:
372 dump_switch(switch)
373 else:
374 print 'No switch found for switch_id %d' % opts.show_switch
375 except InputError as inst:
376 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000377elif opts.list_switch_ports is not None:
378 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000379 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000380 {'command':'db.get_ports_by_switch',
381 'data':{'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000382 if ports is not None:
383 for port in ports:
384 dump_port(port)
385 else:
386 print 'No ports found for switch_id %d' % opts.list_switch_ports
387 except InputError as inst:
388 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000389elif opts.show_port is not None:
390 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000391 port = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000392 {'command':'db.get_port_by_id',
393 'data':{'port_id': opts.show_port}})
Steve McIntyre08dd8392014-12-05 16:07:20 +0000394 if port is not None:
395 dump_port(port)
396 else:
397 print 'No port found for port_id %d' % opts.show_port
398 except InputError as inst:
399 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000400elif opts.lookup_port_by_switch_and_name is not None:
401 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000402 port = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000403 {'command':'db.get_port_by_switch_and_name',
404 'data':
405 {'switch_id': opts.lookup_port_by_switch_and_name[0],
406 'name': opts.lookup_port_by_switch_and_name[1]}})
Steve McIntyre73106572014-12-05 16:13:36 +0000407 if port is not None:
408 print port
409 else:
410 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])
411 except InputError as inst:
412 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000413elif opts.set_port_mode is not None:
414 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000415 port_id = call_vland('vlan_update',
416 {'command':'api.set_port_mode',
417 'data':
418 {'port_id': opts.set_port_mode[0],
419 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000420 print "Updated mode for port_id %d" % port_id
421 except InputError as inst:
422 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000423elif opts.lock_port is not None:
424 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000425 port_id = call_vland('db_update',
426 {'command':'db.set_port_is_locked',
427 'data':
428 {'port_id': opts.lock_port,
429 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000430 print "Locked port_id %d" % port_id
431 except InputError as inst:
432 print 'Failed: %s' % inst
433elif opts.unlock_port is not None:
434 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000435 port_id = call_vland('db_update',
436 {'command':'db.set_port_is_locked',
437 'data':
438 {'port_id': opts.unlock_port,
439 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000440 print "Unlocked port_id %d" % port_id
441 except InputError as inst:
442 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000443elif opts.set_port_current_vlan is not None:
444 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000445 port_id = call_vland('vlan_update',
446 {'command':'api.set_current_vlan',
447 'data':
448 {'port_id': opts.set_current_vlan[0],
449 'vlan_id': opts.set_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000450 print "Set current VLAN on port_id %d" % port_id
451 except InputError as inst:
452 print 'Failed: %s' % inst
453elif opts.get_port_current_vlan is not None:
454 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000455 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000456 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000457 'data':
458 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000459 if vlan_id is not None:
460 print vlan_id
461 else:
462 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
463 except InputError as inst:
464 print 'Failed: %s' % inst
465elif opts.set_port_base_vlan is not None:
466 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000467 port_id = call_vland('db_update',
468 {'command':'db.set_base_vlan',
469 'data':
470 {'port_id': opts.set_base_vlan[0],
471 'base_vlan_id': opts.set_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000472 print "Set base VLAN on port_id %d" % port_id
473 except InputError as inst:
474 print 'Failed: %s' % inst
475elif opts.get_port_base_vlan is not None:
476 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000477 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000478 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000479 'data':
480 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000481 if vlan_id is not None:
482 print vlan_id
483 else:
484 print "No base_vlan_id found for port_id %d" % port_id
485 except InputError as inst:
486 print 'Failed: %s' % inst
487elif opts.restore_port_to_base_vlan is not None:
488 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000489 port_id = call_vland('vlan_update',
490 {'command': 'api.restore_base_vlan',
491 'data':
492 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000493 print "Restored port_id %d back to base VLAN" % port_id
494 except InputError as inst:
495 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000496elif opts.show_vlan is not None:
497 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000498 vlan = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000499 {'command':'db.get_vlan_by_id',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000500 'data':
501 {'vlan_id': opts.show_vlan}})
Steve McIntyree41e3f32014-12-05 18:08:21 +0000502 if vlan is not None:
503 dump_vlan(vlan)
504 else:
505 print 'No vlan found for vlan_id %d' % opts.show_vlan
506 except InputError as inst:
507 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000508elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000509 print 'Config:'
510 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000511 default_vlan_id = call_vland('db_query',
512 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000513 'data':
514 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000515 print 'The default vlan tag (%d) is vlan_id %d' % (config.vland.default_vlan_tag, default_vlan_id)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000516 ret = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000517 print 'VLANd is running %s' % ret['running']
518 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000519 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000520 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000521 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000522 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000523 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000524 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000525elif opts.vland_version:
526 ret = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
527 print 'VLANd version %s' % ret['version']
528elif opts.statistics:
529 ret = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
530 print 'VLANd uptime: %d seconds' % ret['uptime']
531elif opts.version:
532 print 'VLANd admin interface version %s' % version
Steve McIntyrea0534a52014-12-05 17:58:40 +0000533else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000534 print 'No recognised command given. Try -h for help'