blob: c91bfba0180a656c9866bfa1ab3b20ff532886f2 [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 McIntyre13d69832015-01-23 17:50:55 +000033version = "0.2-DEV"
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
Steve McIntyref1c04f92014-12-16 18:23:15 +000058 ipc.client_send(msg)
59 ret = ipc.client_recv_and_close()
60 if 'response' not in ret:
61 raise SocketError("Badly-formed response from VLANd server")
62 if ret['response'] == "ERROR":
Steve McIntyre8a1a1ae2015-01-23 17:52:14 +000063 print "Input error: VLANd server said \"%s\"" % ret['error']
64 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000065 return ret['data']
66
Steve McIntyrec12f6312014-12-15 15:00:12 +000067config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000068
Steve McIntyre844bfd42014-11-27 16:58:31 +000069usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000070parser = optparse.OptionParser(usage=usage, description=banner)
71
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000072# System commands
73system_group = optparse.OptionGroup(parser, "System commands")
74system_group.add_option("--status",
75 dest="status",
76 action = "store_true",
77 default = False,
78 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +000079system_group.add_option("--statistics",
80 dest="statistics",
81 action = "store_true",
82 default = False,
83 help = "Print some system statistics")
84system_group.add_option("--version",
85 dest="version",
86 action = "store_true",
87 default = False,
88 help = "Describe the version of this admin interface")
89system_group.add_option("--vland_version",
90 dest="vland_version",
91 action = "store_true",
92 default = False,
93 help = "Describe the version of the running VLANd")
Steve McIntyrea4bb9912015-01-13 18:40:37 +000094system_group.add_option("--auto_import_switch",
Steve McIntyre6c562602014-12-22 16:10:54 +000095 dest = "auto_import_switch",
96 action = "store",
97 type = "string",
98 help = "Attempt to import a switch's configuration into the VLANd system",
99 nargs = 1,
100 metavar = "<switch name>")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000101system_group.add_option("--probe_switches",
Steve McIntyredeff7952014-12-23 13:45:59 +0000102 dest = "probe_switches",
103 action = "store_true",
104 default = False,
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000105 help = "Probe all the configured switches")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000106parser.add_option_group(system_group)
107# May add shutdown, self-check etc. later
108
Steve McIntyre844bfd42014-11-27 16:58:31 +0000109# Switch commands
110switch_group = optparse.OptionGroup(parser, "Switch commands")
111switch_group.add_option("--list_all_switches",
112 dest = "list_all_switches",
113 action = "store_true",
114 default = False,
115 help = "List all the existing switches in the system")
116switch_group.add_option("--create_switch",
117 dest = "create_switch",
118 action = "store",
119 type = "string",
120 help = "Add a new switch to the system",
121 nargs = 1,
122 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000123switch_group.add_option("--delete_switch",
124 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000125 action = "store",
126 type = "int",
127 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000128 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000129 nargs = 1,
130 metavar = "<switch_id>")
131switch_group.add_option("--show_switch",
132 dest = "show_switch",
133 action = "store",
134 type = "int",
135 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000136 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000137 nargs = 1,
138 metavar = "<switch_id>")
139switch_group.add_option("--lookup_switch_by_name",
140 dest = "lookup_switch_by_name",
141 action = "store",
142 type = "string",
143 help = "Lookup a switch ID by name",
144 nargs = 1,
145 metavar = "<name>")
146switch_group.add_option("--list_switch_ports",
147 dest = "list_switch_ports",
148 action = "store",
149 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000150 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000151 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000152 nargs = 1,
153 metavar = "<switch_id>")
154parser.add_option_group(switch_group)
155
156# Port commands
157port_group = optparse.OptionGroup(parser, "Port commands")
158port_group.add_option("--list_all_ports",
159 dest = "list_all_ports",
160 action = "store_true",
161 default = False,
162 help = "List all the existing ports in the system")
163port_group.add_option("--create_port",
164 dest = "create_port",
165 action = "store",
166 type = "string",
167 help = "Add a new port to the system",
168 nargs = 2,
169 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000170port_group.add_option("--delete_port",
171 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000172 action = "store",
173 type = "int",
174 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000175 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000176 nargs = 1,
177 metavar = "<port_id>")
178port_group.add_option("--show_port",
179 dest = "show_port",
180 action = "store",
181 type = "int",
182 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000183 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000184 nargs = 1,
185 metavar = "<port_id>")
186port_group.add_option("--lookup_port_by_switch_and_name",
187 dest = "lookup_port_by_switch_and_name",
188 action = "store",
189 type = "string",
190 help = "Lookup a port ID by switch and port name",
191 nargs = 2,
192 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000193port_group.add_option("--set_port_mode",
194 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000195 action = "store",
196 type = "string",
197 help = "Set the mode of a port to 'trunk' or 'access'",
198 nargs = 2,
199 metavar = "<port_id> <mode>")
200port_group.add_option("--lock_port",
201 dest = "lock_port",
202 action = "store",
203 type = "string",
204 help = "Lock the settings on a port",
205 nargs = 1,
206 metavar = "<port_id>")
207port_group.add_option("--unlock_port",
208 dest = "unlock_port",
209 action = "store",
210 type = "string",
211 help = "Unock the settings on a port",
212 nargs = 1,
213 metavar = "<port_id>")
214port_group.add_option("--set_port_current_vlan",
215 dest = "set_port_current_vlan",
216 action = "store",
217 type = "int",
218 help = "Set the current VLAN assignment for a port",
219 nargs = 2,
220 metavar = "<port_id> <vlan_id>")
221port_group.add_option("--get_port_current_vlan",
222 dest = "get_port_current_vlan",
223 action = "store",
224 type = "int",
225 help = "Get the current VLAN assignment for a port",
226 nargs = 1,
227 metavar = "<port_id>")
228port_group.add_option("--set_port_base_vlan",
229 dest = "set_port_base_vlan",
230 action = "store",
231 type = "int",
232 help = "Set the base VLAN assignment for a port",
233 nargs = 2,
234 metavar = "<port_id> <vlan_id>")
235port_group.add_option("--get_port_base_vlan",
236 dest = "get_port_base_vlan",
237 action = "store",
238 type = "int",
239 help = "Get the base VLAN assignment for a port",
240 nargs = 1,
241 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000242port_group.add_option("--restore_port_to_base_vlan",
243 dest = "restore_port_to_base_vlan",
244 action = "store",
245 type = "int",
246 help = "Reset the port back to its base VLAN",
247 nargs = 1,
248 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000249parser.add_option_group(port_group)
250
251# VLAN commands
252vlan_group = optparse.OptionGroup(parser, "VLAN commands")
253vlan_group.add_option("--list_all_vlans",
254 dest = "list_all_vlans",
255 action = "store_true",
256 default = False,
257 help = "List all the existing vlans in the system")
258vlan_group.add_option("--create_vlan",
259 dest = "create_vlan",
260 action = "store",
261 type = "string",
262 help = "Add a new vlan to the system",
263 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000264 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000265vlan_group.add_option("--delete_vlan",
266 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000267 action = "store",
268 type = "int",
269 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000270 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000271 nargs = 1,
272 metavar = "<vlan_id>")
273vlan_group.add_option("--show_vlan",
274 dest = "show_vlan",
275 action = "store",
276 type = "int",
277 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000278 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000279 nargs = 1,
280 metavar = "<vlan_id>")
281parser.add_option_group(vlan_group)
282
283(opts, args) = parser.parse_args()
284
Steve McIntyre844bfd42014-11-27 16:58:31 +0000285if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000286 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000287 count = 0
Steve McIntyre844bfd42014-11-27 16:58:31 +0000288 for line in result:
289 print line
290 count += 1
291 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000292elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000293 result = call_vland('db_query', {'command':'db.all_ports', '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
299elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000300 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyre42c69182014-12-18 16:43:43 +0000301 count = 0
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000302 for line in result:
303 print line
304 count += 1
305 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000306elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000307 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000308 switch_id = call_vland('db_update',
309 {'command':'db.create_switch',
310 'data':
311 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000312 print 'Created switch_id %d' % switch_id
313 except InputError as inst:
314 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000315elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000316 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000317 port_id = call_vland('db_update',
318 {'command':'db.create_port',
319 'data':
320 {'switch_id': opts.create_port[0],
321 'name': opts.create_port[1]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000322 print 'Created port_id %d' % port_id
323 except InputError as inst:
324 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000325elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000326 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000327 vlan_id = call_vland('vlan_update',
328 {'command':'api.create_vlan',
329 'data':
330 {'name': opts.create_vlan[0],
331 'tag': opts.create_vlan[1],
Steve McIntyreeb2cd202014-12-18 16:44:49 +0000332 'is_base_vlan': is_positive(opts.create_vlan[2])}})
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000333 print 'Created vlan_id %d' % vlan_id
334 except InputError as inst:
335 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000336elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000337 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000338 switch_id = call_vland('db_update',
339 {'command':'db.delete_switch',
340 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000341 print 'Deleted switch_id %d' % switch_id
342 except InputError as inst:
343 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000344elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000345 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000346 port_id = call_vland('db_update',
347 {'command':'db.delete_port',
348 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000349 except InputError as inst:
350 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000351elif opts.delete_vlan is not None:
352 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000353 vlan_id = call_vland('vlan_update',
354 {'command':'api.delete_vlan',
355 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000356 print 'Deleted vlan_id %d' % vlan_id
357 except InputError as inst:
358 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000359elif opts.lookup_switch_by_name is not None:
360 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000361 switch_id = call_vland('db_query',
362 {'command':'db.get_switch_id_by_name',
363 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000364 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000365 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000366 else:
367 print 'No switch found for name %s' % opts.lookup_switch_by_name
368 except InputError as inst:
369 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000370elif opts.show_switch is not None:
371 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000372 this_switch = call_vland('db_query',
373 {'command':'db.get_switch_by_id',
374 'data':
375 {'switch_id': opts.show_switch}})
376 if this_switch is not None:
377 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000378 else:
379 print 'No switch found for switch_id %d' % opts.show_switch
380 except InputError as inst:
381 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000382elif opts.list_switch_ports is not None:
383 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000384 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000385 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000386 'data':
387 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000388 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000389 for p in ports:
390 dump_port(p)
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000391 else:
392 print 'No ports found for switch_id %d' % opts.list_switch_ports
393 except InputError as inst:
394 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000395elif opts.show_port is not None:
396 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000397 this_port = call_vland('db_query',
398 {'command':'db.get_port_by_id',
399 'data':
400 {'port_id': opts.show_port}})
401 if this_port is not None:
402 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000403 else:
404 print 'No port found for port_id %d' % opts.show_port
405 except InputError as inst:
406 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000407elif opts.lookup_port_by_switch_and_name is not None:
408 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000409 p = call_vland('db_query',
410 {'command':'db.get_port_by_switch_and_name',
411 'data':
412 {'switch_id': opts.lookup_port_by_switch_and_name[0],
413 'name': opts.lookup_port_by_switch_and_name[1]}})
414 if p is not None:
415 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000416 else:
417 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])
418 except InputError as inst:
419 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000420elif opts.set_port_mode is not None:
421 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000422 port_id = call_vland('vlan_update',
423 {'command':'api.set_port_mode',
424 'data':
425 {'port_id': opts.set_port_mode[0],
426 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000427 print "Updated mode for port_id %d" % port_id
428 except InputError as inst:
429 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000430elif opts.lock_port is not None:
431 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000432 port_id = call_vland('db_update',
433 {'command':'db.set_port_is_locked',
434 'data':
435 {'port_id': opts.lock_port,
436 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000437 print "Locked port_id %d" % port_id
438 except InputError as inst:
439 print 'Failed: %s' % inst
440elif opts.unlock_port is not None:
441 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000442 port_id = call_vland('db_update',
443 {'command':'db.set_port_is_locked',
444 'data':
445 {'port_id': opts.unlock_port,
446 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000447 print "Unlocked port_id %d" % port_id
448 except InputError as inst:
449 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000450elif opts.set_port_current_vlan is not None:
451 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000452 port_id = call_vland('vlan_update',
453 {'command':'api.set_current_vlan',
454 'data':
455 {'port_id': opts.set_current_vlan[0],
456 'vlan_id': opts.set_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000457 print "Set current VLAN on port_id %d" % port_id
458 except InputError as inst:
459 print 'Failed: %s' % inst
460elif opts.get_port_current_vlan is not None:
461 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000462 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000463 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000464 'data':
465 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000466 if vlan_id is not None:
467 print vlan_id
468 else:
469 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
470 except InputError as inst:
471 print 'Failed: %s' % inst
472elif opts.set_port_base_vlan is not None:
473 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000474 port_id = call_vland('db_update',
475 {'command':'db.set_base_vlan',
476 'data':
477 {'port_id': opts.set_base_vlan[0],
478 'base_vlan_id': opts.set_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000479 print "Set base VLAN on port_id %d" % port_id
480 except InputError as inst:
481 print 'Failed: %s' % inst
482elif opts.get_port_base_vlan is not None:
483 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000484 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000485 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000486 'data':
487 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000488 if vlan_id is not None:
489 print vlan_id
490 else:
491 print "No base_vlan_id found for port_id %d" % port_id
492 except InputError as inst:
493 print 'Failed: %s' % inst
494elif opts.restore_port_to_base_vlan is not None:
495 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000496 port_id = call_vland('vlan_update',
497 {'command': 'api.restore_base_vlan',
498 'data':
499 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000500 print "Restored port_id %d back to base VLAN" % port_id
501 except InputError as inst:
502 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000503elif opts.show_vlan is not None:
504 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000505 v = call_vland('db_query',
506 {'command':'db.get_vlan_by_id',
507 'data':
508 {'vlan_id': opts.show_vlan}})
509 if v is not None:
510 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000511 else:
512 print 'No vlan found for vlan_id %d' % opts.show_vlan
513 except InputError as inst:
514 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000515elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000516 print 'Config:'
517 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000518 default_vlan_id = call_vland('db_query',
519 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000520 'data':
521 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000522 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 +0000523 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
524 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000525 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000526 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000527 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000528 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000529 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000530 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000531 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000532elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000533 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
534 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000535elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000536 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
537 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000538elif opts.version:
539 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000540elif opts.auto_import_switch:
541 print 'Attempting to import switch %s' % opts.auto_import_switch
542 if opts.auto_import_switch not in config.switches:
543 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
544 ret = call_vland('vlan_update',
545 {'command':'api.auto_import_switch',
546 'data':
547 {'switch': opts.auto_import_switch}})
Steve McIntyre110a5252014-12-24 02:13:13 +0000548 print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (opts.auto_import_switch, ret['switch_id'], ret['num_ports_added'], ret['num_vlans_added'])
Steve McIntyredeff7952014-12-23 13:45:59 +0000549elif opts.probe_switches:
550 print 'Asking VLANd to probe all the configured switches'
551 ret = call_vland('daemon_query',{'command':'daemon.probe_switches', 'data': None})
Steve McIntyredeff7952014-12-23 13:45:59 +0000552 for field in ret:
Steve McIntyred07c8572014-12-23 16:52:23 +0000553 print '%s: %s' % (field, ret[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000554else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000555 print 'No recognised command given. Try -h for help'