blob: cbed93dcfcf3e7f366cc5f77cf348d78af7e6018 [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",
187 nargs = 2,
188 metavar = "<switch_id> <name>")
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],
347 'name': opts.create_port[1]}})
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000348 print 'Created port_id %d' % port_id
349 except InputError as inst:
350 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000351elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000352 try:
Steve McIntyre42122b72015-08-03 19:27:35 +0100353 (vlan_id, vlan_tag) = call_vland('vlan_update',
354 {'command':'api.create_vlan',
355 'data':
356 {'name': opts.create_vlan[0],
357 'tag': opts.create_vlan[1],
358 'is_base_vlan': is_positive(opts.create_vlan[2])}})
359 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000360 except InputError as inst:
361 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000362elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000363 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000364 switch_id = call_vland('db_update',
365 {'command':'db.delete_switch',
366 'data': {'switch_id': opts.delete_switch}})
Steve McIntyre64e38862014-12-02 17:19:37 +0000367 print 'Deleted switch_id %d' % switch_id
368 except InputError as inst:
369 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000370elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000371 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000372 port_id = call_vland('db_update',
373 {'command':'db.delete_port',
374 'data': {'port_id': opts.delete_port}})
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000375 except InputError as inst:
376 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000377elif opts.delete_vlan is not None:
378 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000379 vlan_id = call_vland('vlan_update',
380 {'command':'api.delete_vlan',
381 'data': {'vlan_id': opts.delete_vlan}})
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000382 print 'Deleted vlan_id %d' % vlan_id
383 except InputError as inst:
384 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000385elif opts.lookup_switch_by_name is not None:
386 try:
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000387 switch_id = call_vland('db_query',
388 {'command':'db.get_switch_id_by_name',
389 'data':{'name':opts.lookup_switch_by_name}})
Steve McIntyre8e839a62014-12-05 15:46:05 +0000390 if switch_id is not None:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000391 print '%d' % switch_id
Steve McIntyre8e839a62014-12-05 15:46:05 +0000392 else:
393 print 'No switch found for name %s' % opts.lookup_switch_by_name
394 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100395 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000396elif opts.show_switch is not None:
397 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000398 this_switch = call_vland('db_query',
399 {'command':'db.get_switch_by_id',
400 'data':
401 {'switch_id': opts.show_switch}})
402 if this_switch is not None:
403 dump_switch(this_switch)
Steve McIntyrea132c362014-12-05 15:53:21 +0000404 else:
405 print 'No switch found for switch_id %d' % opts.show_switch
406 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100407 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000408elif opts.list_switch_ports is not None:
409 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000410 ports = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000411 {'command':'db.get_ports_by_switch',
Steve McIntyrebfed0fb2014-12-18 16:48:28 +0000412 'data':
413 {'switch_id': opts.list_switch_ports}})
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000414 if ports is not None:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000415 for p in ports:
Steve McIntyreea15fa72015-03-26 15:41:34 +0000416 print p
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000417 else:
418 print 'No ports found for switch_id %d' % opts.list_switch_ports
419 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100420 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000421elif opts.show_port is not None:
422 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000423 this_port = call_vland('db_query',
424 {'command':'db.get_port_by_id',
425 'data':
426 {'port_id': opts.show_port}})
427 if this_port is not None:
428 dump_port(this_port)
Steve McIntyre08dd8392014-12-05 16:07:20 +0000429 else:
430 print 'No port found for port_id %d' % opts.show_port
431 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100432 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000433elif opts.lookup_port_by_switch_and_name is not None:
434 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000435 p = call_vland('db_query',
436 {'command':'db.get_port_by_switch_and_name',
437 'data':
438 {'switch_id': opts.lookup_port_by_switch_and_name[0],
439 'name': opts.lookup_port_by_switch_and_name[1]}})
440 if p is not None:
441 print p
Steve McIntyre73106572014-12-05 16:13:36 +0000442 else:
443 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])
444 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100445 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000446elif opts.set_port_mode is not None:
447 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000448 port_id = call_vland('vlan_update',
449 {'command':'api.set_port_mode',
450 'data':
451 {'port_id': opts.set_port_mode[0],
452 'mode': opts.set_port_mode[1]}})
Steve McIntyre71b41022014-12-05 17:17:44 +0000453 print "Updated mode for port_id %d" % port_id
454 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100455 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000456elif opts.lock_port is not None:
457 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000458 port_id = call_vland('db_update',
459 {'command':'db.set_port_is_locked',
460 'data':
461 {'port_id': opts.lock_port,
462 'is_locked': True}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000463 print "Locked port_id %d" % port_id
464 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100465 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000466elif opts.unlock_port is not None:
467 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000468 port_id = call_vland('db_update',
469 {'command':'db.set_port_is_locked',
470 'data':
471 {'port_id': opts.unlock_port,
472 'is_locked': False}})
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000473 print "Unlocked port_id %d" % port_id
474 except InputError as inst:
Steve McIntyreb826fc72015-07-27 17:57:40 +0100475 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000476elif opts.set_port_current_vlan is not None:
477 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000478 port_id = call_vland('vlan_update',
479 {'command':'api.set_current_vlan',
480 'data':
Steve McIntyree7062092015-02-13 03:02:14 +0000481 {'port_id': opts.set_port_current_vlan[0],
482 'vlan_id': opts.set_port_current_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000483 print "Set current VLAN on port_id %d" % port_id
484 except InputError as inst:
485 print 'Failed: %s' % inst
486elif opts.get_port_current_vlan is not None:
487 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000488 vlan_id = call_vland('db_query',
Steve McIntyref1c04f92014-12-16 18:23:15 +0000489 {'command':'db.get_current_vlan_id_by_port',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000490 'data':
491 {'port_id': opts.get_port_current_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000492 if vlan_id is not None:
493 print vlan_id
494 else:
495 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
496 except InputError as inst:
497 print 'Failed: %s' % inst
498elif opts.set_port_base_vlan is not None:
499 try:
Steve McIntyre7103de22014-12-17 13:16:48 +0000500 port_id = call_vland('db_update',
501 {'command':'db.set_base_vlan',
502 'data':
Steve McIntyre6ebac382015-07-28 18:14:20 +0100503 {'port_id': opts.set_port_base_vlan[0],
504 'base_vlan_id': opts.set_port_base_vlan[1]}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000505 print "Set base VLAN on port_id %d" % port_id
506 except InputError as inst:
507 print 'Failed: %s' % inst
508elif opts.get_port_base_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_base_vlan_id_by_port',
Steve McIntyrea02ba202014-12-17 16:27:23 +0000512 'data':
513 {'port_id': opts.get_port_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000514 if vlan_id is not None:
515 print vlan_id
516 else:
517 print "No base_vlan_id found for port_id %d" % port_id
518 except InputError as inst:
519 print 'Failed: %s' % inst
520elif opts.restore_port_to_base_vlan is not None:
521 try:
Steve McIntyre3f0aceb2014-12-17 16:27:13 +0000522 port_id = call_vland('vlan_update',
523 {'command': 'api.restore_base_vlan',
524 'data':
525 {'port_id': opts.restore_port_to_base_vlan}})
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000526 print "Restored port_id %d back to base VLAN" % port_id
527 except InputError as inst:
528 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000529elif opts.show_vlan is not None:
530 try:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000531 v = call_vland('db_query',
532 {'command':'db.get_vlan_by_id',
533 'data':
534 {'vlan_id': opts.show_vlan}})
535 if v is not None:
536 dump_vlan(v)
Steve McIntyree41e3f32014-12-05 18:08:21 +0000537 else:
538 print 'No vlan found for vlan_id %d' % opts.show_vlan
539 except InputError as inst:
540 print 'Failed: %s' % inst
Steve McIntyre65533d72015-01-23 18:01:17 +0000541elif opts.lookup_vlan_by_tag is not None:
542 try:
543 vlan_id = call_vland('db_query',
544 {'command':'db.get_vlan_id_by_tag',
545 'data':
546 {'tag': opts.lookup_vlan_by_tag}})
547 if vlan_id is not None:
548 print vlan_id
549 else:
550 print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
551 except InputError as inst:
552 print 'Failed: %s' % inst
553elif opts.show_vlan_tag is not None:
554 try:
555 vlan_tag = call_vland('db_query',
556 {'command':'db.get_vlan_tag_by_id',
557 'data':
558 {'vlan_id': opts.show_vlan_tag}})
559 if vlan_tag is not None:
560 print vlan_tag
561 else:
562 print 'No vlan found for vlan id %d' % opts.show_vlan_tag
563 except InputError as inst:
564 print 'Failed: %s' % inst
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000565elif opts.status:
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000566 print 'Config:'
567 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000568 default_vlan_id = call_vland('db_query',
569 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000570 'data':
571 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre0abacac2014-12-15 16:51:38 +0000572 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 +0000573 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
574 print 'VLANd is running %s' % stat['running']
Steve McIntyref1c04f92014-12-16 18:23:15 +0000575 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000576 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000577 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000578 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000579 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000580 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000581 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000582elif opts.vland_version:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000583 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
584 print 'VLANd version %s' % ver['version']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000585elif opts.statistics:
Steve McIntyre6d84ec12014-12-18 16:56:56 +0000586 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
587 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000588elif opts.version:
589 print 'VLANd admin interface version %s' % version
Steve McIntyre6c562602014-12-22 16:10:54 +0000590elif opts.auto_import_switch:
591 print 'Attempting to import switch %s' % opts.auto_import_switch
592 if opts.auto_import_switch not in config.switches:
593 raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
Steve McIntyree26a1472015-04-01 18:03:31 +0100594 imp = call_vland('vlan_update',
Steve McIntyre6c562602014-12-22 16:10:54 +0000595 {'command':'api.auto_import_switch',
596 'data':
597 {'switch': opts.auto_import_switch}})
Steve McIntyre24a4d572015-07-09 18:25:17 +0100598 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 +0000599elif opts.probe_switches:
600 print 'Asking VLANd to probe all the configured switches'
Steve McIntyree26a1472015-04-01 18:03:31 +0100601 probe = call_vland('daemon_query',
602 {'command':'daemon.probe_switches',
603 'data': None})
604 for field in probe:
605 print '%s: %s' % (field, probe[field])
Steve McIntyre06fe6422015-01-23 17:55:43 +0000606elif opts.shutdown:
607 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100608 shutdown = call_vland('daemon_query',
609 {'command':'daemon.shutdown',
610 'data': None})
611 for field in shutdown:
612 print '%s: %s' % (field, shutdown[field])
Steve McIntyrea0534a52014-12-05 17:58:40 +0000613else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000614 print 'No recognised command given. Try -h for help'