blob: a123d33251e13e7c99c790262078b047bfce73fc [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 McIntyre9951b1c2015-08-05 13:55:38 +010050 print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d number:%d" % (
Steve McIntyre018d30c2015-02-09 06:34:57 +000051 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]),
Steve McIntyre9951b1c2015-08-05 13:55:38 +010057 int(port[6]),
58 int(port[7]))
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000059
Steve McIntyree41e3f32014-12-05 18:08:21 +000060def dump_vlan(vlan):
Steve McIntyre018d30c2015-02-09 06:34:57 +000061 print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % (
62 int(vlan[0]),
63 vlan[1],
64 int(vlan[2]),
65 ("yes" if vlan[3] is True else "no"),
66 vlan[4])
Steve McIntyree41e3f32014-12-05 18:08:21 +000067
Steve McIntyref1c04f92014-12-16 18:23:15 +000068def call_vland(msgtype, msg):
69 ipc = VlanIpc()
70 ipc.client_connect('localhost', config.vland.port)
71 msg['client_name'] = 'admin.py'
72 msg['type'] = msgtype
Steve McIntyref1c04f92014-12-16 18:23:15 +000073 ipc.client_send(msg)
74 ret = ipc.client_recv_and_close()
75 if 'response' not in ret:
76 raise SocketError("Badly-formed response from VLANd server")
77 if ret['response'] == "ERROR":
Steve McIntyre8a1a1ae2015-01-23 17:52:14 +000078 print "Input error: VLANd server said \"%s\"" % ret['error']
79 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000080 return ret['data']
81
Steve McIntyrec12f6312014-12-15 15:00:12 +000082config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000083
Steve McIntyre844bfd42014-11-27 16:58:31 +000084usage = 'Usage: %prog --command [command options]'
Steve McIntyre844bfd42014-11-27 16:58:31 +000085parser = optparse.OptionParser(usage=usage, description=banner)
86
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000087# System commands
88system_group = optparse.OptionGroup(parser, "System commands")
89system_group.add_option("--status",
90 dest="status",
91 action = "store_true",
92 default = False,
93 help = "Describe current system status")
Steve McIntyrec00d4cf2014-12-16 19:23:39 +000094system_group.add_option("--statistics",
95 dest="statistics",
96 action = "store_true",
97 default = False,
98 help = "Print some system statistics")
99system_group.add_option("--version",
100 dest="version",
101 action = "store_true",
102 default = False,
103 help = "Describe the version of this admin interface")
104system_group.add_option("--vland_version",
105 dest="vland_version",
106 action = "store_true",
107 default = False,
108 help = "Describe the version of the running VLANd")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000109system_group.add_option("--auto_import_switch",
Steve McIntyre6c562602014-12-22 16:10:54 +0000110 dest = "auto_import_switch",
111 action = "store",
112 type = "string",
113 help = "Attempt to import a switch's configuration into the VLANd system",
114 nargs = 1,
115 metavar = "<switch name>")
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000116system_group.add_option("--probe_switches",
Steve McIntyredeff7952014-12-23 13:45:59 +0000117 dest = "probe_switches",
118 action = "store_true",
119 default = False,
Steve McIntyrea4bb9912015-01-13 18:40:37 +0000120 help = "Probe all the configured switches")
Steve McIntyre06fe6422015-01-23 17:55:43 +0000121system_group.add_option("--shutdown",
122 dest = "shutdown",
123 action = "store_true",
124 default = False,
125 help = "Shut down a running VLANd")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000126parser.add_option_group(system_group)
127# May add shutdown, self-check etc. later
128
Steve McIntyre844bfd42014-11-27 16:58:31 +0000129# Switch commands
130switch_group = optparse.OptionGroup(parser, "Switch commands")
131switch_group.add_option("--list_all_switches",
132 dest = "list_all_switches",
133 action = "store_true",
134 default = False,
135 help = "List all the existing switches in the system")
136switch_group.add_option("--create_switch",
137 dest = "create_switch",
138 action = "store",
139 type = "string",
140 help = "Add a new switch to the system",
141 nargs = 1,
142 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000143switch_group.add_option("--delete_switch",
144 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000145 action = "store",
146 type = "int",
147 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000148 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000149 nargs = 1,
150 metavar = "<switch_id>")
151switch_group.add_option("--show_switch",
152 dest = "show_switch",
153 action = "store",
154 type = "int",
155 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000156 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000157 nargs = 1,
158 metavar = "<switch_id>")
159switch_group.add_option("--lookup_switch_by_name",
160 dest = "lookup_switch_by_name",
161 action = "store",
162 type = "string",
163 help = "Lookup a switch ID by name",
164 nargs = 1,
165 metavar = "<name>")
166switch_group.add_option("--list_switch_ports",
167 dest = "list_switch_ports",
168 action = "store",
169 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000170 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000171 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000172 nargs = 1,
173 metavar = "<switch_id>")
174parser.add_option_group(switch_group)
175
176# Port commands
177port_group = optparse.OptionGroup(parser, "Port commands")
178port_group.add_option("--list_all_ports",
179 dest = "list_all_ports",
180 action = "store_true",
181 default = False,
182 help = "List all the existing ports in the system")
183port_group.add_option("--create_port",
184 dest = "create_port",
185 action = "store",
186 type = "string",
187 help = "Add a new port to the system",
Steve McIntyreea753972015-08-05 13:52:48 +0100188 nargs = 3,
189 metavar = "<switch_id> <name> <number>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000190port_group.add_option("--delete_port",
191 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000192 action = "store",
193 type = "int",
194 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000195 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000196 nargs = 1,
197 metavar = "<port_id>")
198port_group.add_option("--show_port",
199 dest = "show_port",
200 action = "store",
201 type = "int",
202 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000203 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000204 nargs = 1,
205 metavar = "<port_id>")
206port_group.add_option("--lookup_port_by_switch_and_name",
207 dest = "lookup_port_by_switch_and_name",
208 action = "store",
209 type = "string",
210 help = "Lookup a port ID by switch and port name",
211 nargs = 2,
212 metavar = "<switch_id> <name>")
Steve McIntyre45f55012015-08-05 13:55:15 +0100213port_group.add_option("--lookup_port_by_switch_and_number",
214 dest = "lookup_port_by_switch_and_number",
215 action = "store",
216 type = "string",
217 help = "Lookup a port ID by switch and number",
218 nargs = 2,
219 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000220port_group.add_option("--set_port_mode",
221 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000222 action = "store",
223 type = "string",
224 help = "Set the mode of a port to 'trunk' or 'access'",
225 nargs = 2,
226 metavar = "<port_id> <mode>")
227port_group.add_option("--lock_port",
228 dest = "lock_port",
229 action = "store",
230 type = "string",
231 help = "Lock the settings on a port",
232 nargs = 1,
233 metavar = "<port_id>")
234port_group.add_option("--unlock_port",
235 dest = "unlock_port",
236 action = "store",
237 type = "string",
238 help = "Unock the settings on a port",
239 nargs = 1,
240 metavar = "<port_id>")
241port_group.add_option("--set_port_current_vlan",
242 dest = "set_port_current_vlan",
243 action = "store",
244 type = "int",
245 help = "Set the current VLAN assignment for a port",
246 nargs = 2,
247 metavar = "<port_id> <vlan_id>")
248port_group.add_option("--get_port_current_vlan",
249 dest = "get_port_current_vlan",
250 action = "store",
251 type = "int",
252 help = "Get the current VLAN assignment for a port",
253 nargs = 1,
254 metavar = "<port_id>")
255port_group.add_option("--set_port_base_vlan",
256 dest = "set_port_base_vlan",
257 action = "store",
258 type = "int",
259 help = "Set the base VLAN assignment for a port",
260 nargs = 2,
261 metavar = "<port_id> <vlan_id>")
262port_group.add_option("--get_port_base_vlan",
263 dest = "get_port_base_vlan",
264 action = "store",
265 type = "int",
266 help = "Get the base VLAN assignment for a port",
267 nargs = 1,
268 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000269port_group.add_option("--restore_port_to_base_vlan",
270 dest = "restore_port_to_base_vlan",
271 action = "store",
272 type = "int",
273 help = "Reset the port back to its base VLAN",
274 nargs = 1,
275 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000276parser.add_option_group(port_group)
277
278# VLAN commands
279vlan_group = optparse.OptionGroup(parser, "VLAN commands")
280vlan_group.add_option("--list_all_vlans",
281 dest = "list_all_vlans",
282 action = "store_true",
283 default = False,
284 help = "List all the existing vlans in the system")
285vlan_group.add_option("--create_vlan",
286 dest = "create_vlan",
287 action = "store",
288 type = "string",
289 help = "Add a new vlan to the system",
290 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000291 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000292vlan_group.add_option("--delete_vlan",
293 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000294 action = "store",
295 type = "int",
296 help = "Remove an existing vlan from 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>")
300vlan_group.add_option("--show_vlan",
301 dest = "show_vlan",
302 action = "store",
303 type = "int",
304 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000305 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000306 nargs = 1,
307 metavar = "<vlan_id>")
Steve McIntyre65533d72015-01-23 18:01:17 +0000308vlan_group.add_option("--lookup_vlan_by_tag",
309 dest = "lookup_vlan_by_tag",
310 action = "store",
311 type = "int",
312 help = "Find the vlan ID of an existing vlan in the system",
313 default = None,
314 nargs = 1,
315 metavar = "<tag>")
316vlan_group.add_option("--show_vlan_tag",
317 dest = "show_vlan_tag",
318 action = "store",
319 type = "int",
320 help = "Print the vlan tag of an existing vlan in the system",
321 default = None,
322 nargs = 1,
323 metavar = "<vlan_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000324parser.add_option_group(vlan_group)
325
326(opts, args) = parser.parse_args()
327
Steve McIntyre844bfd42014-11-27 16:58:31 +0000328if opts.list_all_switches:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000329 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyre844bfd42014-11-27 16:58:31 +0000330 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000331 dump_switch(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000332elif opts.list_all_ports:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000333 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000334 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000335 dump_port(line)
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000336elif opts.list_all_vlans:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000337 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000338 for line in result:
Steve McIntyre018d30c2015-02-09 06:34:57 +0000339 dump_vlan(line)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000340elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000341 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000342 switch_id = call_vland('db_update',
343 {'command':'db.create_switch',
344 'data':
345 {'name':opts.create_switch}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000346 print 'Created switch_id %d' % switch_id
347 except InputError as inst:
348 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000349elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000350 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000351 port_id = call_vland('db_update',
352 {'command':'db.create_port',
353 'data':
354 {'switch_id': opts.create_port[0],
Steve McIntyreea753972015-08-05 13:52:48 +0100355 'name': opts.create_port[1],
356 'number': opts.create_port[2]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000357 print 'Created port_id %d' % port_id
358 except InputError as inst:
359 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000360elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000361 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100362 (vlan_id, vlan_tag) = call_vland('vlan_update',
363 {'command':'api.create_vlan',
364 'data':
365 {'name': opts.create_vlan[0],
366 'tag': opts.create_vlan[1],
367 'is_base_vlan': is_positive(opts.create_vlan[2])}})
368 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000369 except InputError as inst:
370 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000371elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000372 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000373 switch_id = call_vland('db_update',
374 {'command':'db.delete_switch',
375 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000376 print 'Deleted switch_id %d' % switch_id
377 except InputError as inst:
378 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000379elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000380 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000381 port_id = call_vland('db_update',
382 {'command':'db.delete_port',
383 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000384 except InputError as inst:
385 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000386elif opts.delete_vlan is not None:
387 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000388 vlan_id = call_vland('vlan_update',
389 {'command':'api.delete_vlan',
390 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000391 print 'Deleted vlan_id %d' % vlan_id
392 except InputError as inst:
393 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000394elif opts.lookup_switch_by_name is not None:
395 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000396 switch_id = call_vland('db_query',
397 {'command':'db.get_switch_id_by_name',
398 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000399 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000400 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000401 else:
402 print 'No switch found for name %s' % opts.lookup_switch_by_name
403 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100404 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000405elif opts.show_switch is not None:
406 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000407 this_switch = call_vland('db_query',
408 {'command':'db.get_switch_by_id',
409 'data':
410 {'switch_id': opts.show_switch}})
411 if this_switch is not None:
412 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000413 else:
414 print 'No switch found for switch_id %d' % opts.show_switch
415 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100416 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000417elif opts.list_switch_ports is not None:
418 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000419 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000420 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000421 'data':
422 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000423 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000424 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000425 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000426 else:
427 print 'No ports found for switch_id %d' % opts.list_switch_ports
428 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100429 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000430elif opts.show_port is not None:
431 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000432 this_port = call_vland('db_query',
433 {'command':'db.get_port_by_id',
434 'data':
435 {'port_id': opts.show_port}})
436 if this_port is not None:
437 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000438 else:
439 print 'No port found for port_id %d' % opts.show_port
440 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100441 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000442elif opts.lookup_port_by_switch_and_name is not None:
443 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000444 p = call_vland('db_query',
445 {'command':'db.get_port_by_switch_and_name',
446 'data':
447 {'switch_id': opts.lookup_port_by_switch_and_name[0],
448 'name': opts.lookup_port_by_switch_and_name[1]}})
449 if p is not None:
450 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000451 else:
452 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])
453 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100454 print 'Failed: %s' % inst
Steve McIntyre45f55012015-08-05 13:55:15 +0100455elif opts.lookup_port_by_switch_and_number is not None:
456 try:
457 p = call_vland('db_query',
458 {'command':'db.get_port_by_switch_and_number',
459 'data':
460 {'switch_id': opts.lookup_port_by_switch_and_number[0],
461 'number': opts.lookup_port_by_switch_and_number[1]}})
462 if p is not None:
463 print p
464 else:
465 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]))
466 except InputError as inst:
467 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000468elif opts.set_port_mode is not None:
469 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000470 port_id = call_vland('vlan_update',
471 {'command':'api.set_port_mode',
472 'data':
473 {'port_id': opts.set_port_mode[0],
474 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000475 print "Updated mode for port_id %d" % port_id
476 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100477 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000478elif opts.lock_port is not None:
479 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000480 port_id = call_vland('db_update',
481 {'command':'db.set_port_is_locked',
482 'data':
483 {'port_id': opts.lock_port,
484 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000485 print "Locked port_id %d" % port_id
486 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100487 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000488elif opts.unlock_port is not None:
489 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000490 port_id = call_vland('db_update',
491 {'command':'db.set_port_is_locked',
492 'data':
493 {'port_id': opts.unlock_port,
494 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000495 print "Unlocked port_id %d" % port_id
496 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100497 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000498elif opts.set_port_current_vlan is not None:
499 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000500 port_id = call_vland('vlan_update',
501 {'command':'api.set_current_vlan',
502 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000503 {'port_id': opts.set_port_current_vlan[0],
504 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000505 print "Set current VLAN on port_id %d" % port_id
506 except InputError as inst:
507 print 'Failed: %s' % inst
508elif opts.get_port_current_vlan is not None:
509 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000510 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000511 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000512 'data':
513 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000514 if vlan_id is not None:
515 print vlan_id
516 else:
517 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
518 except InputError as inst:
519 print 'Failed: %s' % inst
520elif opts.set_port_base_vlan is not None:
521 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000522 port_id = call_vland('db_update',
523 {'command':'db.set_base_vlan',
524 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100525 {'port_id': opts.set_port_base_vlan[0],
526 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000527 print "Set base VLAN on port_id %d" % port_id
528 except InputError as inst:
529 print 'Failed: %s' % inst
530elif opts.get_port_base_vlan is not None:
531 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000532 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000533 {'command':'db.get_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000534 'data':
535 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000536 if vlan_id is not None:
537 print vlan_id
538 else:
539 print "No base_vlan_id found for port_id %d" % port_id
540 except InputError as inst:
541 print 'Failed: %s' % inst
542elif opts.restore_port_to_base_vlan is not None:
543 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000544 port_id = call_vland('vlan_update',
545 {'command': 'api.restore_base_vlan',
546 'data':
547 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000548 print "Restored port_id %d back to base VLAN" % port_id
549 except InputError as inst:
550 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000551elif opts.show_vlan is not None:
552 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000553 v = call_vland('db_query',
554 {'command':'db.get_vlan_by_id',
555 'data':
556 {'vlan_id': opts.show_vlan}})
557 if v is not None:
558 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000559 else:
560 print 'No vlan found for vlan_id %d' % opts.show_vlan
561 except InputError as inst:
562 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000563elif opts.lookup_vlan_by_tag is not None:
564 try:
565 vlan_id = call_vland('db_query',
566 {'command':'db.get_vlan_id_by_tag',
567 'data':
568 {'tag': opts.lookup_vlan_by_tag}})
569 if vlan_id is not None:
570 print vlan_id
571 else:
572 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
573 except InputError as inst:
574 print 'Failed: %s' % inst
575elif opts.show_vlan_tag is not None:
576 try:
577 vlan_tag = call_vland('db_query',
578 {'command':'db.get_vlan_tag_by_id',
579 'data':
580 {'vlan_id': opts.show_vlan_tag}})
581 if vlan_tag is not None:
582 print vlan_tag
583 else:
584 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
585 except InputError as inst:
586 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000587elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000588 print 'Config:'
589 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000590 default_vlan_id = call_vland('db_query',
591 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000592 'data':
593 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000594 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 +0000595 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
596 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000597 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000598 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000599 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000600 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000601 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000602 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000603 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000604elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000605 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
606 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000607elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000608 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
609 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000610elif opts.version:
611 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000612elif opts.auto_import_switch:
613 print 'Attempting to import switch %s' % opts.auto_import_switch
614 if opts.auto_import_switch not in config.switches:
615 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100616 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000617 {'command':'api.auto_import_switch',
618 'data':
619 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100620 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 +0000621elif opts.probe_switches:
622 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100623 probe = call_vland('daemon_query',
624 {'command':'daemon.probe_switches',
625 'data': None})
626 for field in probe:
627 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000628elif opts.shutdown:
629 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100630 shutdown = call_vland('daemon_query',
631 {'command':'daemon.shutdown',
632 'data': None})
633 for field in shutdown:
634 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000635else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000636 print 'No recognised command given. Try -h for help'