blob: 64a869ac4e7fd02eb094341b6cba205044c03101 [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
23import os, sys, types
24import time
25import logging
26import optparse
Steve McIntyrea4ad70e2014-12-02 12:39:43 +000027from errors import CriticalError, InputError
Steve McIntyre844bfd42014-11-27 16:58:31 +000028
29vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
30sys.path.insert(0, vlandpath)
31
32import drivers
33from db.db import VlanDB
34
35version = "0.0.0-DEV"
36banner = "Linaro VLANd admin interface, version %s" % version
37
Steve McIntyreae95fd62014-12-05 16:51:41 +000038def is_positive(text):
39 if text in ('1', 'y', 'Y', 't', 'T', 'True'):
40 return True
41 elif text in ('0', 'n', 'N', 'f', 'F', 'False'):
42 return False
43 else:
44 raise InputError("Cannot parse \"%s\" as True or False" % text)
45
Steve McIntyrea132c362014-12-05 15:53:21 +000046def dump_switch(switch):
47 print switch
48
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000049def dump_port(port):
50 print port
51
Steve McIntyre844bfd42014-11-27 16:58:31 +000052#print '%s' % banner
53
54#print 'Connecting to DB...'
55db = VlanDB()
56
Steve McIntyre4b0193d2014-11-28 18:06:12 +000057# For sanity, we need to know the vlan_id for the default vlan (tag
58# 1). Make sure we know that before anybody attempts to create things
59# that depend on it.
60default_vlan_tag = 1
Steve McIntyre50eb0602014-12-05 15:29:04 +000061default_vlan_id = db.get_vlan_id_by_tag(default_vlan_tag)
Steve McIntyrec99e93b2014-12-02 18:21:33 +000062if default_vlan_id is None:
Steve McIntyre4b0193d2014-11-28 18:06:12 +000063 # It doesn't exist - create it and try again
64 default_vlan_id = db.create_vlan("DEFAULT",
65 default_vlan_tag,
66 True)
67
68
Steve McIntyre844bfd42014-11-27 16:58:31 +000069switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000070print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000071
72ports = db.all_ports()
Steve McIntyreb1db7102014-11-28 17:56:12 +000073print 'The DB knows about %d port(s)' % len(ports)
Steve McIntyre844bfd42014-11-27 16:58:31 +000074
75vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000076print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000077
Steve McIntyre4b0193d2014-11-28 18:06:12 +000078print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
79
Steve McIntyre844bfd42014-11-27 16:58:31 +000080usage = 'Usage: %prog --command [command options]'
81commands = ['switch_create',
82 'switch_destroy',
83 'switch_details']
84parser = optparse.OptionParser(usage=usage, description=banner)
85
86# Switch commands
87switch_group = optparse.OptionGroup(parser, "Switch commands")
88switch_group.add_option("--list_all_switches",
89 dest = "list_all_switches",
90 action = "store_true",
91 default = False,
92 help = "List all the existing switches in the system")
93switch_group.add_option("--create_switch",
94 dest = "create_switch",
95 action = "store",
96 type = "string",
97 help = "Add a new switch to the system",
98 nargs = 1,
99 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000100switch_group.add_option("--delete_switch",
101 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000102 action = "store",
103 type = "int",
104 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000105 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000106 nargs = 1,
107 metavar = "<switch_id>")
108switch_group.add_option("--show_switch",
109 dest = "show_switch",
110 action = "store",
111 type = "int",
112 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000113 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000114 nargs = 1,
115 metavar = "<switch_id>")
116switch_group.add_option("--lookup_switch_by_name",
117 dest = "lookup_switch_by_name",
118 action = "store",
119 type = "string",
120 help = "Lookup a switch ID by name",
121 nargs = 1,
122 metavar = "<name>")
123switch_group.add_option("--list_switch_ports",
124 dest = "list_switch_ports",
125 action = "store",
126 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000127 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000128 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000129 nargs = 1,
130 metavar = "<switch_id>")
131parser.add_option_group(switch_group)
132
133# Port commands
134port_group = optparse.OptionGroup(parser, "Port commands")
135port_group.add_option("--list_all_ports",
136 dest = "list_all_ports",
137 action = "store_true",
138 default = False,
139 help = "List all the existing ports in the system")
140port_group.add_option("--create_port",
141 dest = "create_port",
142 action = "store",
143 type = "string",
144 help = "Add a new port to the system",
145 nargs = 2,
146 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000147port_group.add_option("--delete_port",
148 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000149 action = "store",
150 type = "int",
151 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000152 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000153 nargs = 1,
154 metavar = "<port_id>")
155port_group.add_option("--show_port",
156 dest = "show_port",
157 action = "store",
158 type = "int",
159 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000160 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000161 nargs = 1,
162 metavar = "<port_id>")
163port_group.add_option("--lookup_port_by_switch_and_name",
164 dest = "lookup_port_by_switch_and_name",
165 action = "store",
166 type = "string",
167 help = "Lookup a port ID by switch and port name",
168 nargs = 2,
169 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000170port_group.add_option("--set_port_mode",
171 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000172 action = "store",
173 type = "string",
174 help = "Set the mode of a port to 'trunk' or 'access'",
175 nargs = 2,
176 metavar = "<port_id> <mode>")
177port_group.add_option("--lock_port",
178 dest = "lock_port",
179 action = "store",
180 type = "string",
181 help = "Lock the settings on a port",
182 nargs = 1,
183 metavar = "<port_id>")
184port_group.add_option("--unlock_port",
185 dest = "unlock_port",
186 action = "store",
187 type = "string",
188 help = "Unock the settings on a port",
189 nargs = 1,
190 metavar = "<port_id>")
191port_group.add_option("--set_port_current_vlan",
192 dest = "set_port_current_vlan",
193 action = "store",
194 type = "int",
195 help = "Set the current VLAN assignment for a port",
196 nargs = 2,
197 metavar = "<port_id> <vlan_id>")
198port_group.add_option("--get_port_current_vlan",
199 dest = "get_port_current_vlan",
200 action = "store",
201 type = "int",
202 help = "Get the current VLAN assignment for a port",
203 nargs = 1,
204 metavar = "<port_id>")
205port_group.add_option("--set_port_base_vlan",
206 dest = "set_port_base_vlan",
207 action = "store",
208 type = "int",
209 help = "Set the base VLAN assignment for a port",
210 nargs = 2,
211 metavar = "<port_id> <vlan_id>")
212port_group.add_option("--get_port_base_vlan",
213 dest = "get_port_base_vlan",
214 action = "store",
215 type = "int",
216 help = "Get the base VLAN assignment for a port",
217 nargs = 1,
218 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000219port_group.add_option("--restore_port_to_base_vlan",
220 dest = "restore_port_to_base_vlan",
221 action = "store",
222 type = "int",
223 help = "Reset the port back to its base VLAN",
224 nargs = 1,
225 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000226parser.add_option_group(port_group)
227
228# VLAN commands
229vlan_group = optparse.OptionGroup(parser, "VLAN commands")
230vlan_group.add_option("--list_all_vlans",
231 dest = "list_all_vlans",
232 action = "store_true",
233 default = False,
234 help = "List all the existing vlans in the system")
235vlan_group.add_option("--create_vlan",
236 dest = "create_vlan",
237 action = "store",
238 type = "string",
239 help = "Add a new vlan to the system",
240 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000241 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000242vlan_group.add_option("--delete_vlan",
243 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000244 action = "store",
245 type = "int",
246 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000247 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000248 nargs = 1,
249 metavar = "<vlan_id>")
250vlan_group.add_option("--show_vlan",
251 dest = "show_vlan",
252 action = "store",
253 type = "int",
254 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000255 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000256 nargs = 1,
257 metavar = "<vlan_id>")
258parser.add_option_group(vlan_group)
259
260(opts, args) = parser.parse_args()
261
Steve McIntyre844bfd42014-11-27 16:58:31 +0000262if opts.list_all_switches:
263 result = db.all_switches()
264 count = 0;
265 for line in result:
266 print line
267 count += 1
268 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000269elif opts.list_all_ports:
270 result = db.all_ports()
271 count = 0;
272 for line in result:
273 print line
274 count += 1
275 print '%d entries' % count
276elif opts.list_all_vlans:
277 result = db.all_vlans()
278 count = 0;
279 for line in result:
280 print line
281 count += 1
282 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000283elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000284 try:
285 switch_id = db.create_switch(opts.create_switch)
286 print 'Created switch_id %d' % switch_id
287 except InputError as inst:
288 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000289elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000290 try:
291 port_id = db.create_port(opts.create_port[0],
292 opts.create_port[1],
293 default_vlan_id, default_vlan_id)
294 print 'Created port_id %d' % port_id
295 except InputError as inst:
296 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000297elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000298 try:
299 vlan_id = db.create_vlan(opts.create_vlan[0],
300 opts.create_vlan[1],
Steve McIntyreae95fd62014-12-05 16:51:41 +0000301 is_positive(opts.create_vlan[2]))
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000302 print 'Created vlan_id %d' % vlan_id
303 except InputError as inst:
304 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000305elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000306 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000307 switch_id = db.delete_switch(opts.delete_switch)
Steve McIntyre64e38862014-12-02 17:19:37 +0000308 print 'Deleted switch_id %d' % switch_id
309 except InputError as inst:
310 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000311elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000312 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000313 port_id = db.delete_port(opts.delete_port)
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000314 print 'Deleted port_id %d' % port_id
315 except InputError as inst:
316 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000317elif opts.delete_vlan is not None:
318 try:
319 vlan_id = db.delete_vlan(opts.delete_vlan)
320 print 'Deleted vlan_id %d' % vlan_id
321 except InputError as inst:
322 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000323elif opts.lookup_switch_by_name is not None:
324 try:
325 switch_id = db.get_switch_id_by_name(opts.lookup_switch_by_name)
326 if switch_id is not None:
327 print 'Switch %s has switch_id %d' % (opts.lookup_switch_by_name, switch_id)
328 else:
329 print 'No switch found for name %s' % opts.lookup_switch_by_name
330 except InputError as inst:
331 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000332elif opts.show_switch is not None:
333 try:
334 switch = db.get_switch_by_id(opts.show_switch)
335 if switch is not None:
336 dump_switch(switch)
337 else:
338 print 'No switch found for switch_id %d' % opts.show_switch
339 except InputError as inst:
340 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000341elif opts.list_switch_ports is not None:
342 try:
343 ports = db.get_ports_by_switch(opts.list_switch_ports)
344 if ports is not None:
345 for port in ports:
346 dump_port(port)
347 else:
348 print 'No ports found for switch_id %d' % opts.list_switch_ports
349 except InputError as inst:
350 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000351elif opts.show_port is not None:
352 try:
353 port = db.get_port_by_id(opts.show_port)
354 if port is not None:
355 dump_port(port)
356 else:
357 print 'No port found for port_id %d' % opts.show_port
358 except InputError as inst:
359 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000360elif opts.lookup_port_by_switch_and_name is not None:
361 try:
362 port = db.get_port_by_switch_and_name(opts.lookup_port_by_switch_and_name[0], opts.lookup_port_by_switch_and_name[1])
363 if port is not None:
364 print port
365 else:
366 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])
367 except InputError as inst:
368 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000369elif opts.set_port_mode is not None:
370 try:
371 port_id = db.set_port_mode(opts.set_port_mode[0], opts.set_port_mode[1])
372 print "Updated mode for port_id %d" % port_id
373 except InputError as inst:
374 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000375elif opts.lock_port is not None:
376 try:
377 port_id = db.set_port_is_locked(opts.lock_port, True)
378 print "Locked port_id %d" % port_id
379 except InputError as inst:
380 print 'Failed: %s' % inst
381elif opts.unlock_port is not None:
382 try:
383 port_id = db.set_port_is_locked(opts.unlock_port, False)
384 print "Unlocked port_id %d" % port_id
385 except InputError as inst:
386 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000387elif opts.set_port_current_vlan is not None:
388 try:
389 port_id = db.set_current_vlan(opts.set_port_current_vlan[0], opts.set_port_current_vlan[1])
390 print "Set current VLAN on port_id %d" % port_id
391 except InputError as inst:
392 print 'Failed: %s' % inst
393elif opts.get_port_current_vlan is not None:
394 try:
395 vlan_id = db.get_current_vlan_id_by_port(opts.get_port_current_vlan)
396 if vlan_id is not None:
397 print vlan_id
398 else:
399 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
400 except InputError as inst:
401 print 'Failed: %s' % inst
402elif opts.set_port_base_vlan is not None:
403 try:
404 port_id = db.set_base_vlan(opts.set_port_base_vlan[0], opts.set_port_base_vlan[1])
405 print "Set base VLAN on port_id %d" % port_id
406 except InputError as inst:
407 print 'Failed: %s' % inst
408elif opts.get_port_base_vlan is not None:
409 try:
410 vlan_id = db.get_base_vlan_id_by_port(opts.get_port_base_vlan)
411 if vlan_id is not None:
412 print vlan_id
413 else:
414 print "No base_vlan_id found for port_id %d" % port_id
415 except InputError as inst:
416 print 'Failed: %s' % inst
417elif opts.restore_port_to_base_vlan is not None:
418 try:
419 port_id = db.restore_base_vlan(opts.restore_port_to_base_vlan)
420 print "Restored port_id %d back to base VLAN" % port_id
421 except InputError as inst:
422 print 'Failed: %s' % inst
Steve McIntyrea0534a52014-12-05 17:58:40 +0000423else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000424 print 'No recognised command given. Try -h for help'