blob: 1e833d1a7a82dd98f0a9799ee7fe7b7a8cd954ce [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 McIntyre5694f6d2014-12-18 16:43:30 +000030from errors import InputError, SocketError
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"
35version = "0.6-DEV"
Steve McIntyre844bfd42014-11-27 16:58:31 +000036banner = "Linaro VLANd admin interface, version %s" % version
37
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 McIntyre8a1a1ae2015-01-23 17:52:14 +000088 print "Input error: VLANd server said \"%s\"" % ret['error']
89 sys.exit(1)
Steve McIntyref1c04f92014-12-16 18:23:15 +000090 return ret['data']
91
Steve McIntyrec12f6312014-12-15 15:00:12 +000092config = VlanConfig(filenames=('./vland.cfg',))
Steve McIntyrec12f6312014-12-15 15:00:12 +000093
Steve McIntyre460358a2016-03-12 11:57:53 +000094parser = argparse.ArgumentParser()
Steve McIntyre844bfd42014-11-27 16:58:31 +000095
Steve McIntyre460358a2016-03-12 11:57:53 +000096####################
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +000097# System commands
Steve McIntyre460358a2016-03-12 11:57:53 +000098####################
99sp = parser.add_subparsers(title='Sub-commands',
100 help="Commands",
101 dest='subparser_name')
102p_status = sp.add_parser("status",
103 help="Describe current system status")
104p_status.set_defaults(which = "status")
105p_statistics = sp.add_parser("statistics",
106 help="Print some system statistics")
107p_statistics.set_defaults(which = "statistics")
108p_version = sp.add_parser("version",
109 help="Describe the version of this admin interface")
110p_version.set_defaults(which = "version")
111p_vland_version = sp.add_parser("vland_version",
112 help="Describe the version of the running VLANd")
113p_vland_version.set_defaults(which = "vland_version")
114p_probe_switches = sp.add_parser("probe_switches",
115 help="Probe all the configured switches")
116p_probe_switches.set_defaults(which = "probe_switches")
117p_auto_import = sp.add_parser("auto_import_switch",
118 help="Attempt to import a switch's configuration into the VLANd system")
119p_auto_import.add_argument('--name',
120 required = True,
121 help="Import the switch named SWITCH_NAME in vland.cfg")
122p_auto_import.set_defaults(which = "auto_import_switch")
123p_shutdown = sp.add_parser("shutdown",
124 help="Shut down a running VLANd")
125p_shutdown.set_defaults(which = "shutdown")
Steve McIntyre9cd6c3d2014-12-05 18:10:35 +0000126
Steve McIntyre460358a2016-03-12 11:57:53 +0000127####################
Steve McIntyre844bfd42014-11-27 16:58:31 +0000128# Switch commands
Steve McIntyre460358a2016-03-12 11:57:53 +0000129####################
130p_list_all_switches = sp.add_parser("list_all_switches",
131 help="List all the existing switches in the system")
132p_list_all_switches.set_defaults(which = "list_all_switches")
133p_create_switch = sp.add_parser("create_switch",
134 help = "Add a new switch to the system")
135p_create_switch.set_defaults(which = "create_switch")
136p_create_switch.add_argument('--name',
137 required = True,
138 help="The name of the new switch, as described in vland.cfg")
139p_delete_switch = sp.add_parser("delete_switch",
140 help = "Remove an existing switch from the system")
141p_delete_switch.set_defaults(which = "delete_switch")
142p_delete_switch.add_argument('--switch_id',
143 required = True,
144 help = "The ID of the switch to remove")
145p_show_switch = sp.add_parser("show_switch",
146 help = "Show the details of an existing switch in the system")
147p_show_switch.set_defaults(which = "show_switch")
148p_show_switch.add_argument('--switch_id',
149 required = True,
150 help = "The ID of the switch to show")
151p_lookup_switch_by_name = sp.add_parser("lookup_switch_by_name",
152 help = "Lookup a switch ID by name")
153p_lookup_switch_by_name.set_defaults(which = "lookup_switch_by_name")
154p_lookup_switch_by_name.add_argument('--name',
155 required = True,
156 help = "The switch name to search for")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000157
Steve McIntyre460358a2016-03-12 11:57:53 +0000158####################
159# Ports commands
160####################
161p_list_all_ports = sp.add_parser("list_all_ports",
162 help="List all the existing ports in the system")
163p_list_all_ports.set_defaults(which = "list_all_ports")
164p_create_port = sp.add_parser("create_port",
165 help = "Add a new port to the system")
166p_create_port.set_defaults(which = "create_port")
167p_create_port.add_argument('--switch_id',
168 required = True,
169 help="The ID of the switch containing the new port")
170p_create_port.add_argument('--name',
171 required = True,
172 help="The name of the new port")
173p_create_port.add_argument('--number',
174 required = True,
175 help="The human-friendly number for the new port")
176p_delete_port = sp.add_parser("delete_port",
177 help = "Remove an existing port from the system")
178p_delete_port.set_defaults(which = "delete_port")
179p_delete_port.add_argument('--port_id',
180 required = True,
181 help = "The ID of the port to remove")
182p_show_port = sp.add_parser("show_port",
183 help = "Show the details of an existing port in the system")
184p_show_port.set_defaults(which = "show_port")
185p_show_port.add_argument('--port_id',
186 required = True,
187 help = "The ID of the port to show")
188p_lookup_port_by_switch_and_name = sp.add_parser("lookup_port_by_switch_and_name",
189 help = "Lookup a port ID by switch ID and port name")
190p_lookup_port_by_switch_and_name.set_defaults(which = "lookup_port_by_switch_and_name")
191p_lookup_port_by_switch_and_name.add_argument('--switch_id',
192 required = True,
193 help = "The switch ID to search for")
194p_lookup_port_by_switch_and_name.add_argument('--name',
195 required = True,
196 help = "The port name to search for")
197p_lookup_port_by_switch_and_number = sp.add_parser("lookup_port_by_switch_and_number",
198 help = "Lookup a port ID by switch ID and port number")
199p_lookup_port_by_switch_and_number.set_defaults(which = "lookup_port_by_switch_and_number")
200p_lookup_port_by_switch_and_number.add_argument('--switch_id',
201 required = True,
202 help = "The switch ID to search for")
203p_lookup_port_by_switch_and_number.add_argument('--number',
204 required = True,
205 help = "The port number to search for")
206p_lookup_ports_by_switch = sp.add_parser("lookup_ports_by_switch",
207 help = "Lookup port ID(s) by switch ID")
208p_lookup_ports_by_switch.set_defaults(which = "lookup_ports_by_switch")
209p_lookup_ports_by_switch.add_argument('--switch_id',
210 required = True,
211 help = "The switch ID to search for")
212p_lookup_ports_by_current_vlan = sp.add_parser("lookup_ports_by_current_vlan",
213 help = "Lookup port ID(s) by current VLAN ID")
214p_lookup_ports_by_current_vlan.set_defaults(which = "lookup_ports_by_current_vlan")
215p_lookup_ports_by_current_vlan.add_argument('--vlan_id',
216 required = True,
217 help = "The VLAN ID to search for")
218p_lookup_ports_by_base_vlan = sp.add_parser("lookup_ports_by_base_vlan",
219 help = "Lookup port ID(s) by base vlan ID")
220p_lookup_ports_by_base_vlan.set_defaults(which = "lookup_ports_by_base_vlan")
221p_lookup_ports_by_base_vlan.add_argument('--vlan_id',
222 required = True,
223 help = "The VLAN ID to search for")
224p_lookup_ports_by_trunk = sp.add_parser("lookup_ports_by_trunk",
225 help = "Lookup port ID(s) by trunk ID")
226p_lookup_ports_by_trunk.set_defaults(which = "lookup_ports_by_trunk")
227p_lookup_ports_by_trunk.add_argument('--trunk_id',
228 required = True,
229 help = "The trunk ID to search for")
230p_set_port_mode = sp.add_parser("set_port_mode",
231 help = "Set the mode of a port to 'trunk' or 'access'")
232p_set_port_mode.set_defaults(which = "set_port_mode")
233p_set_port_mode.add_argument('--port_id',
234 required = True,
235 help = "The ID of the port to modify")
236p_set_port_mode.add_argument('--mode',
237 required = True,
238 help = "The mode to select ('trunk' or 'access')")
239p_get_port_mode = sp.add_parser("get_port_mode",
240 help = "Get the mode of a port ('trunk' or 'access')")
241p_get_port_mode.set_defaults(which = "get_port_mode")
242p_get_port_mode.add_argument('--port_id',
243 required = True,
244 help = "The ID of the port to query")
245p_lock_port = sp.add_parser("lock_port",
246 help = "Lock the settings on a port")
247p_lock_port.set_defaults(which = "lock_port")
248p_lock_port.add_argument('--port_id',
249 required = True,
250 help = "The ID of the port to lock")
251p_unlock_port = sp.add_parser("unlock_port",
252 help = "Unlock the settings on a port")
253p_unlock_port.set_defaults(which = "unlock_port")
254p_unlock_port.add_argument('--port_id',
255 required = True,
256 help = "The ID of the port to unlock")
257p_set_port_current_vlan = sp.add_parser("set_port_current_vlan",
258 help = "Set the current VLAN assignment for a port")
259p_set_port_current_vlan.set_defaults(which = "set_port_current_vlan")
260p_set_port_current_vlan.add_argument('--port_id',
261 required = True,
262 help = "The ID of the port to modify")
263p_set_port_current_vlan.add_argument('--vlan_id',
264 required = True,
265 help = "The VLAN ID to be used")
266p_get_port_current_vlan = sp.add_parser("get_port_current_vlan",
267 help = "Get the current VLAN assignment for a port")
268p_get_port_current_vlan.set_defaults(which = "get_port_current_vlan")
269p_get_port_current_vlan.add_argument('--port_id',
270 required = True,
271 help = "The ID of the port to query")
272p_set_port_base_vlan = sp.add_parser("set_port_base_vlan",
273 help = "Set the base VLAN assignment for a port")
274p_set_port_base_vlan.set_defaults(which = "set_port_base_vlan")
275p_set_port_base_vlan.add_argument('--port_id',
276 required = True,
277 help = "The ID of the port to modify")
278p_set_port_base_vlan.add_argument('--vlan_id',
279 required = True,
280 help = "The VLAN ID to be used")
281p_get_port_base_vlan = sp.add_parser("get_port_base_vlan",
282 help = "Get the base VLAN assignment for a port")
283p_get_port_base_vlan.set_defaults(which = "get_port_base_vlan")
284p_get_port_base_vlan.add_argument('--port_id',
285 required = True,
286 help = "The ID of the port to query")
287p_restore_port_to_base_vlan = sp.add_parser("restore_port_to_base_vlan",
288 help = "Reset a port back to its base VLAN")
289p_restore_port_to_base_vlan.set_defaults(which = "restore_port_to_base_vlan")
290p_restore_port_to_base_vlan.add_argument('--port_id',
291 required = True,
292 help = "The ID of the port to modify")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000293
Steve McIntyre460358a2016-03-12 11:57:53 +0000294####################
Steve McIntyre844bfd42014-11-27 16:58:31 +0000295# VLAN commands
Steve McIntyre460358a2016-03-12 11:57:53 +0000296####################
297p_list_all_vlans = sp.add_parser("list_all_vlans",
298 help="List all the existing VLANs in the system")
299p_list_all_vlans.set_defaults(which = "list_all_vlans")
300p_create_vlan = sp.add_parser("create_vlan",
301 help = "Add a new VLAN to the system")
302p_create_vlan.set_defaults(which = "create_vlan")
303p_create_vlan.add_argument('--name',
304 required = True,
305 help="The name of the new VLAN")
306p_create_vlan.add_argument('--tag',
307 required = True,
308 help="The tag for the new VLAN (-1 to automatically select)")
309p_create_vlan.add_argument('--is_base_vlan',
310 required = True,
311 help="Is the new VLAN a base VLAN (true/false)")
312p_delete_vlan = sp.add_parser("delete_vlan",
313 help = "Remove an existing VLAN from the system")
314p_delete_vlan.set_defaults(which = "delete_vlan")
315p_delete_vlan.add_argument('--vlan_id',
316 required = True,
317 help = "The ID of the VLAN to remove")
318p_show_vlan = sp.add_parser("show_vlan",
319 help = "Show the details of an existing VLAN in the system")
320p_show_vlan.set_defaults(which = "show_vlan")
321p_show_vlan.add_argument('--vlan_id',
322 required = True,
323 help = "The ID of the VLAN to show")
324p_lookup_vlan_by_tag = sp.add_parser("lookup_vlan_by_tag",
325 help = "Find the VLAN ID of an existing VLAN in the system")
326p_lookup_vlan_by_tag.set_defaults(which = "lookup_vlan_by_tag")
327p_lookup_vlan_by_tag.add_argument('--tag',
328 required = True,
329 help = "The VLAN tag to search for")
330p_show_vlan_tag = sp.add_parser("show_vlan_tag",
331 help = "Print the VLAN tag of an existing VLAN in the system")
332p_show_vlan_tag.set_defaults(which = "show_vlan_tag")
333p_show_vlan_tag.add_argument('--vlan_id',
334 required = True,
335 help = "The VLAN ID to search for")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000336
Steve McIntyre460358a2016-03-12 11:57:53 +0000337####################
338# Trunk commands
339####################
340p_list_all_trunks = sp.add_parser("list_all_trunks",
341 help="List all the existing trunks in the system")
342p_list_all_trunks.set_defaults(which = "list_all_trunks")
343p_create_trunk = sp.add_parser("create_trunk",
344 help = "Add a new trunk to the system, linking two ports")
345p_create_trunk.set_defaults(which = "create_trunk")
346p_create_trunk.add_argument('--port_id1',
347 required = True,
348 help="The ID of the first port to be used")
349p_create_trunk.add_argument('--port_id2',
350 required = True,
351 help="The ID of the second port to be used")
352p_delete_trunk = sp.add_parser("delete_trunk",
353 help = "Remove an existing trunk from the system")
354p_delete_trunk.set_defaults(which = "delete_trunk")
355p_delete_trunk.add_argument('--trunk_id',
356 required = True,
357 help = "The ID of the trunk to remove")
358p_show_trunk = sp.add_parser("show_trunk",
359 help = "Show the details of an existing trunk in the system")
360p_show_trunk.set_defaults(which = "show_trunk")
361p_show_trunk.add_argument('--trunk_id',
362 required = True,
363 help = "The ID of the trunk to show")
Steve McIntyrec4890132015-08-07 15:19:11 +0100364
Steve McIntyre460358a2016-03-12 11:57:53 +0000365args = parser.parse_args()
Steve McIntyrec4890132015-08-07 15:19:11 +0100366
Steve McIntyre460358a2016-03-12 11:57:53 +0000367# Now work out what to do
368if args.which == 'status':
Steve McIntyrebb718cf2014-12-15 16:57:25 +0000369 print 'Config:'
370 print ' knows about %d switch(es)' % len(config.switches)
Steve McIntyre7103de22014-12-17 13:16:48 +0000371 default_vlan_id = call_vland('db_query',
372 {'command':'db.get_vlan_id_by_tag',
Steve McIntyree3f3eb32014-12-17 13:23:30 +0000373 'data':
374 {'tag': config.vland.default_vlan_tag}})
Steve McIntyre460358a2016-03-12 11:57:53 +0000375 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 +0000376 stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
377 print 'VLANd is running %s' % stat['running']
Steve McIntyreaf24aaa2015-10-23 17:59:04 +0100378 lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f')
Steve McIntyreea343aa2015-10-23 17:46:17 +0100379 print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z')
Steve McIntyref1c04f92014-12-16 18:23:15 +0000380 print 'DB via VLANd:'
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000381 switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000382 print ' knows about %d switch(es)' % len(switches)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000383 ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000384 print ' knows about %d port(s)' % len(ports)
Steve McIntyre091e2ac2014-12-16 19:20:07 +0000385 vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
Steve McIntyref1c04f92014-12-16 18:23:15 +0000386 print ' DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre460358a2016-03-12 11:57:53 +0000387elif args.which == 'shutdown':
Steve McIntyre06fe6422015-01-23 17:55:43 +0000388 print 'Asking VLANd to shutdown'
Steve McIntyree26a1472015-04-01 18:03:31 +0100389 shutdown = call_vland('daemon_query',
390 {'command':'daemon.shutdown',
391 'data': None})
392 for field in shutdown:
393 print '%s: %s' % (field, shutdown[field])
Steve McIntyre460358a2016-03-12 11:57:53 +0000394elif args.which == 'vland_version':
395 ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
396 print 'VLANd version %s' % ver['version']
397elif args.which == 'statistics':
398 stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
399 print 'VLANd uptime: %d seconds' % stats['uptime']
400elif args.which == 'version':
401 print 'VLANd admin interface version %s' % version
402elif args.which == 'auto_import_switch':
403 print 'Attempting to import switch %s' % args.name
404 if args.name not in config.switches:
405 raise InputError("Can't find switch %s in config" % args.name)
406 imp = call_vland('vlan_update',
407 {'command':'api.auto_import_switch',
408 'data':
409 {'switch': args.name}})
410 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'])
411elif args.which == 'probe_switches':
412 print 'Asking VLANd to probe all the configured switches'
413 probe = call_vland('daemon_query',
414 {'command':'daemon.probe_switches',
415 'data': None})
416 for field in probe:
417 print '%s: %s' % (field, probe[field])
418elif args.which == 'list_all_switches':
419 result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
420 for line in result:
421 dump_switch(line)
422elif args.which == 'list_all_ports':
423 result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
424 for line in result:
425 dump_port(line)
426elif args.which == 'list_all_vlans':
427 result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
428 for line in result:
429 dump_vlan(line)
430elif args.which == 'list_all_trunks':
431 result = call_vland('db_query', {'command':'db.all_trunks', 'data':None})
432 for line in result:
433 dump_trunk(line)
434elif args.which == 'create_switch':
435 try:
436 switch_id = call_vland('db_update',
437 {'command':'db.create_switch',
438 'data':
439 {'name':args.name}})
440 print 'Created switch_id %d' % switch_id
441 except InputError as inst:
442 print 'Failed: %s' % inst
443elif args.which == 'create_port':
444 try:
445 port_id = call_vland('db_update',
446 {'command':'db.create_port',
447 'data':
448 {'switch_id': args.switch_id,
449 'name': args.name,
450 'number': args.number}})
451 print 'Created port_id %d' % port_id
452 except InputError as inst:
453 print 'Failed: %s' % inst
454elif args.which == 'create_vlan':
455 try:
456 (vlan_id, vlan_tag) = call_vland('vlan_update',
457 {'command':'api.create_vlan',
458 'data':
459 {'name': args.name,
460 'tag': args.tag,
461 'is_base_vlan': is_positive(args.is_base_vlan)}})
462 print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
463 except InputError as inst:
464 print 'Failed: %s' % inst
465elif args.which == 'create_trunk':
466 try:
467 trunk_id = call_vland('db_update',
468 {'command':'db.create_trunk',
469 'data':
470 {'port_id1': args.port_id1,
471 'port_id2': args.port_id2}})
472 print 'Created trunk_id %d' % trunk_id
473 except InputError as inst:
474 print 'Failed: %s' % inst
475elif args.which == 'delete_switch':
476 try:
477 switch_id = call_vland('db_update',
478 {'command':'db.delete_switch',
479 'data': {'switch_id': args.switch_id}})
480 print 'Deleted switch_id %s' % switch_id
481 except InputError as inst:
482 print 'Failed: %s' % inst
483elif args.which == 'delete_port':
484 try:
485 port_id = call_vland('db_update',
486 {'command':'db.delete_port',
487 'data': {'port_id': args.port_id}})
488 print 'Deleted port_id %s' % port_id
489 except InputError as inst:
490 print 'Failed: %s' % inst
491elif args.which == 'delete_vlan':
492 try:
493 vlan_id = call_vland('vlan_update',
494 {'command':'api.delete_vlan',
495 'data': {'vlan_id': args.vlan_id}})
496 print 'Deleted vlan_id %d' % vlan_id
497 except InputError as inst:
498 print 'Failed: %s' % inst
499elif args.which == 'delete_trunk':
500 try:
501 port_id = call_vland('db_update',
502 {'command':'db.delete_trunk',
503 'data': {'trunk_id': args.trunk_id}})
504 print 'Deleted trunk_id %s' % trunk_id
505 except InputError as inst:
506 print 'Failed: %s' % inst
507elif args.which == 'lookup_switch_by_name':
508 try:
509 switch_id = call_vland('db_query',
510 {'command':'db.get_switch_id_by_name',
511 'data':{'name':args.name}})
512 if switch_id is not None:
513 print '%d' % switch_id
514 else:
515 print 'No switch found for name %s' % args.name
516 except InputError as inst:
517 print 'Failed: %s' % inst
518elif args.which == 'show_switch':
519 try:
520 this_switch = call_vland('db_query',
521 {'command':'db.get_switch_by_id',
522 'data':
523 {'switch_id': args.switch_id}})
524 if this_switch is not None:
525 dump_switch(this_switch)
526 else:
527 print 'No switch found for switch_id %s' % args.switch_id
528 except InputError as inst:
529 print 'Failed: %s' % inst
530elif args.which == 'show_port':
531 try:
532 this_port = call_vland('db_query',
533 {'command':'db.get_port_by_id',
534 'data':
535 {'port_id': args.port_id}})
536 if this_port is not None:
537 dump_port(this_port)
538 else:
539 print 'No port found for port_id %s' % args.port_id
540 except InputError as inst:
541 print 'Failed: %s' % inst
542elif args.which == 'lookup_port_by_switch_and_name':
543 try:
544 p = call_vland('db_query',
545 {'command':'db.get_port_by_switch_and_name',
546 'data':
547 {'switch_id': args.switch_id,
548 'name': args.name}})
549 if p is not None:
550 print p
551 else:
552 print 'No port found for switch_id %s, name %s' % (args.switch_id, args.name)
553 except InputError as inst:
554 print 'Failed: %s' % inst
555elif args.which == 'lookup_port_by_switch_and_number':
556 try:
557 p = call_vland('db_query',
558 {'command':'db.get_port_by_switch_and_number',
559 'data':
560 {'switch_id': args.switch_id,
561 'number': args.number}})
562 if p is not None:
563 print p
564 else:
565 print 'No port found for switch_id %s, port number %s' % (args.switch_id, args.number)
566 except InputError as inst:
567 print 'Failed: %s' % inst
568elif args.which == 'lookup_ports_by_switch':
569 try:
570 p = call_vland('db_query',
571 {'command':'db.get_ports_by_switch',
572 'data':
573 {'switch_id': args.switch_id}})
574 if p is not None:
575 for port_id in p:
576 print port_id
577 else:
578 print 'No ports found for switch_id %s' % args.switch_id
579 except InputError as inst:
580 print 'Failed: %s' % inst
581elif args.which == 'lookup_ports_by_current_vlan':
582 try:
583 p = call_vland('db_query',
584 {'command':'db.get_ports_by_current_vlan',
585 'data':
586 {'vlan_id': args.vlan_id}})
587 if p is not None:
588 for port_id in p:
589 print port_id
590 else:
591 print 'No ports found for current vlan_id %s' % args.vlan_id
592 except InputError as inst:
593 print 'Failed: %s' % inst
594elif args.which == 'lookup_ports_by_base_vlan':
595 try:
596 p = call_vland('db_query',
597 {'command':'db.get_ports_by_base_vlan',
598 'data':
599 {'vlan_id': args.vlan_id}})
600 if p is not None:
601 for port_id in p:
602 print port_id
603 else:
604 print 'No ports found for base vlan_id %s' % args.vlan_id
605 except InputError as inst:
606 print 'Failed: %s' % inst
607elif args.which == 'lookup_ports_by_trunk':
608 try:
609 p = call_vland('db_query',
610 {'command':'db.get_ports_by_trunk',
611 'data':
612 {'trunk_id': args.trunk_id}})
613 if p is not None:
614 for port_id in p:
615 print port_id
616 else:
617 print 'No ports found for trunk_id %s' % args.trunk_id
618 except InputError as inst:
619 print 'Failed: %s' % inst
620elif args.which == 'set_port_mode':
621 try:
622 port_id = call_vland('vlan_update',
623 {'command':'api.set_port_mode',
624 'data':
625 {'port_id': args.port_id,
626 'mode': args.mode}})
627 print "Updated mode for port_id %d" % port_id
628 except InputError as inst:
629 print 'Failed: %s' % inst
630elif args.which == 'lock_port':
631 try:
632 port_id = call_vland('db_update',
633 {'command':'db.set_port_is_locked',
634 'data':
635 {'port_id': args.port_id,
636 'is_locked': True}})
637 print "Locked port_id %d" % port_id
638 except InputError as inst:
639 print 'Failed: %s' % inst
640elif args.which == 'unlock_port':
641 try:
642 port_id = call_vland('db_update',
643 {'command':'db.set_port_is_locked',
644 'data':
645 {'port_id': args.port_id,
646 'is_locked': False}})
647 print "Unlocked port_id %d" % port_id
648 except InputError as inst:
649 print 'Failed: %s' % inst
650elif args.which == 'set_port_current_vlan':
651 try:
652 port_id = call_vland('vlan_update',
653 {'command':'api.set_current_vlan',
654 'data':
655 {'port_id': args.port_id,
656 'vlan_id': args.vlan_id}})
657 print "Set current VLAN on port_id %d" % port_id
658 except InputError as inst:
659 print 'Failed: %s' % inst
660elif args.which == 'get_port_current_vlan':
661 try:
662 vlan_id = call_vland('db_query',
663 {'command':'db.get_current_vlan_id_by_port',
664 'data':
665 {'port_id': args.port_id}})
666 if vlan_id is not None:
667 print vlan_id
668 else:
669 print "No current_vlan_id found for port_id %s" % args.port_id
670 except InputError as inst:
671 print 'Failed: %s' % inst
672elif args.which == 'set_port_base_vlan':
673 try:
674 port_id = call_vland('db_update',
675 {'command':'db.set_base_vlan',
676 'data':
677 {'port_id': args.port_id,
678 'base_vlan_id': args.vlan_id}})
679 print "Set base VLAN on port_id %d" % port_id
680 except InputError as inst:
681 print 'Failed: %s' % inst
682elif args.which == 'get_port_base_vlan':
683 try:
684 vlan_id = call_vland('db_query',
685 {'command':'db.get_base_vlan_id_by_port',
686 'data':
687 {'port_id': args.port_id}})
688 if vlan_id is not None:
689 print vlan_id
690 else:
691 print "No base_vlan_id found for port_id %d" % port_id
692 except InputError as inst:
693 print 'Failed: %s' % inst
694elif args.which == 'restore_port_to_base_vlan':
695 try:
696 port_id = call_vland('vlan_update',
697 {'command': 'api.restore_base_vlan',
698 'data':
699 {'port_id': args.port_id}})
700 print "Restored port_id %d back to base VLAN" % port_id
701 except InputError as inst:
702 print 'Failed: %s' % inst
703elif args.which == 'show_vlan':
704 try:
705 v = call_vland('db_query',
706 {'command':'db.get_vlan_by_id',
707 'data':
708 {'vlan_id': args.vlan_id}})
709 if v is not None:
710 dump_vlan(v)
711 else:
712 print 'No VLAN found for vlan_id %s' % args.vlan_id
713 except InputError as inst:
714 print 'Failed: %s' % inst
715elif args.which == 'lookup_vlan_by_tag':
716 try:
717 vlan_id = call_vland('db_query',
718 {'command':'db.get_vlan_id_by_tag',
719 'data':
720 {'tag': args.tag}})
721 if vlan_id is not None:
722 print vlan_id
723 else:
724 print 'No VLAN found for vlan tag %s' % args.tag
725 except InputError as inst:
726 print 'Failed: %s' % inst
727elif args.which == 'show_vlan_tag':
728 try:
729 vlan_tag = call_vland('db_query',
730 {'command':'db.get_vlan_tag_by_id',
731 'data':
732 {'vlan_id': args.vlan_id}})
733 if vlan_tag is not None:
734 print vlan_tag
735 else:
736 print 'No VLAN found for vlan id %s' % args.vlan_id
737 except InputError as inst:
738 print 'Failed: %s' % inst
739elif args.which == 'show_trunk':
740 try:
741 this_trunk = call_vland('db_query',
742 {'command':'db.get_trunk_by_id',
743 'data':
744 {'trunk_id': args.trunk_id}})
745 if this_trunk is not None:
746 dump_trunk(this_trunk)
747 else:
748 print 'No port found for port_id %s' % args.trunk_id
749 except InputError as inst:
750 print 'Failed: %s' % inst
751
Steve McIntyrea0534a52014-12-05 17:58:40 +0000752else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000753 print 'No recognised command given. Try -h for help'