blob: 2e95ae38926cddc7fc7563be2ef28819086b5c15 [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 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):
Steve McIntyre018d30c2015-02-09 06:34:57 +000045 print "switch_id:%d name:%s" % (
46 int(switch[0]),
47 switch[1])
Steve McIntyrea132c362014-12-05 15:53:21 +000048
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000049def dump_port(port):
Steve McIntyre018d30c2015-02-09 06:34:57 +000050 print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d" % (
51 int(port[0]),
52 port[1],
53 int(port[2]),
54 ("yes" if port[3] is True else "no"),
55 ("trunk" if port[4] is True else "access"),
56 int(port[5]),
57 int(port[6]))
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000058
Steve McIntyree41e3f32014-12-05 18:08:21 +000059def dump_vlan(vlan):
Steve McIntyre018d30c2015-02-09 06:34:57 +000060 print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % (
61 int(vlan[0]),
62 vlan[1],
63 int(vlan[2]),
64 ("yes" if vlan[3] is True else "no"),
65 vlan[4])
Steve McIntyree41e3f32014-12-05 18:08:21 +000066
Steve McIntyref1c04f92014-12-16 18:23:15 +000067def call_vland(msgtype, msg):
68 ipc = VlanIpc()
69 ipc.client_connect('localhost', config.vland.port)
70 msg['client_name'] = 'admin.py'
71 msg['type'] = msgtype
Steve McIntyref1c04f92014-12-16 18:23:15 +000072 ipc.client_send(msg)
73 ret = ipc.client_recv_and_close()
74 if 'response' not in ret:
75 raise SocketError("Badly-formed response from VLANd server")
76 if ret['response'] == "ERROR":
Steve McIntyre8a1a1ae2015-01-23 17:52:14 +000077 print "Input error: VLANd server said \"%s\"" % ret['error']
78 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000079 return ret['data']
80
Steve McIntyrec12f6312014-12-15 15:00:12 +000081config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000082
Steve McIntyre844bfd42014-11-27 16:58:31 +000083usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000084parser = optparse.OptionParser(usage=usage, description=banner)
85
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000086# System commands
87system_group = optparse.OptionGroup(parser, "System commands")
88system_group.add_option("--status",
89 dest="status",
90 action = "store_true",
91 default = False,
92 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +000093system_group.add_option("--statistics",
94 dest="statistics",
95 action = "store_true",
96 default = False,
97 help = "Print some system statistics")
98system_group.add_option("--version",
99 dest="version",
100 action = "store_true",
101 default = False,
102 help = "Describe the version of this admin interface")
103system_group.add_option("--vland_version",
104 dest="vland_version",
105 action = "store_true",
106 default = False,
107 help = "Describe the version of the running VLANd")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000108system_group.add_option("--auto_import_switch",
Steve McIntyre6c562602014-12-22 16:10:54 +0000109 dest = "auto_import_switch",
110 action = "store",
111 type = "string",
112 help = "Attempt to import a switch's configuration into the VLANd system",
113 nargs = 1,
114 metavar = "<switch name>")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000115system_group.add_option("--probe_switches",
Steve McIntyredeff7952014-12-23 13:45:59 +0000116 dest = "probe_switches",
117 action = "store_true",
118 default = False,
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000119 help = "Probe all the configured switches")
Steve McIntyre06fe6422015-01-23 17:55:43 +0000120system_group.add_option("--shutdown",
121 dest = "shutdown",
122 action = "store_true",
123 default = False,
124 help = "Shut down a running VLANd")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000125parser.add_option_group(system_group)
126# May add shutdown, self-check etc. later
127
Steve McIntyre844bfd42014-11-27 16:58:31 +0000128# Switch commands
129switch_group = optparse.OptionGroup(parser, "Switch commands")
130switch_group.add_option("--list_all_switches",
131 dest = "list_all_switches",
132 action = "store_true",
133 default = False,
134 help = "List all the existing switches in the system")
135switch_group.add_option("--create_switch",
136 dest = "create_switch",
137 action = "store",
138 type = "string",
139 help = "Add a new switch to the system",
140 nargs = 1,
141 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000142switch_group.add_option("--delete_switch",
143 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000144 action = "store",
145 type = "int",
146 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000147 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000148 nargs = 1,
149 metavar = "<switch_id>")
150switch_group.add_option("--show_switch",
151 dest = "show_switch",
152 action = "store",
153 type = "int",
154 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000155 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000156 nargs = 1,
157 metavar = "<switch_id>")
158switch_group.add_option("--lookup_switch_by_name",
159 dest = "lookup_switch_by_name",
160 action = "store",
161 type = "string",
162 help = "Lookup a switch ID by name",
163 nargs = 1,
164 metavar = "<name>")
165switch_group.add_option("--list_switch_ports",
166 dest = "list_switch_ports",
167 action = "store",
168 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000169 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000170 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000171 nargs = 1,
172 metavar = "<switch_id>")
173parser.add_option_group(switch_group)
174
175# Port commands
176port_group = optparse.OptionGroup(parser, "Port commands")
177port_group.add_option("--list_all_ports",
178 dest = "list_all_ports",
179 action = "store_true",
180 default = False,
181 help = "List all the existing ports in the system")
182port_group.add_option("--create_port",
183 dest = "create_port",
184 action = "store",
185 type = "string",
186 help = "Add a new port to the system",
Steve McIntyreea753972015-08-05 13:52:48 +0100187 nargs = 3,
188 metavar = "<switch_id> <name> <number>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000189port_group.add_option("--delete_port",
190 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000191 action = "store",
192 type = "int",
193 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000194 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000195 nargs = 1,
196 metavar = "<port_id>")
197port_group.add_option("--show_port",
198 dest = "show_port",
199 action = "store",
200 type = "int",
201 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000202 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000203 nargs = 1,
204 metavar = "<port_id>")
205port_group.add_option("--lookup_port_by_switch_and_name",
206 dest = "lookup_port_by_switch_and_name",
207 action = "store",
208 type = "string",
209 help = "Lookup a port ID by switch and port name",
210 nargs = 2,
211 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000212port_group.add_option("--set_port_mode",
213 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000214 action = "store",
215 type = "string",
216 help = "Set the mode of a port to 'trunk' or 'access'",
217 nargs = 2,
218 metavar = "<port_id> <mode>")
219port_group.add_option("--lock_port",
220 dest = "lock_port",
221 action = "store",
222 type = "string",
223 help = "Lock the settings on a port",
224 nargs = 1,
225 metavar = "<port_id>")
226port_group.add_option("--unlock_port",
227 dest = "unlock_port",
228 action = "store",
229 type = "string",
230 help = "Unock the settings on a port",
231 nargs = 1,
232 metavar = "<port_id>")
233port_group.add_option("--set_port_current_vlan",
234 dest = "set_port_current_vlan",
235 action = "store",
236 type = "int",
237 help = "Set the current VLAN assignment for a port",
238 nargs = 2,
239 metavar = "<port_id> <vlan_id>")
240port_group.add_option("--get_port_current_vlan",
241 dest = "get_port_current_vlan",
242 action = "store",
243 type = "int",
244 help = "Get the current VLAN assignment for a port",
245 nargs = 1,
246 metavar = "<port_id>")
247port_group.add_option("--set_port_base_vlan",
248 dest = "set_port_base_vlan",
249 action = "store",
250 type = "int",
251 help = "Set the base VLAN assignment for a port",
252 nargs = 2,
253 metavar = "<port_id> <vlan_id>")
254port_group.add_option("--get_port_base_vlan",
255 dest = "get_port_base_vlan",
256 action = "store",
257 type = "int",
258 help = "Get the base VLAN assignment for a port",
259 nargs = 1,
260 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000261port_group.add_option("--restore_port_to_base_vlan",
262 dest = "restore_port_to_base_vlan",
263 action = "store",
264 type = "int",
265 help = "Reset the port back to its base VLAN",
266 nargs = 1,
267 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000268parser.add_option_group(port_group)
269
270# VLAN commands
271vlan_group = optparse.OptionGroup(parser, "VLAN commands")
272vlan_group.add_option("--list_all_vlans",
273 dest = "list_all_vlans",
274 action = "store_true",
275 default = False,
276 help = "List all the existing vlans in the system")
277vlan_group.add_option("--create_vlan",
278 dest = "create_vlan",
279 action = "store",
280 type = "string",
281 help = "Add a new vlan to the system",
282 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000283 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000284vlan_group.add_option("--delete_vlan",
285 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000286 action = "store",
287 type = "int",
288 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000289 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000290 nargs = 1,
291 metavar = "<vlan_id>")
292vlan_group.add_option("--show_vlan",
293 dest = "show_vlan",
294 action = "store",
295 type = "int",
296 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000297 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000298 nargs = 1,
299 metavar = "<vlan_id>")
Steve McIntyre65533d72015-01-23 18:01:17 +0000300vlan_group.add_option("--lookup_vlan_by_tag",
301 dest = "lookup_vlan_by_tag",
302 action = "store",
303 type = "int",
304 help = "Find the vlan ID of an existing vlan in the system",
305 default = None,
306 nargs = 1,
307 metavar = "<tag>")
308vlan_group.add_option("--show_vlan_tag",
309 dest = "show_vlan_tag",
310 action = "store",
311 type = "int",
312 help = "Print the vlan tag of an existing vlan in the system",
313 default = None,
314 nargs = 1,
315 metavar = "<vlan_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000316parser.add_option_group(vlan_group)
317
318(opts, args) = parser.parse_args()
319
Steve McIntyre844bfd42014-11-27 16:58:31 +0000320if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000321 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000322 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000323 dump_switch(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000324elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000325 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000326 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000327 dump_port(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000328elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000329 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000330 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000331 dump_vlan(line)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000332elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000333 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000334 switch_id = call_vland('db_update',
335 {'command':'db.create_switch',
336 'data':
337 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000338 print 'Created switch_id %d' % switch_id
339 except InputError as inst:
340 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000341elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000342 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000343 port_id = call_vland('db_update',
344 {'command':'db.create_port',
345 'data':
346 {'switch_id': opts.create_port[0],
Steve McIntyreea753972015-08-05 13:52:48 +0100347 'name': opts.create_port[1],
348 'number': opts.create_port[2]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000349 print 'Created port_id %d' % port_id
350 except InputError as inst:
351 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000352elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000353 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100354 (vlan_id, vlan_tag) = call_vland('vlan_update',
355 {'command':'api.create_vlan',
356 'data':
357 {'name': opts.create_vlan[0],
358 'tag': opts.create_vlan[1],
359 'is_base_vlan': is_positive(opts.create_vlan[2])}})
360 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000361 except InputError as inst:
362 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000363elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000364 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000365 switch_id = call_vland('db_update',
366 {'command':'db.delete_switch',
367 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000368 print 'Deleted switch_id %d' % switch_id
369 except InputError as inst:
370 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000371elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000372 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000373 port_id = call_vland('db_update',
374 {'command':'db.delete_port',
375 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000376 except InputError as inst:
377 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000378elif opts.delete_vlan is not None:
379 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000380 vlan_id = call_vland('vlan_update',
381 {'command':'api.delete_vlan',
382 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000383 print 'Deleted vlan_id %d' % vlan_id
384 except InputError as inst:
385 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000386elif opts.lookup_switch_by_name is not None:
387 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000388 switch_id = call_vland('db_query',
389 {'command':'db.get_switch_id_by_name',
390 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000391 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000392 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000393 else:
394 print 'No switch found for name %s' % opts.lookup_switch_by_name
395 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100396 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000397elif opts.show_switch is not None:
398 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000399 this_switch = call_vland('db_query',
400 {'command':'db.get_switch_by_id',
401 'data':
402 {'switch_id': opts.show_switch}})
403 if this_switch is not None:
404 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000405 else:
406 print 'No switch found for switch_id %d' % opts.show_switch
407 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100408 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000409elif opts.list_switch_ports is not None:
410 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000411 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000412 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000413 'data':
414 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000415 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000416 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000417 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000418 else:
419 print 'No ports found for switch_id %d' % opts.list_switch_ports
420 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100421 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000422elif opts.show_port is not None:
423 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000424 this_port = call_vland('db_query',
425 {'command':'db.get_port_by_id',
426 'data':
427 {'port_id': opts.show_port}})
428 if this_port is not None:
429 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000430 else:
431 print 'No port found for port_id %d' % opts.show_port
432 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100433 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000434elif opts.lookup_port_by_switch_and_name is not None:
435 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000436 p = call_vland('db_query',
437 {'command':'db.get_port_by_switch_and_name',
438 'data':
439 {'switch_id': opts.lookup_port_by_switch_and_name[0],
440 'name': opts.lookup_port_by_switch_and_name[1]}})
441 if p is not None:
442 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000443 else:
444 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])
445 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100446 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000447elif opts.set_port_mode is not None:
448 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000449 port_id = call_vland('vlan_update',
450 {'command':'api.set_port_mode',
451 'data':
452 {'port_id': opts.set_port_mode[0],
453 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000454 print "Updated mode for port_id %d" % port_id
455 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100456 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000457elif opts.lock_port is not None:
458 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000459 port_id = call_vland('db_update',
460 {'command':'db.set_port_is_locked',
461 'data':
462 {'port_id': opts.lock_port,
463 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000464 print "Locked port_id %d" % port_id
465 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100466 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000467elif opts.unlock_port is not None:
468 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000469 port_id = call_vland('db_update',
470 {'command':'db.set_port_is_locked',
471 'data':
472 {'port_id': opts.unlock_port,
473 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000474 print "Unlocked port_id %d" % port_id
475 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100476 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000477elif opts.set_port_current_vlan is not None:
478 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000479 port_id = call_vland('vlan_update',
480 {'command':'api.set_current_vlan',
481 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000482 {'port_id': opts.set_port_current_vlan[0],
483 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000484 print "Set current VLAN on port_id %d" % port_id
485 except InputError as inst:
486 print 'Failed: %s' % inst
487elif opts.get_port_current_vlan is not None:
488 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000489 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000490 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000491 'data':
492 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000493 if vlan_id is not None:
494 print vlan_id
495 else:
496 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
497 except InputError as inst:
498 print 'Failed: %s' % inst
499elif opts.set_port_base_vlan is not None:
500 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000501 port_id = call_vland('db_update',
502 {'command':'db.set_base_vlan',
503 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100504 {'port_id': opts.set_port_base_vlan[0],
505 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000506 print "Set base VLAN on port_id %d" % port_id
507 except InputError as inst:
508 print 'Failed: %s' % inst
509elif opts.get_port_base_vlan is not None:
510 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000511 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000512 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000513 'data':
514 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000515 if vlan_id is not None:
516 print vlan_id
517 else:
518 print "No base_vlan_id found for port_id %d" % port_id
519 except InputError as inst:
520 print 'Failed: %s' % inst
521elif opts.restore_port_to_base_vlan is not None:
522 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000523 port_id = call_vland('vlan_update',
524 {'command': 'api.restore_base_vlan',
525 'data':
526 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000527 print "Restored port_id %d back to base VLAN" % port_id
528 except InputError as inst:
529 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000530elif opts.show_vlan is not None:
531 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000532 v = call_vland('db_query',
533 {'command':'db.get_vlan_by_id',
534 'data':
535 {'vlan_id': opts.show_vlan}})
536 if v is not None:
537 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000538 else:
539 print 'No vlan found for vlan_id %d' % opts.show_vlan
540 except InputError as inst:
541 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000542elif opts.lookup_vlan_by_tag is not None:
543 try:
544 vlan_id = call_vland('db_query',
545 {'command':'db.get_vlan_id_by_tag',
546 'data':
547 {'tag': opts.lookup_vlan_by_tag}})
548 if vlan_id is not None:
549 print vlan_id
550 else:
551 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
552 except InputError as inst:
553 print 'Failed: %s' % inst
554elif opts.show_vlan_tag is not None:
555 try:
556 vlan_tag = call_vland('db_query',
557 {'command':'db.get_vlan_tag_by_id',
558 'data':
559 {'vlan_id': opts.show_vlan_tag}})
560 if vlan_tag is not None:
561 print vlan_tag
562 else:
563 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
564 except InputError as inst:
565 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000566elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000567 print 'Config:'
568 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000569 default_vlan_id = call_vland('db_query',
570 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000571 'data':
572 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000573 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 +0000574 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
575 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000576 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000577 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000578 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000579 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000580 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000581 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000582 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000583elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000584 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
585 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000586elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000587 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
588 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000589elif opts.version:
590 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000591elif opts.auto_import_switch:
592 print 'Attempting to import switch %s' % opts.auto_import_switch
593 if opts.auto_import_switch not in config.switches:
594 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100595 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000596 {'command':'api.auto_import_switch',
597 'data':
598 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100599 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 +0000600elif opts.probe_switches:
601 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100602 probe = call_vland('daemon_query',
603 {'command':'daemon.probe_switches',
604 'data': None})
605 for field in probe:
606 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000607elif opts.shutdown:
608 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100609 shutdown = call_vland('daemon_query',
610 {'command':'daemon.shutdown',
611 'data': None})
612 for field in shutdown:
613 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000614else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000615 print 'No recognised command given. Try -h for help'