blob: 5879764b32f6ecab21f5ca08a02329eea18afdd1 [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 McIntyre45f55012015-08-05 13:55:15 +0100212port_group.add_option("--lookup_port_by_switch_and_number",
213 dest = "lookup_port_by_switch_and_number",
214 action = "store",
215 type = "string",
216 help = "Lookup a port ID by switch and number",
217 nargs = 2,
218 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000219port_group.add_option("--set_port_mode",
220 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000221 action = "store",
222 type = "string",
223 help = "Set the mode of a port to 'trunk' or 'access'",
224 nargs = 2,
225 metavar = "<port_id> <mode>")
226port_group.add_option("--lock_port",
227 dest = "lock_port",
228 action = "store",
229 type = "string",
230 help = "Lock the settings on a port",
231 nargs = 1,
232 metavar = "<port_id>")
233port_group.add_option("--unlock_port",
234 dest = "unlock_port",
235 action = "store",
236 type = "string",
237 help = "Unock the settings on a port",
238 nargs = 1,
239 metavar = "<port_id>")
240port_group.add_option("--set_port_current_vlan",
241 dest = "set_port_current_vlan",
242 action = "store",
243 type = "int",
244 help = "Set the current VLAN assignment for a port",
245 nargs = 2,
246 metavar = "<port_id> <vlan_id>")
247port_group.add_option("--get_port_current_vlan",
248 dest = "get_port_current_vlan",
249 action = "store",
250 type = "int",
251 help = "Get the current VLAN assignment for a port",
252 nargs = 1,
253 metavar = "<port_id>")
254port_group.add_option("--set_port_base_vlan",
255 dest = "set_port_base_vlan",
256 action = "store",
257 type = "int",
258 help = "Set the base VLAN assignment for a port",
259 nargs = 2,
260 metavar = "<port_id> <vlan_id>")
261port_group.add_option("--get_port_base_vlan",
262 dest = "get_port_base_vlan",
263 action = "store",
264 type = "int",
265 help = "Get the base VLAN assignment for a port",
266 nargs = 1,
267 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000268port_group.add_option("--restore_port_to_base_vlan",
269 dest = "restore_port_to_base_vlan",
270 action = "store",
271 type = "int",
272 help = "Reset the port back to its base VLAN",
273 nargs = 1,
274 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000275parser.add_option_group(port_group)
276
277# VLAN commands
278vlan_group = optparse.OptionGroup(parser, "VLAN commands")
279vlan_group.add_option("--list_all_vlans",
280 dest = "list_all_vlans",
281 action = "store_true",
282 default = False,
283 help = "List all the existing vlans in the system")
284vlan_group.add_option("--create_vlan",
285 dest = "create_vlan",
286 action = "store",
287 type = "string",
288 help = "Add a new vlan to the system",
289 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000290 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000291vlan_group.add_option("--delete_vlan",
292 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000293 action = "store",
294 type = "int",
295 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000296 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000297 nargs = 1,
298 metavar = "<vlan_id>")
299vlan_group.add_option("--show_vlan",
300 dest = "show_vlan",
301 action = "store",
302 type = "int",
303 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000304 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000305 nargs = 1,
306 metavar = "<vlan_id>")
Steve McIntyre65533d72015-01-23 18:01:17 +0000307vlan_group.add_option("--lookup_vlan_by_tag",
308 dest = "lookup_vlan_by_tag",
309 action = "store",
310 type = "int",
311 help = "Find the vlan ID of an existing vlan in the system",
312 default = None,
313 nargs = 1,
314 metavar = "<tag>")
315vlan_group.add_option("--show_vlan_tag",
316 dest = "show_vlan_tag",
317 action = "store",
318 type = "int",
319 help = "Print the vlan tag of an existing vlan in the system",
320 default = None,
321 nargs = 1,
322 metavar = "<vlan_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000323parser.add_option_group(vlan_group)
324
325(opts, args) = parser.parse_args()
326
Steve McIntyre844bfd42014-11-27 16:58:31 +0000327if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000328 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000329 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000330 dump_switch(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000331elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000332 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000333 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000334 dump_port(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000335elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000336 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000337 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000338 dump_vlan(line)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000339elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000340 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000341 switch_id = call_vland('db_update',
342 {'command':'db.create_switch',
343 'data':
344 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000345 print 'Created switch_id %d' % switch_id
346 except InputError as inst:
347 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000348elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000349 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000350 port_id = call_vland('db_update',
351 {'command':'db.create_port',
352 'data':
353 {'switch_id': opts.create_port[0],
Steve McIntyreea753972015-08-05 13:52:48 +0100354 'name': opts.create_port[1],
355 'number': opts.create_port[2]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000356 print 'Created port_id %d' % port_id
357 except InputError as inst:
358 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000359elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000360 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100361 (vlan_id, vlan_tag) = call_vland('vlan_update',
362 {'command':'api.create_vlan',
363 'data':
364 {'name': opts.create_vlan[0],
365 'tag': opts.create_vlan[1],
366 'is_base_vlan': is_positive(opts.create_vlan[2])}})
367 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000368 except InputError as inst:
369 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000370elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000371 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000372 switch_id = call_vland('db_update',
373 {'command':'db.delete_switch',
374 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000375 print 'Deleted switch_id %d' % switch_id
376 except InputError as inst:
377 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000378elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000379 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000380 port_id = call_vland('db_update',
381 {'command':'db.delete_port',
382 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000383 except InputError as inst:
384 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000385elif opts.delete_vlan is not None:
386 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000387 vlan_id = call_vland('vlan_update',
388 {'command':'api.delete_vlan',
389 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000390 print 'Deleted vlan_id %d' % vlan_id
391 except InputError as inst:
392 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000393elif opts.lookup_switch_by_name is not None:
394 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000395 switch_id = call_vland('db_query',
396 {'command':'db.get_switch_id_by_name',
397 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000398 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000399 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000400 else:
401 print 'No switch found for name %s' % opts.lookup_switch_by_name
402 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100403 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000404elif opts.show_switch is not None:
405 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000406 this_switch = call_vland('db_query',
407 {'command':'db.get_switch_by_id',
408 'data':
409 {'switch_id': opts.show_switch}})
410 if this_switch is not None:
411 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000412 else:
413 print 'No switch found for switch_id %d' % opts.show_switch
414 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100415 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000416elif opts.list_switch_ports is not None:
417 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000418 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000419 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000420 'data':
421 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000422 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000423 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000424 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000425 else:
426 print 'No ports found for switch_id %d' % opts.list_switch_ports
427 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100428 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000429elif opts.show_port is not None:
430 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000431 this_port = call_vland('db_query',
432 {'command':'db.get_port_by_id',
433 'data':
434 {'port_id': opts.show_port}})
435 if this_port is not None:
436 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000437 else:
438 print 'No port found for port_id %d' % opts.show_port
439 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100440 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000441elif opts.lookup_port_by_switch_and_name is not None:
442 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000443 p = call_vland('db_query',
444 {'command':'db.get_port_by_switch_and_name',
445 'data':
446 {'switch_id': opts.lookup_port_by_switch_and_name[0],
447 'name': opts.lookup_port_by_switch_and_name[1]}})
448 if p is not None:
449 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000450 else:
451 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])
452 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100453 print 'Failed: %s' % inst
Steve McIntyre45f55012015-08-05 13:55:15 +0100454elif opts.lookup_port_by_switch_and_number is not None:
455 try:
456 p = call_vland('db_query',
457 {'command':'db.get_port_by_switch_and_number',
458 'data':
459 {'switch_id': opts.lookup_port_by_switch_and_number[0],
460 'number': opts.lookup_port_by_switch_and_number[1]}})
461 if p is not None:
462 print p
463 else:
464 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]))
465 except InputError as inst:
466 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000467elif opts.set_port_mode is not None:
468 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000469 port_id = call_vland('vlan_update',
470 {'command':'api.set_port_mode',
471 'data':
472 {'port_id': opts.set_port_mode[0],
473 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000474 print "Updated mode for port_id %d" % port_id
475 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100476 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000477elif opts.lock_port is not None:
478 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000479 port_id = call_vland('db_update',
480 {'command':'db.set_port_is_locked',
481 'data':
482 {'port_id': opts.lock_port,
483 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000484 print "Locked port_id %d" % port_id
485 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100486 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000487elif opts.unlock_port is not None:
488 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000489 port_id = call_vland('db_update',
490 {'command':'db.set_port_is_locked',
491 'data':
492 {'port_id': opts.unlock_port,
493 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000494 print "Unlocked port_id %d" % port_id
495 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100496 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000497elif opts.set_port_current_vlan is not None:
498 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000499 port_id = call_vland('vlan_update',
500 {'command':'api.set_current_vlan',
501 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000502 {'port_id': opts.set_port_current_vlan[0],
503 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000504 print "Set current VLAN on port_id %d" % port_id
505 except InputError as inst:
506 print 'Failed: %s' % inst
507elif opts.get_port_current_vlan is not None:
508 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000509 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000510 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000511 'data':
512 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000513 if vlan_id is not None:
514 print vlan_id
515 else:
516 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
517 except InputError as inst:
518 print 'Failed: %s' % inst
519elif opts.set_port_base_vlan is not None:
520 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000521 port_id = call_vland('db_update',
522 {'command':'db.set_base_vlan',
523 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100524 {'port_id': opts.set_port_base_vlan[0],
525 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000526 print "Set base VLAN on port_id %d" % port_id
527 except InputError as inst:
528 print 'Failed: %s' % inst
529elif opts.get_port_base_vlan is not None:
530 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000531 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000532 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000533 'data':
534 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000535 if vlan_id is not None:
536 print vlan_id
537 else:
538 print "No base_vlan_id found for port_id %d" % port_id
539 except InputError as inst:
540 print 'Failed: %s' % inst
541elif opts.restore_port_to_base_vlan is not None:
542 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000543 port_id = call_vland('vlan_update',
544 {'command': 'api.restore_base_vlan',
545 'data':
546 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000547 print "Restored port_id %d back to base VLAN" % port_id
548 except InputError as inst:
549 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000550elif opts.show_vlan is not None:
551 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000552 v = call_vland('db_query',
553 {'command':'db.get_vlan_by_id',
554 'data':
555 {'vlan_id': opts.show_vlan}})
556 if v is not None:
557 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000558 else:
559 print 'No vlan found for vlan_id %d' % opts.show_vlan
560 except InputError as inst:
561 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000562elif opts.lookup_vlan_by_tag is not None:
563 try:
564 vlan_id = call_vland('db_query',
565 {'command':'db.get_vlan_id_by_tag',
566 'data':
567 {'tag': opts.lookup_vlan_by_tag}})
568 if vlan_id is not None:
569 print vlan_id
570 else:
571 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
572 except InputError as inst:
573 print 'Failed: %s' % inst
574elif opts.show_vlan_tag is not None:
575 try:
576 vlan_tag = call_vland('db_query',
577 {'command':'db.get_vlan_tag_by_id',
578 'data':
579 {'vlan_id': opts.show_vlan_tag}})
580 if vlan_tag is not None:
581 print vlan_tag
582 else:
583 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
584 except InputError as inst:
585 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000586elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000587 print 'Config:'
588 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000589 default_vlan_id = call_vland('db_query',
590 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000591 'data':
592 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000593 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 +0000594 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
595 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000596 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000597 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000598 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000599 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000600 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000601 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000602 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000603elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000604 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
605 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000606elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000607 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
608 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000609elif opts.version:
610 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000611elif opts.auto_import_switch:
612 print 'Attempting to import switch %s' % opts.auto_import_switch
613 if opts.auto_import_switch not in config.switches:
614 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100615 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000616 {'command':'api.auto_import_switch',
617 'data':
618 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100619 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 +0000620elif opts.probe_switches:
621 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100622 probe = call_vland('daemon_query',
623 {'command':'daemon.probe_switches',
624 'data': None})
625 for field in probe:
626 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000627elif opts.shutdown:
628 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100629 shutdown = call_vland('daemon_query',
630 {'command':'daemon.shutdown',
631 'data': None})
632 for field in shutdown:
633 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000634else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000635 print 'No recognised command given. Try -h for help'