blob: f9a59b465ea4df65a7d5265c8fec99a2497fe18b [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
33version = "0.0.0-DEV"
34banner = "Linaro VLANd admin interface, version %s" % version
35
Steve McIntyreae95fd62014-12-05 16:51:41 +000036def is_positive(text):
37 if text in ('1', 'y', 'Y', 't', 'T', 'True'):
38 return True
39 elif text in ('0', 'n', 'N', 'f', 'F', 'False'):
40 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 McIntyre9cd6c3d2014-12-05 18:10:35 +000099parser.add_option_group(system_group)
100# May add shutdown, self-check etc. later
101
Steve McIntyre844bfd42014-11-27 16:58:31 +0000102# Switch commands
103switch_group = optparse.OptionGroup(parser, "Switch commands")
104switch_group.add_option("--list_all_switches",
105 dest = "list_all_switches",
106 action = "store_true",
107 default = False,
108 help = "List all the existing switches in the system")
109switch_group.add_option("--create_switch",
110 dest = "create_switch",
111 action = "store",
112 type = "string",
113 help = "Add a new switch to the system",
114 nargs = 1,
115 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000116switch_group.add_option("--delete_switch",
117 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000118 action = "store",
119 type = "int",
120 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000121 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000122 nargs = 1,
123 metavar = "<switch_id>")
124switch_group.add_option("--show_switch",
125 dest = "show_switch",
126 action = "store",
127 type = "int",
128 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000129 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000130 nargs = 1,
131 metavar = "<switch_id>")
132switch_group.add_option("--lookup_switch_by_name",
133 dest = "lookup_switch_by_name",
134 action = "store",
135 type = "string",
136 help = "Lookup a switch ID by name",
137 nargs = 1,
138 metavar = "<name>")
139switch_group.add_option("--list_switch_ports",
140 dest = "list_switch_ports",
141 action = "store",
142 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000143 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000144 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000145 nargs = 1,
146 metavar = "<switch_id>")
147parser.add_option_group(switch_group)
148
149# Port commands
150port_group = optparse.OptionGroup(parser, "Port commands")
151port_group.add_option("--list_all_ports",
152 dest = "list_all_ports",
153 action = "store_true",
154 default = False,
155 help = "List all the existing ports in the system")
156port_group.add_option("--create_port",
157 dest = "create_port",
158 action = "store",
159 type = "string",
160 help = "Add a new port to the system",
161 nargs = 2,
162 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000163port_group.add_option("--delete_port",
164 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000165 action = "store",
166 type = "int",
167 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000168 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000169 nargs = 1,
170 metavar = "<port_id>")
171port_group.add_option("--show_port",
172 dest = "show_port",
173 action = "store",
174 type = "int",
175 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000176 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000177 nargs = 1,
178 metavar = "<port_id>")
179port_group.add_option("--lookup_port_by_switch_and_name",
180 dest = "lookup_port_by_switch_and_name",
181 action = "store",
182 type = "string",
183 help = "Lookup a port ID by switch and port name",
184 nargs = 2,
185 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000186port_group.add_option("--set_port_mode",
187 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000188 action = "store",
189 type = "string",
190 help = "Set the mode of a port to 'trunk' or 'access'",
191 nargs = 2,
192 metavar = "<port_id> <mode>")
193port_group.add_option("--lock_port",
194 dest = "lock_port",
195 action = "store",
196 type = "string",
197 help = "Lock the settings on a port",
198 nargs = 1,
199 metavar = "<port_id>")
200port_group.add_option("--unlock_port",
201 dest = "unlock_port",
202 action = "store",
203 type = "string",
204 help = "Unock the settings on a port",
205 nargs = 1,
206 metavar = "<port_id>")
207port_group.add_option("--set_port_current_vlan",
208 dest = "set_port_current_vlan",
209 action = "store",
210 type = "int",
211 help = "Set the current VLAN assignment for a port",
212 nargs = 2,
213 metavar = "<port_id> <vlan_id>")
214port_group.add_option("--get_port_current_vlan",
215 dest = "get_port_current_vlan",
216 action = "store",
217 type = "int",
218 help = "Get the current VLAN assignment for a port",
219 nargs = 1,
220 metavar = "<port_id>")
221port_group.add_option("--set_port_base_vlan",
222 dest = "set_port_base_vlan",
223 action = "store",
224 type = "int",
225 help = "Set the base VLAN assignment for a port",
226 nargs = 2,
227 metavar = "<port_id> <vlan_id>")
228port_group.add_option("--get_port_base_vlan",
229 dest = "get_port_base_vlan",
230 action = "store",
231 type = "int",
232 help = "Get the base VLAN assignment for a port",
233 nargs = 1,
234 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000235port_group.add_option("--restore_port_to_base_vlan",
236 dest = "restore_port_to_base_vlan",
237 action = "store",
238 type = "int",
239 help = "Reset the port back to its base VLAN",
240 nargs = 1,
241 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000242parser.add_option_group(port_group)
243
244# VLAN commands
245vlan_group = optparse.OptionGroup(parser, "VLAN commands")
246vlan_group.add_option("--list_all_vlans",
247 dest = "list_all_vlans",
248 action = "store_true",
249 default = False,
250 help = "List all the existing vlans in the system")
251vlan_group.add_option("--create_vlan",
252 dest = "create_vlan",
253 action = "store",
254 type = "string",
255 help = "Add a new vlan to the system",
256 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000257 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000258vlan_group.add_option("--delete_vlan",
259 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000260 action = "store",
261 type = "int",
262 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000263 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000264 nargs = 1,
265 metavar = "<vlan_id>")
266vlan_group.add_option("--show_vlan",
267 dest = "show_vlan",
268 action = "store",
269 type = "int",
270 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000271 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000272 nargs = 1,
273 metavar = "<vlan_id>")
274parser.add_option_group(vlan_group)
275
276(opts, args) = parser.parse_args()
277
Steve McIntyre844bfd42014-11-27 16:58:31 +0000278if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000279 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000280 count = 0
Steve McIntyre844bfd42014-11-27 16:58:31 +0000281 for line in result:
282 print line
283 count += 1
284 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000285elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000286 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000287 count = 0
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000288 for line in result:
289 print line
290 count += 1
291 print '%d entries' % count
292elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000293 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000294 count = 0
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000295 for line in result:
296 print line
297 count += 1
298 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000299elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000300 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000301 switch_id = call_vland('db_update',
302 {'command':'db.create_switch',
303 'data':
304 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000305 print 'Created switch_id %d' % switch_id
306 except InputError as inst:
307 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000308elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000309 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000310 port_id = call_vland('db_update',
311 {'command':'db.create_port',
312 'data':
313 {'switch_id': opts.create_port[0],
314 'name': opts.create_port[1]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000315 print 'Created port_id %d' % port_id
316 except InputError as inst:
317 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000318elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000319 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000320 vlan_id = call_vland('vlan_update',
321 {'command':'api.create_vlan',
322 'data':
323 {'name': opts.create_vlan[0],
324 'tag': opts.create_vlan[1],
Steve McIntyreeb2cd202014-12-18 16:44:49 +0000325 'is_base_vlan': is_positive(opts.create_vlan[2])}})
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000326 print 'Created vlan_id %d' % vlan_id
327 except InputError as inst:
328 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000329elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000330 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000331 switch_id = call_vland('db_update',
332 {'command':'db.delete_switch',
333 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000334 print 'Deleted switch_id %d' % switch_id
335 except InputError as inst:
336 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000337elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000338 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000339 port_id = call_vland('db_update',
340 {'command':'db.delete_port',
341 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000342 except InputError as inst:
343 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000344elif opts.delete_vlan is not None:
345 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000346 vlan_id = call_vland('vlan_update',
347 {'command':'api.delete_vlan',
348 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000349 print 'Deleted vlan_id %d' % vlan_id
350 except InputError as inst:
351 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000352elif opts.lookup_switch_by_name is not None:
353 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000354 switch_id = call_vland('db_query',
355 {'command':'db.get_switch_id_by_name',
356 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000357 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000358 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000359 else:
360 print 'No switch found for name %s' % opts.lookup_switch_by_name
361 except InputError as inst:
362 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000363elif opts.show_switch is not None:
364 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000365 switch = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000366 {'command':'db.get_switch_by_id',
367 'data':{'switch_id': opts.show_switch}})
Steve McIntyrea132c362014-12-05 15:53:21 +0000368 if switch is not None:
369 dump_switch(switch)
370 else:
371 print 'No switch found for switch_id %d' % opts.show_switch
372 except InputError as inst:
373 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000374elif opts.list_switch_ports is not None:
375 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000376 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000377 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000378 'data':
379 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000380 if ports is not None:
381 for port in ports:
382 dump_port(port)
383 else:
384 print 'No ports found for switch_id %d' % opts.list_switch_ports
385 except InputError as inst:
386 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000387elif opts.show_port is not None:
388 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000389 port = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000390 {'command':'db.get_port_by_id',
391 'data':{'port_id': opts.show_port}})
Steve McIntyre08dd8392014-12-05 16:07:20 +0000392 if port is not None:
393 dump_port(port)
394 else:
395 print 'No port found for port_id %d' % opts.show_port
396 except InputError as inst:
397 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000398elif opts.lookup_port_by_switch_and_name is not None:
399 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000400 port = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000401 {'command':'db.get_port_by_switch_and_name',
402 'data':
403 {'switch_id': opts.lookup_port_by_switch_and_name[0],
404 'name': opts.lookup_port_by_switch_and_name[1]}})
Steve McIntyre73106572014-12-05 16:13:36 +0000405 if port is not None:
406 print port
407 else:
408 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])
409 except InputError as inst:
410 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000411elif opts.set_port_mode is not None:
412 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000413 port_id = call_vland('vlan_update',
414 {'command':'api.set_port_mode',
415 'data':
416 {'port_id': opts.set_port_mode[0],
417 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000418 print "Updated mode for port_id %d" % port_id
419 except InputError as inst:
420 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000421elif opts.lock_port is not None:
422 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000423 port_id = call_vland('db_update',
424 {'command':'db.set_port_is_locked',
425 'data':
426 {'port_id': opts.lock_port,
427 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000428 print "Locked port_id %d" % port_id
429 except InputError as inst:
430 print 'Failed: %s' % inst
431elif opts.unlock_port is not None:
432 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000433 port_id = call_vland('db_update',
434 {'command':'db.set_port_is_locked',
435 'data':
436 {'port_id': opts.unlock_port,
437 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000438 print "Unlocked port_id %d" % port_id
439 except InputError as inst:
440 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000441elif opts.set_port_current_vlan is not None:
442 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000443 port_id = call_vland('vlan_update',
444 {'command':'api.set_current_vlan',
445 'data':
446 {'port_id': opts.set_current_vlan[0],
447 'vlan_id': opts.set_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000448 print "Set current VLAN on port_id %d" % port_id
449 except InputError as inst:
450 print 'Failed: %s' % inst
451elif opts.get_port_current_vlan is not None:
452 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000453 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000454 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000455 'data':
456 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000457 if vlan_id is not None:
458 print vlan_id
459 else:
460 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
461 except InputError as inst:
462 print 'Failed: %s' % inst
463elif opts.set_port_base_vlan is not None:
464 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000465 port_id = call_vland('db_update',
466 {'command':'db.set_base_vlan',
467 'data':
468 {'port_id': opts.set_base_vlan[0],
469 'base_vlan_id': opts.set_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000470 print "Set base VLAN on port_id %d" % port_id
471 except InputError as inst:
472 print 'Failed: %s' % inst
473elif opts.get_port_base_vlan is not None:
474 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000475 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000476 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000477 'data':
478 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000479 if vlan_id is not None:
480 print vlan_id
481 else:
482 print "No base_vlan_id found for port_id %d" % port_id
483 except InputError as inst:
484 print 'Failed: %s' % inst
485elif opts.restore_port_to_base_vlan is not None:
486 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000487 port_id = call_vland('vlan_update',
488 {'command': 'api.restore_base_vlan',
489 'data':
490 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000491 print "Restored port_id %d back to base VLAN" % port_id
492 except InputError as inst:
493 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000494elif opts.show_vlan is not None:
495 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000496 vlan = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000497 {'command':'db.get_vlan_by_id',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000498 'data':
499 {'vlan_id': opts.show_vlan}})
Steve McIntyree41e3f32014-12-05 18:08:21 +0000500 if vlan is not None:
501 dump_vlan(vlan)
502 else:
503 print 'No vlan found for vlan_id %d' % opts.show_vlan
504 except InputError as inst:
505 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000506elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000507 print 'Config:'
508 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000509 default_vlan_id = call_vland('db_query',
510 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000511 'data':
512 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000513 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 +0000514 ret = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000515 print 'VLANd is running %s' % ret['running']
516 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000517 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000518 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000519 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000520 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000521 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000522 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000523elif opts.vland_version:
524 ret = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
525 print 'VLANd version %s' % ret['version']
526elif opts.statistics:
527 ret = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
528 print 'VLANd uptime: %d seconds' % ret['uptime']
529elif opts.version:
530 print 'VLANd admin interface version %s' % version
Steve McIntyrea0534a52014-12-05 17:58:40 +0000531else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000532 print 'No recognised command given. Try -h for help'