blob: 1b345ee6d7f6f21d6785edf2d461c221a6b8ec38 [file] [log] [blame]
Steve McIntyre844bfd42014-11-27 16:58:31 +00001#! /usr/bin/python
2
Steve McIntyre94ef65e2015-09-25 01:08:14 +01003# Copyright 2014-2015 Linaro Limited
Steve McIntyre844bfd42014-11-27 16:58:31 +00004#
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
Steve McIntyreea343aa2015-10-23 17:46:17 +010025import datetime, time
Steve McIntyre844bfd42014-11-27 16:58:31 +000026
27vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
28sys.path.insert(0, vlandpath)
29
Steve McIntyre5694f6d2014-12-18 16:43:30 +000030from errors import InputError, SocketError
Steve McIntyre21f02702014-12-16 18:19:47 +000031from config.config import VlanConfig
32from ipc.ipc import VlanIpc
Steve McIntyre844bfd42014-11-27 16:58:31 +000033
Steve McIntyre00dc61b2015-09-28 02:44:29 +010034version = "0.5-DEV"
Steve McIntyre844bfd42014-11-27 16:58:31 +000035banner = "Linaro VLANd admin interface, version %s" % version
36
Steve McIntyrec4890132015-08-07 15:19:11 +010037TRUNK_ID_NONE = -1
38
Steve McIntyreae95fd62014-12-05 16:51:41 +000039def is_positive(text):
Steve McIntyrec6895fc2014-12-19 15:01:08 +000040 if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000041 return True
Steve McIntyrec6895fc2014-12-19 15:01:08 +000042 elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000043 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):
Steve McIntyre018d30c2015-02-09 06:34:57 +000048 print "switch_id:%d name:%s" % (
49 int(switch[0]),
50 switch[1])
Steve McIntyrea132c362014-12-05 15:53:21 +000051
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000052def dump_port(port):
Steve McIntyrec4890132015-08-07 15:19:11 +010053 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 +000054 int(port[0]),
55 port[1],
56 int(port[2]),
57 ("yes" if port[3] is True else "no"),
58 ("trunk" if port[4] is True else "access"),
59 int(port[5]),
Steve McIntyre9951b1c2015-08-05 13:55:38 +010060 int(port[6]),
Steve McIntyrec4890132015-08-07 15:19:11 +010061 int(port[7]),
62 'None' if (TRUNK_ID_NONE == port[8]) else port[8])
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000063
Steve McIntyree41e3f32014-12-05 18:08:21 +000064def dump_vlan(vlan):
Steve McIntyre018d30c2015-02-09 06:34:57 +000065 print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % (
66 int(vlan[0]),
67 vlan[1],
68 int(vlan[2]),
69 ("yes" if vlan[3] is True else "no"),
70 vlan[4])
Steve McIntyree41e3f32014-12-05 18:08:21 +000071
Steve McIntyrec4890132015-08-07 15:19:11 +010072def dump_trunk(trunk):
73 print "trunk_id:%d creation_time:%s" % (
74 int(trunk[0]),
75 trunk[1])
76
Steve McIntyref1c04f92014-12-16 18:23:15 +000077def call_vland(msgtype, msg):
78 ipc = VlanIpc()
79 ipc.client_connect('localhost', config.vland.port)
80 msg['client_name'] = 'admin.py'
81 msg['type'] = msgtype
Steve McIntyref1c04f92014-12-16 18:23:15 +000082 ipc.client_send(msg)
83 ret = ipc.client_recv_and_close()
84 if 'response' not in ret:
85 raise SocketError("Badly-formed response from VLANd server")
86 if ret['response'] == "ERROR":
Steve McIntyre8a1a1ae2015-01-23 17:52:14 +000087 print "Input error: VLANd server said \"%s\"" % ret['error']
88 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000089 return ret['data']
90
Steve McIntyrec12f6312014-12-15 15:00:12 +000091config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000092
Steve McIntyre844bfd42014-11-27 16:58:31 +000093usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000094parser = optparse.OptionParser(usage=usage, description=banner)
95
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000096# System commands
97system_group = optparse.OptionGroup(parser, "System commands")
98system_group.add_option("--status",
99 dest="status",
100 action = "store_true",
101 default = False,
102 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +0000103system_group.add_option("--statistics",
104 dest="statistics",
105 action = "store_true",
106 default = False,
107 help = "Print some system statistics")
108system_group.add_option("--version",
109 dest="version",
110 action = "store_true",
111 default = False,
112 help = "Describe the version of this admin interface")
113system_group.add_option("--vland_version",
114 dest="vland_version",
115 action = "store_true",
116 default = False,
117 help = "Describe the version of the running VLANd")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000118system_group.add_option("--auto_import_switch",
Steve McIntyre6c562602014-12-22 16:10:54 +0000119 dest = "auto_import_switch",
120 action = "store",
121 type = "string",
122 help = "Attempt to import a switch's configuration into the VLANd system",
123 nargs = 1,
124 metavar = "<switch name>")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000125system_group.add_option("--probe_switches",
Steve McIntyredeff7952014-12-23 13:45:59 +0000126 dest = "probe_switches",
127 action = "store_true",
128 default = False,
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000129 help = "Probe all the configured switches")
Steve McIntyre06fe6422015-01-23 17:55:43 +0000130system_group.add_option("--shutdown",
131 dest = "shutdown",
132 action = "store_true",
133 default = False,
134 help = "Shut down a running VLANd")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000135parser.add_option_group(system_group)
136# May add shutdown, self-check etc. later
137
Steve McIntyre844bfd42014-11-27 16:58:31 +0000138# Switch commands
139switch_group = optparse.OptionGroup(parser, "Switch commands")
140switch_group.add_option("--list_all_switches",
141 dest = "list_all_switches",
142 action = "store_true",
143 default = False,
144 help = "List all the existing switches in the system")
145switch_group.add_option("--create_switch",
146 dest = "create_switch",
147 action = "store",
148 type = "string",
149 help = "Add a new switch to the system",
150 nargs = 1,
151 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000152switch_group.add_option("--delete_switch",
153 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000154 action = "store",
155 type = "int",
156 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000157 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000158 nargs = 1,
159 metavar = "<switch_id>")
160switch_group.add_option("--show_switch",
161 dest = "show_switch",
162 action = "store",
163 type = "int",
164 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000165 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000166 nargs = 1,
167 metavar = "<switch_id>")
168switch_group.add_option("--lookup_switch_by_name",
169 dest = "lookup_switch_by_name",
170 action = "store",
171 type = "string",
172 help = "Lookup a switch ID by name",
173 nargs = 1,
174 metavar = "<name>")
175switch_group.add_option("--list_switch_ports",
176 dest = "list_switch_ports",
177 action = "store",
178 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000179 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000180 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000181 nargs = 1,
182 metavar = "<switch_id>")
183parser.add_option_group(switch_group)
184
185# Port commands
186port_group = optparse.OptionGroup(parser, "Port commands")
187port_group.add_option("--list_all_ports",
188 dest = "list_all_ports",
189 action = "store_true",
190 default = False,
191 help = "List all the existing ports in the system")
192port_group.add_option("--create_port",
193 dest = "create_port",
194 action = "store",
195 type = "string",
196 help = "Add a new port to the system",
Steve McIntyreea753972015-08-05 13:52:48 +0100197 nargs = 3,
198 metavar = "<switch_id> <name> <number>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000199port_group.add_option("--delete_port",
200 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000201 action = "store",
202 type = "int",
203 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000204 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000205 nargs = 1,
206 metavar = "<port_id>")
207port_group.add_option("--show_port",
208 dest = "show_port",
209 action = "store",
210 type = "int",
211 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000212 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000213 nargs = 1,
214 metavar = "<port_id>")
215port_group.add_option("--lookup_port_by_switch_and_name",
216 dest = "lookup_port_by_switch_and_name",
217 action = "store",
218 type = "string",
219 help = "Lookup a port ID by switch and port name",
220 nargs = 2,
221 metavar = "<switch_id> <name>")
Steve McIntyre45f55012015-08-05 13:55:15 +0100222port_group.add_option("--lookup_port_by_switch_and_number",
223 dest = "lookup_port_by_switch_and_number",
224 action = "store",
225 type = "string",
226 help = "Lookup a port ID by switch and number",
227 nargs = 2,
228 metavar = "<switch_id> <name>")
Steve McIntyrec4890132015-08-07 15:19:11 +0100229port_group.add_option("--lookup_ports_by_switch",
230 dest = "lookup_ports_by_switch",
231 action = "store",
232 type = "string",
233 help = "Lookup port ID(s) by switch",
234 nargs = 1,
235 metavar = "<switch_id>")
236port_group.add_option("--lookup_ports_by_current_vlan",
237 dest = "lookup_ports_by_current_vlan",
238 action = "store",
239 type = "string",
240 help = "Lookup port ID(s) by current VLAN",
241 nargs = 1,
242 metavar = "<vlan_id>")
243port_group.add_option("--lookup_ports_by_base_vlan",
244 dest = "lookup_ports_by_base_vlan",
245 action = "store",
246 type = "string",
247 help = "Lookup port ID(s) by base VLAN",
248 nargs = 1,
249 metavar = "<vlan_id>")
250port_group.add_option("--lookup_ports_by_trunk",
251 dest = "lookup_ports_by_trunk",
252 action = "store",
253 type = "string",
254 help = "Lookup port ID(s) by trunk",
255 nargs = 1,
256 metavar = "<trunk_id>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000257port_group.add_option("--set_port_mode",
258 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000259 action = "store",
260 type = "string",
261 help = "Set the mode of a port to 'trunk' or 'access'",
262 nargs = 2,
263 metavar = "<port_id> <mode>")
264port_group.add_option("--lock_port",
265 dest = "lock_port",
266 action = "store",
267 type = "string",
268 help = "Lock the settings on a port",
269 nargs = 1,
270 metavar = "<port_id>")
271port_group.add_option("--unlock_port",
272 dest = "unlock_port",
273 action = "store",
274 type = "string",
275 help = "Unock the settings on a port",
276 nargs = 1,
277 metavar = "<port_id>")
278port_group.add_option("--set_port_current_vlan",
279 dest = "set_port_current_vlan",
280 action = "store",
281 type = "int",
282 help = "Set the current VLAN assignment for a port",
283 nargs = 2,
284 metavar = "<port_id> <vlan_id>")
285port_group.add_option("--get_port_current_vlan",
286 dest = "get_port_current_vlan",
287 action = "store",
288 type = "int",
289 help = "Get the current VLAN assignment for a port",
290 nargs = 1,
291 metavar = "<port_id>")
292port_group.add_option("--set_port_base_vlan",
293 dest = "set_port_base_vlan",
294 action = "store",
295 type = "int",
296 help = "Set the base VLAN assignment for a port",
297 nargs = 2,
298 metavar = "<port_id> <vlan_id>")
299port_group.add_option("--get_port_base_vlan",
300 dest = "get_port_base_vlan",
301 action = "store",
302 type = "int",
303 help = "Get the base VLAN assignment for a port",
304 nargs = 1,
305 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000306port_group.add_option("--restore_port_to_base_vlan",
307 dest = "restore_port_to_base_vlan",
308 action = "store",
309 type = "int",
310 help = "Reset the port back to its base VLAN",
311 nargs = 1,
312 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000313parser.add_option_group(port_group)
314
315# VLAN commands
316vlan_group = optparse.OptionGroup(parser, "VLAN commands")
317vlan_group.add_option("--list_all_vlans",
318 dest = "list_all_vlans",
319 action = "store_true",
320 default = False,
321 help = "List all the existing vlans in the system")
322vlan_group.add_option("--create_vlan",
323 dest = "create_vlan",
324 action = "store",
325 type = "string",
326 help = "Add a new vlan to the system",
327 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000328 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000329vlan_group.add_option("--delete_vlan",
330 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000331 action = "store",
332 type = "int",
333 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000334 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000335 nargs = 1,
336 metavar = "<vlan_id>")
337vlan_group.add_option("--show_vlan",
338 dest = "show_vlan",
339 action = "store",
340 type = "int",
341 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000342 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000343 nargs = 1,
344 metavar = "<vlan_id>")
Steve McIntyre65533d72015-01-23 18:01:17 +0000345vlan_group.add_option("--lookup_vlan_by_tag",
346 dest = "lookup_vlan_by_tag",
347 action = "store",
348 type = "int",
349 help = "Find the vlan ID of an existing vlan in the system",
350 default = None,
351 nargs = 1,
352 metavar = "<tag>")
353vlan_group.add_option("--show_vlan_tag",
354 dest = "show_vlan_tag",
355 action = "store",
356 type = "int",
357 help = "Print the vlan tag of an existing vlan in the system",
358 default = None,
359 nargs = 1,
360 metavar = "<vlan_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000361parser.add_option_group(vlan_group)
362
Steve McIntyrec4890132015-08-07 15:19:11 +0100363# Port commands
364trunk_group = optparse.OptionGroup(parser, "Trunk commands")
365trunk_group.add_option("--list_all_trunks",
366 dest = "list_all_trunks",
367 action = "store_true",
368 default = False,
369 help = "List all the existing trunks in the system")
370trunk_group.add_option("--create_trunk",
371 dest = "create_trunk",
372 action = "store",
373 type = "string",
374 help = "Add a new trunk to the system, linking two ports",
375 nargs = 2,
376 metavar = "<port_id1> <port_id2>")
377trunk_group.add_option("--delete_trunk",
378 dest = "delete_trunk",
379 action = "store",
380 type = "int",
381 help = "Remove an existing trunk from the system",
382 default = None,
383 nargs = 1,
384 metavar = "<trunk_id>")
385trunk_group.add_option("--show_trunk",
386 dest = "show_trunk",
387 action = "store",
388 type = "int",
389 help = "Show the details of an existing trunk in the system",
390 default = None,
391 nargs = 1,
392 metavar = "<trunk_id>")
393parser.add_option_group(trunk_group)
394
395
Steve McIntyre844bfd42014-11-27 16:58:31 +0000396(opts, args) = parser.parse_args()
397
Steve McIntyre844bfd42014-11-27 16:58:31 +0000398if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000399 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000400 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000401 dump_switch(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000402elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000403 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000404 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000405 dump_port(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000406elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000407 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000408 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000409 dump_vlan(line)
Steve McIntyrec4890132015-08-07 15:19:11 +0100410elif opts.list_all_trunks:
411 result = call_vland('db_query', {'command':'db.all_trunks', 'data':None})
412 for line in result:
413 dump_trunk(line)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000414elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000415 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000416 switch_id = call_vland('db_update',
417 {'command':'db.create_switch',
418 'data':
419 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000420 print 'Created switch_id %d' % switch_id
421 except InputError as inst:
422 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000423elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000424 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000425 port_id = call_vland('db_update',
426 {'command':'db.create_port',
427 'data':
428 {'switch_id': opts.create_port[0],
Steve McIntyreea753972015-08-05 13:52:48 +0100429 'name': opts.create_port[1],
430 'number': opts.create_port[2]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000431 print 'Created port_id %d' % port_id
432 except InputError as inst:
433 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000434elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000435 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100436 (vlan_id, vlan_tag) = call_vland('vlan_update',
437 {'command':'api.create_vlan',
438 'data':
439 {'name': opts.create_vlan[0],
440 'tag': opts.create_vlan[1],
441 'is_base_vlan': is_positive(opts.create_vlan[2])}})
442 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000443 except InputError as inst:
444 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100445elif opts.create_trunk is not None:
446 try:
447 trunk_id = call_vland('db_update',
448 {'command':'db.create_trunk',
449 'data':
450 {'port_id1': opts.create_trunk[0],
451 'port_id2': opts.create_trunk[1]}})
452 print 'Created trunk_id %d' % trunk_id
453 except InputError as inst:
454 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000455elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000456 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000457 switch_id = call_vland('db_update',
458 {'command':'db.delete_switch',
459 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000460 print 'Deleted switch_id %d' % switch_id
461 except InputError as inst:
462 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000463elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000464 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000465 port_id = call_vland('db_update',
466 {'command':'db.delete_port',
467 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000468 except InputError as inst:
469 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000470elif opts.delete_vlan is not None:
471 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000472 vlan_id = call_vland('vlan_update',
473 {'command':'api.delete_vlan',
474 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000475 print 'Deleted vlan_id %d' % vlan_id
476 except InputError as inst:
477 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100478elif opts.delete_trunk is not None:
479 try:
480 port_id = call_vland('db_update',
481 {'command':'db.delete_trunk',
482 'data': {'trunk_id': opts.delete_trunk}})
483 except InputError as inst:
484 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000485elif opts.lookup_switch_by_name is not None:
486 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000487 switch_id = call_vland('db_query',
488 {'command':'db.get_switch_id_by_name',
489 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000490 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000491 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000492 else:
493 print 'No switch found for name %s' % opts.lookup_switch_by_name
494 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100495 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000496elif opts.show_switch is not None:
497 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000498 this_switch = call_vland('db_query',
499 {'command':'db.get_switch_by_id',
500 'data':
501 {'switch_id': opts.show_switch}})
502 if this_switch is not None:
503 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000504 else:
505 print 'No switch found for switch_id %d' % opts.show_switch
506 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100507 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000508elif opts.list_switch_ports is not None:
509 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000510 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000511 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000512 'data':
513 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000514 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000515 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000516 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000517 else:
518 print 'No ports found for switch_id %d' % opts.list_switch_ports
519 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100520 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000521elif opts.show_port is not None:
522 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000523 this_port = call_vland('db_query',
524 {'command':'db.get_port_by_id',
525 'data':
526 {'port_id': opts.show_port}})
527 if this_port is not None:
528 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000529 else:
530 print 'No port found for port_id %d' % opts.show_port
531 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100532 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000533elif opts.lookup_port_by_switch_and_name is not None:
534 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000535 p = call_vland('db_query',
536 {'command':'db.get_port_by_switch_and_name',
537 'data':
538 {'switch_id': opts.lookup_port_by_switch_and_name[0],
539 'name': opts.lookup_port_by_switch_and_name[1]}})
540 if p is not None:
541 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000542 else:
543 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])
544 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100545 print 'Failed: %s' % inst
Steve McIntyre45f55012015-08-05 13:55:15 +0100546elif opts.lookup_port_by_switch_and_number is not None:
547 try:
548 p = call_vland('db_query',
549 {'command':'db.get_port_by_switch_and_number',
550 'data':
551 {'switch_id': opts.lookup_port_by_switch_and_number[0],
552 'number': opts.lookup_port_by_switch_and_number[1]}})
553 if p is not None:
554 print p
555 else:
556 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]))
557 except InputError as inst:
558 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100559elif opts.lookup_ports_by_switch is not None:
560 try:
561 p = call_vland('db_query',
562 {'command':'db.get_ports_by_switch',
563 'data':
564 {'switch_id': opts.lookup_ports_by_switch}})
565 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100566 for port_id in p:
567 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100568 else:
569 print 'No ports found for switch_id %d' % int(opts.lookup_ports_by_switch)
570 except InputError as inst:
571 print 'Failed: %s' % inst
572elif opts.lookup_ports_by_current_vlan is not None:
573 try:
574 p = call_vland('db_query',
575 {'command':'db.get_ports_by_current_vlan',
576 'data':
577 {'vlan_id': opts.lookup_ports_by_current_vlan}})
578 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100579 for port_id in p:
580 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100581 else:
582 print 'No ports found for current vlan_id %d' % int(opts.lookup_ports_by_current_vlan)
583 except InputError as inst:
584 print 'Failed: %s' % inst
585elif opts.lookup_ports_by_base_vlan is not None:
586 try:
587 p = call_vland('db_query',
588 {'command':'db.get_ports_by_base_vlan',
589 'data':
590 {'vlan_id': opts.lookup_ports_by_base_vlan}})
591 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100592 for port_id in p:
593 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100594 else:
595 print 'No ports found for base vlan_id %d' % int(opts.lookup_ports_by_base_vlan)
596 except InputError as inst:
597 print 'Failed: %s' % inst
598elif opts.lookup_ports_by_trunk is not None:
599 try:
600 p = call_vland('db_query',
601 {'command':'db.get_ports_by_trunk',
602 'data':
603 {'trunk_id': opts.lookup_ports_by_trunk}})
604 if p is not None:
Steve McIntyre913c0e82015-09-18 14:23:13 +0100605 for port_id in p:
606 print port_id
Steve McIntyrec4890132015-08-07 15:19:11 +0100607 else:
608 print 'No ports found for trunk_id %d' % int(opts.lookup_ports_by_trunk)
609 except InputError as inst:
610 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000611elif opts.set_port_mode is not None:
612 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000613 port_id = call_vland('vlan_update',
614 {'command':'api.set_port_mode',
615 'data':
616 {'port_id': opts.set_port_mode[0],
617 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000618 print "Updated mode for port_id %d" % port_id
619 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100620 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000621elif opts.lock_port is not None:
622 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000623 port_id = call_vland('db_update',
624 {'command':'db.set_port_is_locked',
625 'data':
626 {'port_id': opts.lock_port,
627 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000628 print "Locked port_id %d" % port_id
629 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100630 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000631elif opts.unlock_port is not None:
632 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000633 port_id = call_vland('db_update',
634 {'command':'db.set_port_is_locked',
635 'data':
636 {'port_id': opts.unlock_port,
637 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000638 print "Unlocked port_id %d" % port_id
639 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100640 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000641elif opts.set_port_current_vlan is not None:
642 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000643 port_id = call_vland('vlan_update',
644 {'command':'api.set_current_vlan',
645 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000646 {'port_id': opts.set_port_current_vlan[0],
647 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000648 print "Set current VLAN on port_id %d" % port_id
649 except InputError as inst:
650 print 'Failed: %s' % inst
651elif opts.get_port_current_vlan is not None:
652 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000653 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000654 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000655 'data':
656 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000657 if vlan_id is not None:
658 print vlan_id
659 else:
660 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
661 except InputError as inst:
662 print 'Failed: %s' % inst
663elif opts.set_port_base_vlan is not None:
664 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000665 port_id = call_vland('db_update',
666 {'command':'db.set_base_vlan',
667 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100668 {'port_id': opts.set_port_base_vlan[0],
669 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000670 print "Set base VLAN on port_id %d" % port_id
671 except InputError as inst:
672 print 'Failed: %s' % inst
673elif opts.get_port_base_vlan is not None:
674 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000675 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000676 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000677 'data':
678 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000679 if vlan_id is not None:
680 print vlan_id
681 else:
682 print "No base_vlan_id found for port_id %d" % port_id
683 except InputError as inst:
684 print 'Failed: %s' % inst
685elif opts.restore_port_to_base_vlan is not None:
686 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000687 port_id = call_vland('vlan_update',
688 {'command': 'api.restore_base_vlan',
689 'data':
690 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000691 print "Restored port_id %d back to base VLAN" % port_id
692 except InputError as inst:
693 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000694elif opts.show_vlan is not None:
695 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000696 v = call_vland('db_query',
697 {'command':'db.get_vlan_by_id',
698 'data':
699 {'vlan_id': opts.show_vlan}})
700 if v is not None:
701 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000702 else:
703 print 'No vlan found for vlan_id %d' % opts.show_vlan
704 except InputError as inst:
705 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000706elif opts.lookup_vlan_by_tag is not None:
707 try:
708 vlan_id = call_vland('db_query',
709 {'command':'db.get_vlan_id_by_tag',
710 'data':
711 {'tag': opts.lookup_vlan_by_tag}})
712 if vlan_id is not None:
713 print vlan_id
714 else:
715 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
716 except InputError as inst:
717 print 'Failed: %s' % inst
718elif opts.show_vlan_tag is not None:
719 try:
720 vlan_tag = call_vland('db_query',
721 {'command':'db.get_vlan_tag_by_id',
722 'data':
723 {'vlan_id': opts.show_vlan_tag}})
724 if vlan_tag is not None:
725 print vlan_tag
726 else:
727 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
728 except InputError as inst:
729 print 'Failed: %s' % inst
Steve McIntyrec4890132015-08-07 15:19:11 +0100730elif opts.show_trunk is not None:
731 try:
732 this_trunk = call_vland('db_query',
733 {'command':'db.get_trunk_by_id',
734 'data':
735 {'trunk_id': opts.show_trunk}})
736 if this_trunk is not None:
737 dump_trunk(this_trunk)
738 else:
739 print 'No port found for port_id %d' % opts.show_port
740 except InputError as inst:
741 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000742elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000743 print 'Config:'
744 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000745 default_vlan_id = call_vland('db_query',
746 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000747 'data':
748 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000749 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 +0000750 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
751 print 'VLANd is running %s' % stat['running']
Steve McIntyreaf24aaa2015-10-23 17:59:04 +0100752 lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f')
Steve McIntyreea343aa2015-10-23 17:46:17 +0100753 print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z')
Steve McIntyref1c04f92014-12-16 18:23:15 +0000754 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000755 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000756 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000757 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000758 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000759 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000760 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000761elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000762 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
763 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000764elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000765 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
766 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000767elif opts.version:
768 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000769elif opts.auto_import_switch:
770 print 'Attempting to import switch %s' % opts.auto_import_switch
771 if opts.auto_import_switch not in config.switches:
772 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100773 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000774 {'command':'api.auto_import_switch',
775 'data':
776 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100777 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 +0000778elif opts.probe_switches:
779 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100780 probe = call_vland('daemon_query',
781 {'command':'daemon.probe_switches',
782 'data': None})
783 for field in probe:
784 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000785elif opts.shutdown:
786 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100787 shutdown = call_vland('daemon_query',
788 {'command':'daemon.shutdown',
789 'data': None})
790 for field in shutdown:
791 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000792else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000793 print 'No recognised command given. Try -h for help'