blob: ca13a0dccd931bcf10a3f8195ee7e75bb837502d [file] [log] [blame]
Steve McIntyre844bfd42014-11-27 16:58:31 +00001#! /usr/bin/python
2
Steve McIntyred2313b22016-03-12 11:50:10 +00003# Copyright 2014-2016 Linaro Limited
Steve McIntyre844bfd42014-11-27 16:58:31 +00004#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18# MA 02110-1301, USA.
19#
20# VLANd admin interface
21#
22
Steve McIntyre5694f6d2014-12-18 16:43:30 +000023import os, sys
Steve McIntyre460358a2016-03-12 11:57:53 +000024import argparse
Steve McIntyreea343aa2015-10-23 17:46:17 +010025import datetime, time
Steve McIntyre844bfd42014-11-27 16:58:31 +000026
27vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
28sys.path.insert(0, vlandpath)
29
Steve McIntyreb01959f2016-03-22 17:02:39 +000030from errors import InputError, SocketError, NotFoundError, Error
Steve McIntyre21f02702014-12-16 18:19:47 +000031from config.config import VlanConfig
32from ipc.ipc import VlanIpc
Steve McIntyre844bfd42014-11-27 16:58:31 +000033
Steve McIntyred2313b22016-03-12 11:50:10 +000034prog = "admin"
Steve McIntyrec03d68d2016-03-24 17:38:34 +000035version = "0.6"
Steve McIntyre844bfd42014-11-27 16:58:31 +000036banner = "Linaro VLANd admin interface, version %s" % version
Steve McIntyref5ef6962016-03-22 17:21:47 +000037exitcode = Error.OK
Steve McIntyrec4890132015-08-07 15:19:11 +010038TRUNK_ID_NONE = -1
39
Steve McIntyreae95fd62014-12-05 16:51:41 +000040def is_positive(text):
Steve McIntyrec6895fc2014-12-19 15:01:08 +000041 if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000042 return True
Steve McIntyrec6895fc2014-12-19 15:01:08 +000043 elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'):
Steve McIntyreae95fd62014-12-05 16:51:41 +000044 return False
45 else:
46 raise InputError("Cannot parse \"%s\" as True or False" % text)
47
Steve McIntyrea132c362014-12-05 15:53:21 +000048def dump_switch(switch):
Steve McIntyre018d30c2015-02-09 06:34:57 +000049 print "switch_id:%d name:%s" % (
50 int(switch[0]),
51 switch[1])
Steve McIntyrea132c362014-12-05 15:53:21 +000052
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000053def dump_port(port):
Steve McIntyrec4890132015-08-07 15:19:11 +010054 print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d number:%d trunk_id:%s" % (
Steve McIntyre018d30c2015-02-09 06:34:57 +000055 int(port[0]),
56 port[1],
57 int(port[2]),
58 ("yes" if port[3] is True else "no"),
59 ("trunk" if port[4] is True else "access"),
60 int(port[5]),
Steve McIntyre9951b1c2015-08-05 13:55:38 +010061 int(port[6]),
Steve McIntyrec4890132015-08-07 15:19:11 +010062 int(port[7]),
63 'None' if (TRUNK_ID_NONE == port[8]) else port[8])
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000064
Steve McIntyree41e3f32014-12-05 18:08:21 +000065def dump_vlan(vlan):
Steve McIntyre018d30c2015-02-09 06:34:57 +000066 print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % (
67 int(vlan[0]),
68 vlan[1],
69 int(vlan[2]),
70 ("yes" if vlan[3] is True else "no"),
71 vlan[4])
Steve McIntyree41e3f32014-12-05 18:08:21 +000072
Steve McIntyrec4890132015-08-07 15:19:11 +010073def dump_trunk(trunk):
74 print "trunk_id:%d creation_time:%s" % (
75 int(trunk[0]),
76 trunk[1])
77
Steve McIntyref1c04f92014-12-16 18:23:15 +000078def call_vland(msgtype, msg):
79 ipc = VlanIpc()
80 ipc.client_connect('localhost', config.vland.port)
81 msg['client_name'] = 'admin.py'
82 msg['type'] = msgtype
Steve McIntyref1c04f92014-12-16 18:23:15 +000083 ipc.client_send(msg)
84 ret = ipc.client_recv_and_close()
85 if 'response' not in ret:
86 raise SocketError("Badly-formed response from VLANd server")
87 if ret['response'] == "ERROR":
Steve McIntyreb01959f2016-03-22 17:02:39 +000088 raise InputError("VLANd server said \"%s\"" % ret['error'])
89 if ret['response'] == "NOTFOUND":
90 raise NotFoundError("VLANd server said \"%s\"" % ret['error'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000091 return ret['data']
92
Steve McIntyrec12f6312014-12-15 15:00:12 +000093config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000094
Steve McIntyre460358a2016-03-12 11:57:53 +000095parser = argparse.ArgumentParser()
Steve McIntyre844bfd42014-11-27 16:58:31 +000096
Steve McIntyre460358a2016-03-12 11:57:53 +000097####################
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000098# System commands
Steve McIntyre460358a2016-03-12 11:57:53 +000099####################
100sp = parser.add_subparsers(title='Sub-commands',
101 help="Commands",
102 dest='subparser_name')
103p_status = sp.add_parser("status",
104 help="Describe current system status")
105p_status.set_defaults(which = "status")
106p_statistics = sp.add_parser("statistics",
107 help="Print some system statistics")
108p_statistics.set_defaults(which = "statistics")
109p_version = sp.add_parser("version",
110 help="Describe the version of this admin interface")
111p_version.set_defaults(which = "version")
112p_vland_version = sp.add_parser("vland_version",
113 help="Describe the version of the running VLANd")
114p_vland_version.set_defaults(which = "vland_version")
115p_probe_switches = sp.add_parser("probe_switches",
116 help="Probe all the configured switches")
117p_probe_switches.set_defaults(which = "probe_switches")
118p_auto_import = sp.add_parser("auto_import_switch",
119 help="Attempt to import a switch's configuration into the VLANd system")
120p_auto_import.add_argument('--name',
121 required = True,
122 help="Import the switch named SWITCH_NAME in vland.cfg")
123p_auto_import.set_defaults(which = "auto_import_switch")
124p_shutdown = sp.add_parser("shutdown",
125 help="Shut down a running VLANd")
126p_shutdown.set_defaults(which = "shutdown")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000127
Steve McIntyre460358a2016-03-12 11:57:53 +0000128####################
Steve McIntyre844bfd42014-11-27 16:58:31 +0000129# Switch commands
Steve McIntyre460358a2016-03-12 11:57:53 +0000130####################
131p_list_all_switches = sp.add_parser("list_all_switches",
132 help="List all the existing switches in the system")
133p_list_all_switches.set_defaults(which = "list_all_switches")
134p_create_switch = sp.add_parser("create_switch",
135 help = "Add a new switch to the system")
136p_create_switch.set_defaults(which = "create_switch")
137p_create_switch.add_argument('--name',
138 required = True,
139 help="The name of the new switch, as described in vland.cfg")
140p_delete_switch = sp.add_parser("delete_switch",
141 help = "Remove an existing switch from the system")
142p_delete_switch.set_defaults(which = "delete_switch")
143p_delete_switch.add_argument('--switch_id',
144 required = True,
145 help = "The ID of the switch to remove")
146p_show_switch = sp.add_parser("show_switch",
147 help = "Show the details of an existing switch in the system")
148p_show_switch.set_defaults(which = "show_switch")
149p_show_switch.add_argument('--switch_id',
150 required = True,
151 help = "The ID of the switch to show")
152p_lookup_switch_by_name = sp.add_parser("lookup_switch_by_name",
153 help = "Lookup a switch ID by name")
154p_lookup_switch_by_name.set_defaults(which = "lookup_switch_by_name")
155p_lookup_switch_by_name.add_argument('--name',
156 required = True,
157 help = "The switch name to search for")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000158
Steve McIntyre460358a2016-03-12 11:57:53 +0000159####################
160# Ports commands
161####################
162p_list_all_ports = sp.add_parser("list_all_ports",
163 help="List all the existing ports in the system")
164p_list_all_ports.set_defaults(which = "list_all_ports")
165p_create_port = sp.add_parser("create_port",
166 help = "Add a new port to the system")
167p_create_port.set_defaults(which = "create_port")
168p_create_port.add_argument('--switch_id',
169 required = True,
170 help="The ID of the switch containing the new port")
171p_create_port.add_argument('--name',
172 required = True,
173 help="The name of the new port")
174p_create_port.add_argument('--number',
175 required = True,
176 help="The human-friendly number for the new port")
177p_delete_port = sp.add_parser("delete_port",
178 help = "Remove an existing port from the system")
179p_delete_port.set_defaults(which = "delete_port")
180p_delete_port.add_argument('--port_id',
181 required = True,
182 help = "The ID of the port to remove")
183p_show_port = sp.add_parser("show_port",
184 help = "Show the details of an existing port in the system")
185p_show_port.set_defaults(which = "show_port")
186p_show_port.add_argument('--port_id',
187 required = True,
188 help = "The ID of the port to show")
189p_lookup_port_by_switch_and_name = sp.add_parser("lookup_port_by_switch_and_name",
190 help = "Lookup a port ID by switch ID and port name")
191p_lookup_port_by_switch_and_name.set_defaults(which = "lookup_port_by_switch_and_name")
192p_lookup_port_by_switch_and_name.add_argument('--switch_id',
193 required = True,
194 help = "The switch ID to search for")
195p_lookup_port_by_switch_and_name.add_argument('--name',
196 required = True,
197 help = "The port name to search for")
198p_lookup_port_by_switch_and_number = sp.add_parser("lookup_port_by_switch_and_number",
199 help = "Lookup a port ID by switch ID and port number")
200p_lookup_port_by_switch_and_number.set_defaults(which = "lookup_port_by_switch_and_number")
201p_lookup_port_by_switch_and_number.add_argument('--switch_id',
202 required = True,
203 help = "The switch ID to search for")
204p_lookup_port_by_switch_and_number.add_argument('--number',
205 required = True,
206 help = "The port number to search for")
207p_lookup_ports_by_switch = sp.add_parser("lookup_ports_by_switch",
208 help = "Lookup port ID(s) by switch ID")
209p_lookup_ports_by_switch.set_defaults(which = "lookup_ports_by_switch")
210p_lookup_ports_by_switch.add_argument('--switch_id',
211 required = True,
212 help = "The switch ID to search for")
213p_lookup_ports_by_current_vlan = sp.add_parser("lookup_ports_by_current_vlan",
214 help = "Lookup port ID(s) by current VLAN ID")
215p_lookup_ports_by_current_vlan.set_defaults(which = "lookup_ports_by_current_vlan")
216p_lookup_ports_by_current_vlan.add_argument('--vlan_id',
217 required = True,
218 help = "The VLAN ID to search for")
219p_lookup_ports_by_base_vlan = sp.add_parser("lookup_ports_by_base_vlan",
220 help = "Lookup port ID(s) by base vlan ID")
221p_lookup_ports_by_base_vlan.set_defaults(which = "lookup_ports_by_base_vlan")
222p_lookup_ports_by_base_vlan.add_argument('--vlan_id',
223 required = True,
224 help = "The VLAN ID to search for")
225p_lookup_ports_by_trunk = sp.add_parser("lookup_ports_by_trunk",
226 help = "Lookup port ID(s) by trunk ID")
227p_lookup_ports_by_trunk.set_defaults(which = "lookup_ports_by_trunk")
228p_lookup_ports_by_trunk.add_argument('--trunk_id',
229 required = True,
230 help = "The trunk ID to search for")
231p_set_port_mode = sp.add_parser("set_port_mode",
232 help = "Set the mode of a port to 'trunk' or 'access'")
233p_set_port_mode.set_defaults(which = "set_port_mode")
234p_set_port_mode.add_argument('--port_id',
235 required = True,
236 help = "The ID of the port to modify")
237p_set_port_mode.add_argument('--mode',
238 required = True,
239 help = "The mode to select ('trunk' or 'access')")
240p_get_port_mode = sp.add_parser("get_port_mode",
241 help = "Get the mode of a port ('trunk' or 'access')")
242p_get_port_mode.set_defaults(which = "get_port_mode")
243p_get_port_mode.add_argument('--port_id',
244 required = True,
245 help = "The ID of the port to query")
246p_lock_port = sp.add_parser("lock_port",
247 help = "Lock the settings on a port")
248p_lock_port.set_defaults(which = "lock_port")
249p_lock_port.add_argument('--port_id',
250 required = True,
251 help = "The ID of the port to lock")
252p_unlock_port = sp.add_parser("unlock_port",
253 help = "Unlock the settings on a port")
254p_unlock_port.set_defaults(which = "unlock_port")
255p_unlock_port.add_argument('--port_id',
256 required = True,
257 help = "The ID of the port to unlock")
258p_set_port_current_vlan = sp.add_parser("set_port_current_vlan",
259 help = "Set the current VLAN assignment for a port")
260p_set_port_current_vlan.set_defaults(which = "set_port_current_vlan")
261p_set_port_current_vlan.add_argument('--port_id',
262 required = True,
263 help = "The ID of the port to modify")
264p_set_port_current_vlan.add_argument('--vlan_id',
265 required = True,
266 help = "The VLAN ID to be used")
267p_get_port_current_vlan = sp.add_parser("get_port_current_vlan",
268 help = "Get the current VLAN assignment for a port")
269p_get_port_current_vlan.set_defaults(which = "get_port_current_vlan")
270p_get_port_current_vlan.add_argument('--port_id',
271 required = True,
272 help = "The ID of the port to query")
273p_set_port_base_vlan = sp.add_parser("set_port_base_vlan",
274 help = "Set the base VLAN assignment for a port")
275p_set_port_base_vlan.set_defaults(which = "set_port_base_vlan")
276p_set_port_base_vlan.add_argument('--port_id',
277 required = True,
278 help = "The ID of the port to modify")
279p_set_port_base_vlan.add_argument('--vlan_id',
280 required = True,
281 help = "The VLAN ID to be used")
282p_get_port_base_vlan = sp.add_parser("get_port_base_vlan",
283 help = "Get the base VLAN assignment for a port")
284p_get_port_base_vlan.set_defaults(which = "get_port_base_vlan")
285p_get_port_base_vlan.add_argument('--port_id',
286 required = True,
287 help = "The ID of the port to query")
288p_restore_port_to_base_vlan = sp.add_parser("restore_port_to_base_vlan",
289 help = "Reset a port back to its base VLAN")
290p_restore_port_to_base_vlan.set_defaults(which = "restore_port_to_base_vlan")
291p_restore_port_to_base_vlan.add_argument('--port_id',
292 required = True,
293 help = "The ID of the port to modify")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000294
Steve McIntyre460358a2016-03-12 11:57:53 +0000295####################
Steve McIntyre844bfd42014-11-27 16:58:31 +0000296# VLAN commands
Steve McIntyre460358a2016-03-12 11:57:53 +0000297####################
298p_list_all_vlans = sp.add_parser("list_all_vlans",
299 help="List all the existing VLANs in the system")
300p_list_all_vlans.set_defaults(which = "list_all_vlans")
301p_create_vlan = sp.add_parser("create_vlan",
302 help = "Add a new VLAN to the system")
303p_create_vlan.set_defaults(which = "create_vlan")
304p_create_vlan.add_argument('--name',
305 required = True,
306 help="The name of the new VLAN")
307p_create_vlan.add_argument('--tag',
308 required = True,
309 help="The tag for the new VLAN (-1 to automatically select)")
310p_create_vlan.add_argument('--is_base_vlan',
311 required = True,
312 help="Is the new VLAN a base VLAN (true/false)")
313p_delete_vlan = sp.add_parser("delete_vlan",
314 help = "Remove an existing VLAN from the system")
315p_delete_vlan.set_defaults(which = "delete_vlan")
316p_delete_vlan.add_argument('--vlan_id',
317 required = True,
318 help = "The ID of the VLAN to remove")
319p_show_vlan = sp.add_parser("show_vlan",
320 help = "Show the details of an existing VLAN in the system")
321p_show_vlan.set_defaults(which = "show_vlan")
322p_show_vlan.add_argument('--vlan_id',
323 required = True,
324 help = "The ID of the VLAN to show")
325p_lookup_vlan_by_tag = sp.add_parser("lookup_vlan_by_tag",
326 help = "Find the VLAN ID of an existing VLAN in the system")
327p_lookup_vlan_by_tag.set_defaults(which = "lookup_vlan_by_tag")
328p_lookup_vlan_by_tag.add_argument('--tag',
329 required = True,
330 help = "The VLAN tag to search for")
331p_show_vlan_tag = sp.add_parser("show_vlan_tag",
332 help = "Print the VLAN tag of an existing VLAN in the system")
333p_show_vlan_tag.set_defaults(which = "show_vlan_tag")
334p_show_vlan_tag.add_argument('--vlan_id',
335 required = True,
336 help = "The VLAN ID to search for")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000337
Steve McIntyre460358a2016-03-12 11:57:53 +0000338####################
339# Trunk commands
340####################
341p_list_all_trunks = sp.add_parser("list_all_trunks",
342 help="List all the existing trunks in the system")
343p_list_all_trunks.set_defaults(which = "list_all_trunks")
344p_create_trunk = sp.add_parser("create_trunk",
345 help = "Add a new trunk to the system, linking two ports")
346p_create_trunk.set_defaults(which = "create_trunk")
347p_create_trunk.add_argument('--port_id1',
348 required = True,
349 help="The ID of the first port to be used")
350p_create_trunk.add_argument('--port_id2',
351 required = True,
352 help="The ID of the second port to be used")
353p_delete_trunk = sp.add_parser("delete_trunk",
354 help = "Remove an existing trunk from the system")
355p_delete_trunk.set_defaults(which = "delete_trunk")
356p_delete_trunk.add_argument('--trunk_id',
357 required = True,
358 help = "The ID of the trunk to remove")
359p_show_trunk = sp.add_parser("show_trunk",
360 help = "Show the details of an existing trunk in the system")
361p_show_trunk.set_defaults(which = "show_trunk")
362p_show_trunk.add_argument('--trunk_id',
363 required = True,
364 help = "The ID of the trunk to show")
Steve McIntyrec4890132015-08-07 15:19:11 +0100365
Steve McIntyre460358a2016-03-12 11:57:53 +0000366args = parser.parse_args()
Steve McIntyrec4890132015-08-07 15:19:11 +0100367
Steve McIntyre460358a2016-03-12 11:57:53 +0000368# Now work out what to do
369if args.which == 'status':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000370 try:
371 print 'Config:'
372 print ' knows about %d switch(es)' % len(config.switches)
373 default_vlan_id = call_vland('db_query',
374 {'command':'db.get_vlan_id_by_tag',
375 'data':
376 {'tag': config.vland.default_vlan_tag}})
377 print 'The default vlan tag (%d) is vlan ID %d' % (config.vland.default_vlan_tag, default_vlan_id)
378 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
379 print 'VLANd is running %s' % stat['running']
380 lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f')
381 print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z')
382 print 'DB via VLANd:'
383 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
384 print ' knows about %d switch(es)' % len(switches)
385 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
386 print ' knows about %d port(s)' % len(ports)
387 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
388 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000389 except (InputError, NotFoundError):
390 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000391elif args.which == 'shutdown':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000392 try:
393 print 'Asking VLANd to shutdown'
394 shutdown = call_vland('daemon_query',
395 {'command':'daemon.shutdown',
396 'data': None})
397 for field in shutdown:
398 print '%s: %s' % (field, shutdown[field])
Steve McIntyref5ef6962016-03-22 17:21:47 +0000399 except (InputError, NotFoundError):
400 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000401elif args.which == 'vland_version':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000402 try:
403 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
404 print 'VLANd version %s' % ver['version']
Steve McIntyref5ef6962016-03-22 17:21:47 +0000405 except (InputError, NotFoundError):
406 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000407elif args.which == 'statistics':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000408 try:
409 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
410 print 'VLANd uptime: %d seconds' % stats['uptime']
Steve McIntyref5ef6962016-03-22 17:21:47 +0000411 except (InputError, NotFoundError):
412 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000413elif args.which == 'version':
414 print 'VLANd admin interface version %s' % version
415elif args.which == 'auto_import_switch':
416 print 'Attempting to import switch %s' % args.name
417 if args.name not in config.switches:
418 raise InputError("Can't find switch %s in config" % args.name)
Steve McIntyreb01959f2016-03-22 17:02:39 +0000419 try:
420 imp = call_vland('vlan_update',
421 {'command':'api.auto_import_switch',
422 'data':
423 {'switch': args.name}})
424 print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (args.name, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added'])
Steve McIntyref5ef6962016-03-22 17:21:47 +0000425 except (InputError, NotFoundError):
Steve McIntyreb01959f2016-03-22 17:02:39 +0000426 print 'Import failed - see log for details'
Steve McIntyref5ef6962016-03-22 17:21:47 +0000427 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000428elif args.which == 'probe_switches':
429 print 'Asking VLANd to probe all the configured switches'
Steve McIntyreb01959f2016-03-22 17:02:39 +0000430 try:
431 probe = call_vland('daemon_query',
432 {'command':'daemon.probe_switches',
433 'data': None})
434 for field in probe:
435 print '%s: %s' % (field, probe[field])
Steve McIntyref5ef6962016-03-22 17:21:47 +0000436 except (InputError, NotFoundError):
Steve McIntyreb01959f2016-03-22 17:02:39 +0000437 print 'Probe failed - see log for details'
Steve McIntyref5ef6962016-03-22 17:21:47 +0000438 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000439elif args.which == 'list_all_switches':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000440 try:
441 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
442 for line in result:
443 dump_switch(line)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000444 except (InputError, NotFoundError):
445 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000446elif args.which == 'list_all_ports':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000447 try:
448 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
449 for line in result:
450 dump_port(line)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000451 except (InputError, NotFoundError):
452 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000453elif args.which == 'list_all_vlans':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000454 try:
455 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
456 for line in result:
457 dump_vlan(line)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000458 except (InputError, NotFoundError):
459 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000460elif args.which == 'list_all_trunks':
Steve McIntyreb01959f2016-03-22 17:02:39 +0000461 try:
462 result = call_vland('db_query', {'command':'db.all_trunks', 'data':None})
463 for line in result:
464 dump_trunk(line)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000465 except (InputError, NotFoundError):
466 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000467elif args.which == 'create_switch':
468 try:
469 switch_id = call_vland('db_update',
470 {'command':'db.create_switch',
471 'data':
472 {'name':args.name}})
473 print 'Created switch_id %d' % switch_id
474 except InputError as inst:
475 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000476 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000477elif args.which == 'create_port':
478 try:
479 port_id = call_vland('db_update',
480 {'command':'db.create_port',
481 'data':
482 {'switch_id': args.switch_id,
483 'name': args.name,
484 'number': args.number}})
485 print 'Created port_id %d' % port_id
486 except InputError as inst:
487 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000488 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000489 except NotFoundError as inst:
490 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000491 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000492elif args.which == 'create_vlan':
493 try:
494 (vlan_id, vlan_tag) = call_vland('vlan_update',
495 {'command':'api.create_vlan',
496 'data':
497 {'name': args.name,
498 'tag': args.tag,
499 'is_base_vlan': is_positive(args.is_base_vlan)}})
500 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
501 except InputError as inst:
502 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000503 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000504 except NotFoundError as inst:
505 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000506 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000507elif args.which == 'create_trunk':
508 try:
509 trunk_id = call_vland('db_update',
510 {'command':'db.create_trunk',
511 'data':
512 {'port_id1': args.port_id1,
513 'port_id2': args.port_id2}})
514 print 'Created trunk_id %d' % trunk_id
515 except InputError as inst:
516 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000517 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000518 except NotFoundError as inst:
519 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000520 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000521elif args.which == 'delete_switch':
522 try:
523 switch_id = call_vland('db_update',
524 {'command':'db.delete_switch',
525 'data': {'switch_id': args.switch_id}})
526 print 'Deleted switch_id %s' % switch_id
527 except InputError as inst:
528 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000529 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000530 except NotFoundError as inst:
531 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000532 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000533elif args.which == 'delete_port':
534 try:
535 port_id = call_vland('db_update',
536 {'command':'db.delete_port',
537 'data': {'port_id': args.port_id}})
538 print 'Deleted port_id %s' % port_id
539 except InputError as inst:
540 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000541 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000542 except NotFoundError as inst:
543 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000544 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000545elif args.which == 'delete_vlan':
546 try:
547 vlan_id = call_vland('vlan_update',
548 {'command':'api.delete_vlan',
549 'data': {'vlan_id': args.vlan_id}})
550 print 'Deleted vlan_id %d' % vlan_id
551 except InputError as inst:
552 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000553 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000554 except NotFoundError as inst:
555 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000556 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000557elif args.which == 'delete_trunk':
558 try:
Steve McIntyrea5bf2202018-01-31 14:15:21 +0000559 trunk_id = call_vland('db_update',
Steve McIntyre460358a2016-03-12 11:57:53 +0000560 {'command':'db.delete_trunk',
561 'data': {'trunk_id': args.trunk_id}})
562 print 'Deleted trunk_id %s' % trunk_id
563 except InputError as inst:
564 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000565 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000566 except NotFoundError as inst:
567 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000568 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000569elif args.which == 'lookup_switch_by_name':
570 try:
571 switch_id = call_vland('db_query',
572 {'command':'db.get_switch_id_by_name',
573 'data':{'name':args.name}})
574 if switch_id is not None:
575 print '%d' % switch_id
576 else:
577 print 'No switch found for name %s' % args.name
Steve McIntyref5ef6962016-03-22 17:21:47 +0000578 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000579 except InputError as inst:
580 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000581 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000582elif args.which == 'show_switch':
583 try:
584 this_switch = call_vland('db_query',
585 {'command':'db.get_switch_by_id',
586 'data':
587 {'switch_id': args.switch_id}})
588 if this_switch is not None:
589 dump_switch(this_switch)
590 else:
591 print 'No switch found for switch_id %s' % args.switch_id
592 except InputError as inst:
593 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000594 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000595elif args.which == 'show_port':
596 try:
597 this_port = call_vland('db_query',
598 {'command':'db.get_port_by_id',
599 'data':
600 {'port_id': args.port_id}})
601 if this_port is not None:
602 dump_port(this_port)
603 else:
604 print 'No port found for port_id %s' % args.port_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000605 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000606 except InputError as inst:
607 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000608 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000609elif args.which == 'lookup_port_by_switch_and_name':
610 try:
611 p = call_vland('db_query',
612 {'command':'db.get_port_by_switch_and_name',
613 'data':
614 {'switch_id': args.switch_id,
615 'name': args.name}})
616 if p is not None:
617 print p
618 else:
619 print 'No port found for switch_id %s, name %s' % (args.switch_id, args.name)
Steve McIntyref5ef6962016-03-22 17:21:47 +0000620 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000621 except InputError as inst:
622 print 'Failed: %s' % inst
623elif args.which == 'lookup_port_by_switch_and_number':
624 try:
625 p = call_vland('db_query',
626 {'command':'db.get_port_by_switch_and_number',
627 'data':
628 {'switch_id': args.switch_id,
629 'number': args.number}})
630 if p is not None:
631 print p
632 else:
633 print 'No port found for switch_id %s, port number %s' % (args.switch_id, args.number)
634 except InputError as inst:
635 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000636 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000637elif args.which == 'lookup_ports_by_switch':
638 try:
639 p = call_vland('db_query',
640 {'command':'db.get_ports_by_switch',
641 'data':
642 {'switch_id': args.switch_id}})
643 if p is not None:
644 for port_id in p:
645 print port_id
646 else:
647 print 'No ports found for switch_id %s' % args.switch_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000648 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000649 except InputError as inst:
650 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000651 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000652elif args.which == 'lookup_ports_by_current_vlan':
653 try:
654 p = call_vland('db_query',
655 {'command':'db.get_ports_by_current_vlan',
656 'data':
657 {'vlan_id': args.vlan_id}})
658 if p is not None:
659 for port_id in p:
660 print port_id
661 else:
662 print 'No ports found for current vlan_id %s' % args.vlan_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000663 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000664 except InputError as inst:
665 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000666 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000667elif args.which == 'lookup_ports_by_base_vlan':
668 try:
669 p = call_vland('db_query',
670 {'command':'db.get_ports_by_base_vlan',
671 'data':
672 {'vlan_id': args.vlan_id}})
673 if p is not None:
674 for port_id in p:
675 print port_id
676 else:
677 print 'No ports found for base vlan_id %s' % args.vlan_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000678 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000679 except InputError as inst:
680 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000681 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000682elif args.which == 'lookup_ports_by_trunk':
683 try:
684 p = call_vland('db_query',
685 {'command':'db.get_ports_by_trunk',
686 'data':
687 {'trunk_id': args.trunk_id}})
688 if p is not None:
689 for port_id in p:
690 print port_id
691 else:
692 print 'No ports found for trunk_id %s' % args.trunk_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000693 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000694 except InputError as inst:
695 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000696 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000697elif args.which == 'set_port_mode':
698 try:
699 port_id = call_vland('vlan_update',
700 {'command':'api.set_port_mode',
701 'data':
702 {'port_id': args.port_id,
703 'mode': args.mode}})
704 print "Updated mode for port_id %d" % port_id
705 except InputError as inst:
706 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000707 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000708 except NotFoundError as inst:
709 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000710 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000711elif args.which == 'lock_port':
712 try:
713 port_id = call_vland('db_update',
714 {'command':'db.set_port_is_locked',
715 'data':
716 {'port_id': args.port_id,
717 'is_locked': True}})
718 print "Locked port_id %d" % port_id
719 except InputError as inst:
720 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000721 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000722 except NotFoundError as inst:
723 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000724 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000725elif args.which == 'unlock_port':
726 try:
727 port_id = call_vland('db_update',
728 {'command':'db.set_port_is_locked',
729 'data':
730 {'port_id': args.port_id,
731 'is_locked': False}})
732 print "Unlocked port_id %d" % port_id
733 except InputError as inst:
734 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000735 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000736 except NotFoundError as inst:
737 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000738 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000739elif args.which == 'set_port_current_vlan':
740 try:
741 port_id = call_vland('vlan_update',
742 {'command':'api.set_current_vlan',
743 'data':
744 {'port_id': args.port_id,
745 'vlan_id': args.vlan_id}})
746 print "Set current VLAN on port_id %d" % port_id
747 except InputError as inst:
748 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000749 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000750 except NotFoundError as inst:
751 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000752 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000753elif args.which == 'get_port_current_vlan':
754 try:
755 vlan_id = call_vland('db_query',
756 {'command':'db.get_current_vlan_id_by_port',
757 'data':
758 {'port_id': args.port_id}})
759 if vlan_id is not None:
760 print vlan_id
761 else:
762 print "No current_vlan_id found for port_id %s" % args.port_id
763 except InputError as inst:
764 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000765 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000766 except NotFoundError as inst:
767 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000768 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000769elif args.which == 'set_port_base_vlan':
770 try:
771 port_id = call_vland('db_update',
772 {'command':'db.set_base_vlan',
773 'data':
774 {'port_id': args.port_id,
775 'base_vlan_id': args.vlan_id}})
776 print "Set base VLAN on port_id %d" % port_id
777 except InputError as inst:
778 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000779 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000780 except NotFoundError as inst:
781 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000782 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000783elif args.which == 'get_port_base_vlan':
784 try:
785 vlan_id = call_vland('db_query',
786 {'command':'db.get_base_vlan_id_by_port',
787 'data':
788 {'port_id': args.port_id}})
789 if vlan_id is not None:
790 print vlan_id
791 else:
792 print "No base_vlan_id found for port_id %d" % port_id
793 except InputError as inst:
794 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000795 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000796 except NotFoundError as inst:
797 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000798 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000799elif args.which == 'restore_port_to_base_vlan':
800 try:
801 port_id = call_vland('vlan_update',
802 {'command': 'api.restore_base_vlan',
803 'data':
804 {'port_id': args.port_id}})
805 print "Restored port_id %d back to base VLAN" % port_id
806 except InputError as inst:
807 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000808 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000809 except NotFoundError as inst:
810 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000811 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000812elif args.which == 'show_vlan':
813 try:
814 v = call_vland('db_query',
815 {'command':'db.get_vlan_by_id',
816 'data':
817 {'vlan_id': args.vlan_id}})
818 if v is not None:
819 dump_vlan(v)
820 else:
821 print 'No VLAN found for vlan_id %s' % args.vlan_id
822 except InputError as inst:
823 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000824 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000825 except NotFoundError as inst:
826 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000827 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000828elif args.which == 'lookup_vlan_by_tag':
829 try:
830 vlan_id = call_vland('db_query',
831 {'command':'db.get_vlan_id_by_tag',
832 'data':
833 {'tag': args.tag}})
834 if vlan_id is not None:
835 print vlan_id
836 else:
837 print 'No VLAN found for vlan tag %s' % args.tag
Steve McIntyref5ef6962016-03-22 17:21:47 +0000838 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000839 except InputError as inst:
840 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000841 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000842elif args.which == 'show_vlan_tag':
843 try:
844 vlan_tag = call_vland('db_query',
845 {'command':'db.get_vlan_tag_by_id',
846 'data':
847 {'vlan_id': args.vlan_id}})
848 if vlan_tag is not None:
849 print vlan_tag
850 else:
851 print 'No VLAN found for vlan id %s' % args.vlan_id
Steve McIntyref5ef6962016-03-22 17:21:47 +0000852 exitcode = Error.NOTFOUND
Steve McIntyre460358a2016-03-12 11:57:53 +0000853 except InputError as inst:
854 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000855 exitcode = Error.FAILED
Steve McIntyre460358a2016-03-12 11:57:53 +0000856elif args.which == 'show_trunk':
857 try:
858 this_trunk = call_vland('db_query',
859 {'command':'db.get_trunk_by_id',
860 'data':
861 {'trunk_id': args.trunk_id}})
862 if this_trunk is not None:
863 dump_trunk(this_trunk)
864 else:
865 print 'No port found for port_id %s' % args.trunk_id
866 except InputError as inst:
867 print 'Failed: %s' % inst
Steve McIntyreb01959f2016-03-22 17:02:39 +0000868 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000869 exitcode = Error.FAILED
Steve McIntyreb01959f2016-03-22 17:02:39 +0000870 except NotFoundError as inst:
871 print 'Failed: %s' % inst
Steve McIntyref5ef6962016-03-22 17:21:47 +0000872 exitcode = Error.NOTFOUND
Steve McIntyrea0534a52014-12-05 17:58:40 +0000873else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000874 print 'No recognised command given. Try -h for help'
Steve McIntyreb01959f2016-03-22 17:02:39 +0000875
Steve McIntyref5ef6962016-03-22 17:21:47 +0000876sys.exit(exitcode)