blob: cf647b056943972d3687254bb0096db36dcb70ba [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 McIntyre77fd71c2015-07-31 17:16:01 +010033version = "0.4-DEV"
Steve McIntyre844bfd42014-11-27 16:58:31 +000034banner = "Linaro VLANd admin interface, version %s" % version
35
Steve McIntyrec4890132015-08-07 15:19:11 +010036TRUNK_ID_NONE = -1
37
Steve McIntyreae95fd62014-12-05 16:51:41 +000038def is_positive(text):
Steve McIntyrec6895fc2014-12-19 15:01:08 +000039 if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000040 return True
Steve McIntyrec6895fc2014-12-19 15:01:08 +000041 elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000042 return False
43 else:
44 raise InputError("Cannot parse \"%s\" as True or False" % text)
45
Steve McIntyrea132c362014-12-05 15:53:21 +000046def dump_switch(switch):
Steve McIntyre018d30c2015-02-09 06:34:57 +000047 print "switch_id:%d name:%s" % (
48 int(switch[0]),
49 switch[1])
Steve McIntyrea132c362014-12-05 15:53:21 +000050
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000051def dump_port(port):
Steve McIntyrec4890132015-08-07 15:19:11 +010052 print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d number:%d trunk_id:%s" % (
Steve McIntyre018d30c2015-02-09 06:34:57 +000053 int(port[0]),
54 port[1],
55 int(port[2]),
56 ("yes" if port[3] is True else "no"),
57 ("trunk" if port[4] is True else "access"),
58 int(port[5]),
Steve McIntyre9951b1c2015-08-05 13:55:38 +010059 int(port[6]),
Steve McIntyrec4890132015-08-07 15:19:11 +010060 int(port[7]),
61 'None' if (TRUNK_ID_NONE == port[8]) else port[8])
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000062
Steve McIntyree41e3f32014-12-05 18:08:21 +000063def dump_vlan(vlan):
Steve McIntyre018d30c2015-02-09 06:34:57 +000064 print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % (
65 int(vlan[0]),
66 vlan[1],
67 int(vlan[2]),
68 ("yes" if vlan[3] is True else "no"),
69 vlan[4])
Steve McIntyree41e3f32014-12-05 18:08:21 +000070
Steve McIntyrec4890132015-08-07 15:19:11 +010071def dump_trunk(trunk):
72 print "trunk_id:%d creation_time:%s" % (
73 int(trunk[0]),
74 trunk[1])
75
Steve McIntyref1c04f92014-12-16 18:23:15 +000076def call_vland(msgtype, msg):
77 ipc = VlanIpc()
78 ipc.client_connect('localhost', config.vland.port)
79 msg['client_name'] = 'admin.py'
80 msg['type'] = msgtype
Steve McIntyref1c04f92014-12-16 18:23:15 +000081 ipc.client_send(msg)
82 ret = ipc.client_recv_and_close()
83 if 'response' not in ret:
84 raise SocketError("Badly-formed response from VLANd server")
85 if ret['response'] == "ERROR":
Steve McIntyre8a1a1ae2015-01-23 17:52:14 +000086 print "Input error: VLANd server said \"%s\"" % ret['error']
87 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000088 return ret['data']
89
Steve McIntyrec12f6312014-12-15 15:00:12 +000090config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000091
Steve McIntyre844bfd42014-11-27 16:58:31 +000092usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000093parser = optparse.OptionParser(usage=usage, description=banner)
94
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000095# System commands
96system_group = optparse.OptionGroup(parser, "System commands")
97system_group.add_option("--status",
98 dest="status",
99 action = "store_true",
100 default = False,
101 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +0000102system_group.add_option("--statistics",
103 dest="statistics",
104 action = "store_true",
105 default = False,
106 help = "Print some system statistics")
107system_group.add_option("--version",
108 dest="version",
109 action = "store_true",
110 default = False,
111 help = "Describe the version of this admin interface")
112system_group.add_option("--vland_version",
113 dest="vland_version",
114 action = "store_true",
115 default = False,
116 help = "Describe the version of the running VLANd")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000117system_group.add_option("--auto_import_switch",
Steve McIntyre6c562602014-12-22 16:10:54 +0000118 dest = "auto_import_switch",
119 action = "store",
120 type = "string",
121 help = "Attempt to import a switch's configuration into the VLANd system",
122 nargs = 1,
123 metavar = "<switch name>")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000124system_group.add_option("--probe_switches",
Steve McIntyredeff7952014-12-23 13:45:59 +0000125 dest = "probe_switches",
126 action = "store_true",
127 default = False,
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000128 help = "Probe all the configured switches")
Steve McIntyre06fe6422015-01-23 17:55:43 +0000129system_group.add_option("--shutdown",
130 dest = "shutdown",
131 action = "store_true",
132 default = False,
133 help = "Shut down a running VLANd")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000134parser.add_option_group(system_group)
135# May add shutdown, self-check etc. later
136
Steve McIntyre844bfd42014-11-27 16:58:31 +0000137# Switch commands
138switch_group = optparse.OptionGroup(parser, "Switch commands")
139switch_group.add_option("--list_all_switches",
140 dest = "list_all_switches",
141 action = "store_true",
142 default = False,
143 help = "List all the existing switches in the system")
144switch_group.add_option("--create_switch",
145 dest = "create_switch",
146 action = "store",
147 type = "string",
148 help = "Add a new switch to the system",
149 nargs = 1,
150 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000151switch_group.add_option("--delete_switch",
152 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000153 action = "store",
154 type = "int",
155 help = "Remove an existing switch from 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>")
159switch_group.add_option("--show_switch",
160 dest = "show_switch",
161 action = "store",
162 type = "int",
163 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000164 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000165 nargs = 1,
166 metavar = "<switch_id>")
167switch_group.add_option("--lookup_switch_by_name",
168 dest = "lookup_switch_by_name",
169 action = "store",
170 type = "string",
171 help = "Lookup a switch ID by name",
172 nargs = 1,
173 metavar = "<name>")
174switch_group.add_option("--list_switch_ports",
175 dest = "list_switch_ports",
176 action = "store",
177 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000178 help = "List the IDs of the ports on an existing switch 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 = "<switch_id>")
182parser.add_option_group(switch_group)
183
184# Port commands
185port_group = optparse.OptionGroup(parser, "Port commands")
186port_group.add_option("--list_all_ports",
187 dest = "list_all_ports",
188 action = "store_true",
189 default = False,
190 help = "List all the existing ports in the system")
191port_group.add_option("--create_port",
192 dest = "create_port",
193 action = "store",
194 type = "string",
195 help = "Add a new port to the system",
Steve McIntyreea753972015-08-05 13:52:48 +0100196 nargs = 3,
197 metavar = "<switch_id> <name> <number>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000198port_group.add_option("--delete_port",
199 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000200 action = "store",
201 type = "int",
202 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000203 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000204 nargs = 1,
205 metavar = "<port_id>")
206port_group.add_option("--show_port",
207 dest = "show_port",
208 action = "store",
209 type = "int",
210 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000211 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000212 nargs = 1,
213 metavar = "<port_id>")
214port_group.add_option("--lookup_port_by_switch_and_name",
215 dest = "lookup_port_by_switch_and_name",
216 action = "store",
217 type = "string",
218 help = "Lookup a port ID by switch and port name",
219 nargs = 2,
220 metavar = "<switch_id> <name>")
Steve McIntyre45f55012015-08-05 13:55:15 +0100221port_group.add_option("--lookup_port_by_switch_and_number",
222 dest = "lookup_port_by_switch_and_number",
223 action = "store",
224 type = "string",
225 help = "Lookup a port ID by switch and number",
226 nargs = 2,
227 metavar = "<switch_id> <name>")
Steve McIntyrec4890132015-08-07 15:19:11 +0100228port_group.add_option("--lookup_ports_by_switch",
229 dest = "lookup_ports_by_switch",
230 action = "store",
231 type = "string",
232 help = "Lookup port ID(s) by switch",
233 nargs = 1,
234 metavar = "<switch_id>")
235port_group.add_option("--lookup_ports_by_current_vlan",
236 dest = "lookup_ports_by_current_vlan",
237 action = "store",
238 type = "string",
239 help = "Lookup port ID(s) by current VLAN",
240 nargs = 1,
241 metavar = "<vlan_id>")
242port_group.add_option("--lookup_ports_by_base_vlan",
243 dest = "lookup_ports_by_base_vlan",
244 action = "store",
245 type = "string",
246 help = "Lookup port ID(s) by base VLAN",
247 nargs = 1,
248 metavar = "<vlan_id>")
249port_group.add_option("--lookup_ports_by_trunk",
250 dest = "lookup_ports_by_trunk",
251 action = "store",
252 type = "string",
253 help = "Lookup port ID(s) by trunk",
254 nargs = 1,
255 metavar = "<trunk_id>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000256port_group.add_option("--set_port_mode",
257 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000258 action = "store",
259 type = "string",
260 help = "Set the mode of a port to 'trunk' or 'access'",
261 nargs = 2,
262 metavar = "<port_id> <mode>")
263port_group.add_option("--lock_port",
264 dest = "lock_port",
265 action = "store",
266 type = "string",
267 help = "Lock the settings on a port",
268 nargs = 1,
269 metavar = "<port_id>")
270port_group.add_option("--unlock_port",
271 dest = "unlock_port",
272 action = "store",
273 type = "string",
274 help = "Unock the settings on a port",
275 nargs = 1,
276 metavar = "<port_id>")
277port_group.add_option("--set_port_current_vlan",
278 dest = "set_port_current_vlan",
279 action = "store",
280 type = "int",
281 help = "Set the current VLAN assignment for a port",
282 nargs = 2,
283 metavar = "<port_id> <vlan_id>")
284port_group.add_option("--get_port_current_vlan",
285 dest = "get_port_current_vlan",
286 action = "store",
287 type = "int",
288 help = "Get the current VLAN assignment for a port",
289 nargs = 1,
290 metavar = "<port_id>")
291port_group.add_option("--set_port_base_vlan",
292 dest = "set_port_base_vlan",
293 action = "store",
294 type = "int",
295 help = "Set the base VLAN assignment for a port",
296 nargs = 2,
297 metavar = "<port_id> <vlan_id>")
298port_group.add_option("--get_port_base_vlan",
299 dest = "get_port_base_vlan",
300 action = "store",
301 type = "int",
302 help = "Get the base VLAN assignment for a port",
303 nargs = 1,
304 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000305port_group.add_option("--restore_port_to_base_vlan",
306 dest = "restore_port_to_base_vlan",
307 action = "store",
308 type = "int",
309 help = "Reset the port back to its base VLAN",
310 nargs = 1,
311 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000312parser.add_option_group(port_group)
313
314# VLAN commands
315vlan_group = optparse.OptionGroup(parser, "VLAN commands")
316vlan_group.add_option("--list_all_vlans",
317 dest = "list_all_vlans",
318 action = "store_true",
319 default = False,
320 help = "List all the existing vlans in the system")
321vlan_group.add_option("--create_vlan",
322 dest = "create_vlan",
323 action = "store",
324 type = "string",
325 help = "Add a new vlan to the system",
326 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000327 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000328vlan_group.add_option("--delete_vlan",
329 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000330 action = "store",
331 type = "int",
332 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000333 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000334 nargs = 1,
335 metavar = "<vlan_id>")
336vlan_group.add_option("--show_vlan",
337 dest = "show_vlan",
338 action = "store",
339 type = "int",
340 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000341 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000342 nargs = 1,
343 metavar = "<vlan_id>")
Steve McIntyre65533d72015-01-23 18:01:17 +0000344vlan_group.add_option("--lookup_vlan_by_tag",
345 dest = "lookup_vlan_by_tag",
346 action = "store",
347 type = "int",
348 help = "Find the vlan ID of an existing vlan in the system",
349 default = None,
350 nargs = 1,
351 metavar = "<tag>")
352vlan_group.add_option("--show_vlan_tag",
353 dest = "show_vlan_tag",
354 action = "store",
355 type = "int",
356 help = "Print the vlan tag of an existing vlan in the system",
357 default = None,
358 nargs = 1,
359 metavar = "<vlan_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000360parser.add_option_group(vlan_group)
361
Steve McIntyrec4890132015-08-07 15:19:11 +0100362# Port commands
363trunk_group = optparse.OptionGroup(parser, "Trunk commands")
364trunk_group.add_option("--list_all_trunks",
365 dest = "list_all_trunks",
366 action = "store_true",
367 default = False,
368 help = "List all the existing trunks in the system")
369trunk_group.add_option("--create_trunk",
370 dest = "create_trunk",
371 action = "store",
372 type = "string",
373 help = "Add a new trunk to the system, linking two ports",
374 nargs = 2,
375 metavar = "<port_id1> <port_id2>")
376trunk_group.add_option("--delete_trunk",
377 dest = "delete_trunk",
378 action = "store",
379 type = "int",
380 help = "Remove an existing trunk from the system",
381 default = None,
382 nargs = 1,
383 metavar = "<trunk_id>")
384trunk_group.add_option("--show_trunk",
385 dest = "show_trunk",
386 action = "store",
387 type = "int",
388 help = "Show the details of an existing trunk in the system",
389 default = None,
390 nargs = 1,
391 metavar = "<trunk_id>")
392parser.add_option_group(trunk_group)
393
394
Steve McIntyre844bfd42014-11-27 16:58:31 +0000395(opts, args) = parser.parse_args()
396
Steve McIntyre844bfd42014-11-27 16:58:31 +0000397if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000398 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000399 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000400 dump_switch(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000401elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000402 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000403 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000404 dump_port(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000405elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000406 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000407 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000408 dump_vlan(line)
Steve McIntyrec4890132015-08-07 15:19:11 +0100409elif opts.list_all_trunks:
410 result = call_vland('db_query', {'command':'db.all_trunks', 'data':None})
411 for line in result:
412 dump_trunk(line)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000413elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000414 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000415 switch_id = call_vland('db_update',
416 {'command':'db.create_switch',
417 'data':
418 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000419 print 'Created switch_id %d' % switch_id
420 except InputError as inst:
421 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000422elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000423 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000424 port_id = call_vland('db_update',
425 {'command':'db.create_port',
426 'data':
427 {'switch_id': opts.create_port[0],
Steve McIntyreea753972015-08-05 13:52:48 +0100428 'name': opts.create_port[1],
429 'number': opts.create_port[2]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000430 print 'Created port_id %d' % port_id
431 except InputError as inst:
432 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000433elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000434 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100435 (vlan_id, vlan_tag) = call_vland('vlan_update',
436 {'command':'api.create_vlan',
437 'data':
438 {'name': opts.create_vlan[0],
439 'tag': opts.create_vlan[1],
440 'is_base_vlan': is_positive(opts.create_vlan[2])}})
441 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000442 except InputError as inst:
443 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100444elif opts.create_trunk is not None:
445 try:
446 trunk_id = call_vland('db_update',
447 {'command':'db.create_trunk',
448 'data':
449 {'port_id1': opts.create_trunk[0],
450 'port_id2': opts.create_trunk[1]}})
451 print 'Created trunk_id %d' % trunk_id
452 except InputError as inst:
453 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000454elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000455 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000456 switch_id = call_vland('db_update',
457 {'command':'db.delete_switch',
458 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000459 print 'Deleted switch_id %d' % switch_id
460 except InputError as inst:
461 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000462elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000463 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000464 port_id = call_vland('db_update',
465 {'command':'db.delete_port',
466 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000467 except InputError as inst:
468 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000469elif opts.delete_vlan is not None:
470 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000471 vlan_id = call_vland('vlan_update',
472 {'command':'api.delete_vlan',
473 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000474 print 'Deleted vlan_id %d' % vlan_id
475 except InputError as inst:
476 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100477elif opts.delete_trunk is not None:
478 try:
479 port_id = call_vland('db_update',
480 {'command':'db.delete_trunk',
481 'data': {'trunk_id': opts.delete_trunk}})
482 except InputError as inst:
483 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000484elif opts.lookup_switch_by_name is not None:
485 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000486 switch_id = call_vland('db_query',
487 {'command':'db.get_switch_id_by_name',
488 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000489 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000490 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000491 else:
492 print 'No switch found for name %s' % opts.lookup_switch_by_name
493 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100494 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000495elif opts.show_switch is not None:
496 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000497 this_switch = call_vland('db_query',
498 {'command':'db.get_switch_by_id',
499 'data':
500 {'switch_id': opts.show_switch}})
501 if this_switch is not None:
502 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000503 else:
504 print 'No switch found for switch_id %d' % opts.show_switch
505 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100506 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000507elif opts.list_switch_ports is not None:
508 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000509 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000510 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000511 'data':
512 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000513 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000514 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000515 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000516 else:
517 print 'No ports found for switch_id %d' % opts.list_switch_ports
518 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100519 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000520elif opts.show_port is not None:
521 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000522 this_port = call_vland('db_query',
523 {'command':'db.get_port_by_id',
524 'data':
525 {'port_id': opts.show_port}})
526 if this_port is not None:
527 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000528 else:
529 print 'No port found for port_id %d' % opts.show_port
530 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100531 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000532elif opts.lookup_port_by_switch_and_name is not None:
533 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000534 p = call_vland('db_query',
535 {'command':'db.get_port_by_switch_and_name',
536 'data':
537 {'switch_id': opts.lookup_port_by_switch_and_name[0],
538 'name': opts.lookup_port_by_switch_and_name[1]}})
539 if p is not None:
540 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000541 else:
542 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])
543 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100544 print 'Failed: %s' % inst
Steve McIntyre45f55012015-08-05 13:55:15 +0100545elif opts.lookup_port_by_switch_and_number is not None:
546 try:
547 p = call_vland('db_query',
548 {'command':'db.get_port_by_switch_and_number',
549 'data':
550 {'switch_id': opts.lookup_port_by_switch_and_number[0],
551 'number': opts.lookup_port_by_switch_and_number[1]}})
552 if p is not None:
553 print p
554 else:
555 print 'No port found for switch_id %d, port number %d' % (int(opts.lookup_port_by_switch_and_number[0]), int(opts.lookup_port_by_switch_and_number[1]))
556 except InputError as inst:
557 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100558elif opts.lookup_ports_by_switch is not None:
559 try:
560 p = call_vland('db_query',
561 {'command':'db.get_ports_by_switch',
562 'data':
563 {'switch_id': opts.lookup_ports_by_switch}})
564 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100565 for port_id in p:
566 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100567 else:
568 print 'No ports found for switch_id %d' % int(opts.lookup_ports_by_switch)
569 except InputError as inst:
570 print 'Failed: %s' % inst
571elif opts.lookup_ports_by_current_vlan is not None:
572 try:
573 p = call_vland('db_query',
574 {'command':'db.get_ports_by_current_vlan',
575 'data':
576 {'vlan_id': opts.lookup_ports_by_current_vlan}})
577 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100578 for port_id in p:
579 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100580 else:
581 print 'No ports found for current vlan_id %d' % int(opts.lookup_ports_by_current_vlan)
582 except InputError as inst:
583 print 'Failed: %s' % inst
584elif opts.lookup_ports_by_base_vlan is not None:
585 try:
586 p = call_vland('db_query',
587 {'command':'db.get_ports_by_base_vlan',
588 'data':
589 {'vlan_id': opts.lookup_ports_by_base_vlan}})
590 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100591 for port_id in p:
592 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100593 else:
594 print 'No ports found for base vlan_id %d' % int(opts.lookup_ports_by_base_vlan)
595 except InputError as inst:
596 print 'Failed: %s' % inst
597elif opts.lookup_ports_by_trunk is not None:
598 try:
599 p = call_vland('db_query',
600 {'command':'db.get_ports_by_trunk',
601 'data':
602 {'trunk_id': opts.lookup_ports_by_trunk}})
603 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100604 for port_id in p:
605 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100606 else:
607 print 'No ports found for trunk_id %d' % int(opts.lookup_ports_by_trunk)
608 except InputError as inst:
609 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000610elif opts.set_port_mode is not None:
611 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000612 port_id = call_vland('vlan_update',
613 {'command':'api.set_port_mode',
614 'data':
615 {'port_id': opts.set_port_mode[0],
616 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000617 print "Updated mode for port_id %d" % port_id
618 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100619 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000620elif opts.lock_port is not None:
621 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000622 port_id = call_vland('db_update',
623 {'command':'db.set_port_is_locked',
624 'data':
625 {'port_id': opts.lock_port,
626 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000627 print "Locked port_id %d" % port_id
628 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100629 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000630elif opts.unlock_port is not None:
631 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000632 port_id = call_vland('db_update',
633 {'command':'db.set_port_is_locked',
634 'data':
635 {'port_id': opts.unlock_port,
636 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000637 print "Unlocked port_id %d" % port_id
638 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100639 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000640elif opts.set_port_current_vlan is not None:
641 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000642 port_id = call_vland('vlan_update',
643 {'command':'api.set_current_vlan',
644 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000645 {'port_id': opts.set_port_current_vlan[0],
646 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000647 print "Set current VLAN on port_id %d" % port_id
648 except InputError as inst:
649 print 'Failed: %s' % inst
650elif opts.get_port_current_vlan is not None:
651 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000652 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000653 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000654 'data':
655 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000656 if vlan_id is not None:
657 print vlan_id
658 else:
659 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
660 except InputError as inst:
661 print 'Failed: %s' % inst
662elif opts.set_port_base_vlan is not None:
663 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000664 port_id = call_vland('db_update',
665 {'command':'db.set_base_vlan',
666 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100667 {'port_id': opts.set_port_base_vlan[0],
668 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000669 print "Set base VLAN on port_id %d" % port_id
670 except InputError as inst:
671 print 'Failed: %s' % inst
672elif opts.get_port_base_vlan is not None:
673 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000674 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000675 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000676 'data':
677 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000678 if vlan_id is not None:
679 print vlan_id
680 else:
681 print "No base_vlan_id found for port_id %d" % port_id
682 except InputError as inst:
683 print 'Failed: %s' % inst
684elif opts.restore_port_to_base_vlan is not None:
685 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000686 port_id = call_vland('vlan_update',
687 {'command': 'api.restore_base_vlan',
688 'data':
689 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000690 print "Restored port_id %d back to base VLAN" % port_id
691 except InputError as inst:
692 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000693elif opts.show_vlan is not None:
694 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000695 v = call_vland('db_query',
696 {'command':'db.get_vlan_by_id',
697 'data':
698 {'vlan_id': opts.show_vlan}})
699 if v is not None:
700 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000701 else:
702 print 'No vlan found for vlan_id %d' % opts.show_vlan
703 except InputError as inst:
704 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000705elif opts.lookup_vlan_by_tag is not None:
706 try:
707 vlan_id = call_vland('db_query',
708 {'command':'db.get_vlan_id_by_tag',
709 'data':
710 {'tag': opts.lookup_vlan_by_tag}})
711 if vlan_id is not None:
712 print vlan_id
713 else:
714 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
715 except InputError as inst:
716 print 'Failed: %s' % inst
717elif opts.show_vlan_tag is not None:
718 try:
719 vlan_tag = call_vland('db_query',
720 {'command':'db.get_vlan_tag_by_id',
721 'data':
722 {'vlan_id': opts.show_vlan_tag}})
723 if vlan_tag is not None:
724 print vlan_tag
725 else:
726 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
727 except InputError as inst:
728 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100729elif opts.show_trunk is not None:
730 try:
731 this_trunk = call_vland('db_query',
732 {'command':'db.get_trunk_by_id',
733 'data':
734 {'trunk_id': opts.show_trunk}})
735 if this_trunk is not None:
736 dump_trunk(this_trunk)
737 else:
738 print 'No port found for port_id %d' % opts.show_port
739 except InputError as inst:
740 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000741elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000742 print 'Config:'
743 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000744 default_vlan_id = call_vland('db_query',
745 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000746 'data':
747 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000748 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 +0000749 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
750 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000751 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000752 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000753 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000754 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000755 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000756 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000757 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000758elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000759 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
760 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000761elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000762 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
763 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000764elif opts.version:
765 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000766elif opts.auto_import_switch:
767 print 'Attempting to import switch %s' % opts.auto_import_switch
768 if opts.auto_import_switch not in config.switches:
769 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100770 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000771 {'command':'api.auto_import_switch',
772 'data':
773 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100774 print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (opts.auto_import_switch, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added'])
Steve McIntyredeff7952014-12-23 13:45:59 +0000775elif opts.probe_switches:
776 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100777 probe = call_vland('daemon_query',
778 {'command':'daemon.probe_switches',
779 'data': None})
780 for field in probe:
781 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000782elif opts.shutdown:
783 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100784 shutdown = call_vland('daemon_query',
785 {'command':'daemon.shutdown',
786 'data': None})
787 for field in shutdown:
788 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000789else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000790 print 'No recognised command given. Try -h for help'