blob: 00971ea8689a1cab7c51d74a620750e587cbf718 [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 McIntyree41e3f32014-12-05 18:08:21 +000052def dump_vlan(vlan):
53 print vlan
54
Steve McIntyre844bfd42014-11-27 16:58:31 +000055#print '%s' % banner
56
57#print 'Connecting to DB...'
58db = VlanDB()
59
Steve McIntyre4b0193d2014-11-28 18:06:12 +000060# For sanity, we need to know the vlan_id for the default vlan (tag
61# 1). Make sure we know that before anybody attempts to create things
62# that depend on it.
63default_vlan_tag = 1
Steve McIntyre50eb0602014-12-05 15:29:04 +000064default_vlan_id = db.get_vlan_id_by_tag(default_vlan_tag)
Steve McIntyrec99e93b2014-12-02 18:21:33 +000065if default_vlan_id is None:
Steve McIntyre4b0193d2014-11-28 18:06:12 +000066 # It doesn't exist - create it and try again
67 default_vlan_id = db.create_vlan("DEFAULT",
68 default_vlan_tag,
69 True)
70
71
Steve McIntyre844bfd42014-11-27 16:58:31 +000072switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000073print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000074
75ports = db.all_ports()
Steve McIntyreb1db7102014-11-28 17:56:12 +000076print 'The DB knows about %d port(s)' % len(ports)
Steve McIntyre844bfd42014-11-27 16:58:31 +000077
78vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000079print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000080
Steve McIntyre4b0193d2014-11-28 18:06:12 +000081print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
82
Steve McIntyre844bfd42014-11-27 16:58:31 +000083usage = 'Usage: %prog --command [command options]'
84commands = ['switch_create',
85 'switch_destroy',
86 'switch_details']
87parser = optparse.OptionParser(usage=usage, description=banner)
88
89# Switch commands
90switch_group = optparse.OptionGroup(parser, "Switch commands")
91switch_group.add_option("--list_all_switches",
92 dest = "list_all_switches",
93 action = "store_true",
94 default = False,
95 help = "List all the existing switches in the system")
96switch_group.add_option("--create_switch",
97 dest = "create_switch",
98 action = "store",
99 type = "string",
100 help = "Add a new switch to the system",
101 nargs = 1,
102 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000103switch_group.add_option("--delete_switch",
104 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000105 action = "store",
106 type = "int",
107 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000108 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000109 nargs = 1,
110 metavar = "<switch_id>")
111switch_group.add_option("--show_switch",
112 dest = "show_switch",
113 action = "store",
114 type = "int",
115 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000116 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000117 nargs = 1,
118 metavar = "<switch_id>")
119switch_group.add_option("--lookup_switch_by_name",
120 dest = "lookup_switch_by_name",
121 action = "store",
122 type = "string",
123 help = "Lookup a switch ID by name",
124 nargs = 1,
125 metavar = "<name>")
126switch_group.add_option("--list_switch_ports",
127 dest = "list_switch_ports",
128 action = "store",
129 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000130 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000131 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000132 nargs = 1,
133 metavar = "<switch_id>")
134parser.add_option_group(switch_group)
135
136# Port commands
137port_group = optparse.OptionGroup(parser, "Port commands")
138port_group.add_option("--list_all_ports",
139 dest = "list_all_ports",
140 action = "store_true",
141 default = False,
142 help = "List all the existing ports in the system")
143port_group.add_option("--create_port",
144 dest = "create_port",
145 action = "store",
146 type = "string",
147 help = "Add a new port to the system",
148 nargs = 2,
149 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000150port_group.add_option("--delete_port",
151 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000152 action = "store",
153 type = "int",
154 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000155 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000156 nargs = 1,
157 metavar = "<port_id>")
158port_group.add_option("--show_port",
159 dest = "show_port",
160 action = "store",
161 type = "int",
162 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000163 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000164 nargs = 1,
165 metavar = "<port_id>")
166port_group.add_option("--lookup_port_by_switch_and_name",
167 dest = "lookup_port_by_switch_and_name",
168 action = "store",
169 type = "string",
170 help = "Lookup a port ID by switch and port name",
171 nargs = 2,
172 metavar = "<switch_id> <name>")
Steve McIntyre71b41022014-12-05 17:17:44 +0000173port_group.add_option("--set_port_mode",
174 dest = "set_port_mode",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000175 action = "store",
176 type = "string",
177 help = "Set the mode of a port to 'trunk' or 'access'",
178 nargs = 2,
179 metavar = "<port_id> <mode>")
180port_group.add_option("--lock_port",
181 dest = "lock_port",
182 action = "store",
183 type = "string",
184 help = "Lock the settings on a port",
185 nargs = 1,
186 metavar = "<port_id>")
187port_group.add_option("--unlock_port",
188 dest = "unlock_port",
189 action = "store",
190 type = "string",
191 help = "Unock the settings on a port",
192 nargs = 1,
193 metavar = "<port_id>")
194port_group.add_option("--set_port_current_vlan",
195 dest = "set_port_current_vlan",
196 action = "store",
197 type = "int",
198 help = "Set the current VLAN assignment for a port",
199 nargs = 2,
200 metavar = "<port_id> <vlan_id>")
201port_group.add_option("--get_port_current_vlan",
202 dest = "get_port_current_vlan",
203 action = "store",
204 type = "int",
205 help = "Get the current VLAN assignment for a port",
206 nargs = 1,
207 metavar = "<port_id>")
208port_group.add_option("--set_port_base_vlan",
209 dest = "set_port_base_vlan",
210 action = "store",
211 type = "int",
212 help = "Set the base VLAN assignment for a port",
213 nargs = 2,
214 metavar = "<port_id> <vlan_id>")
215port_group.add_option("--get_port_base_vlan",
216 dest = "get_port_base_vlan",
217 action = "store",
218 type = "int",
219 help = "Get the base VLAN assignment for a port",
220 nargs = 1,
221 metavar = "<port_id>")
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000222port_group.add_option("--restore_port_to_base_vlan",
223 dest = "restore_port_to_base_vlan",
224 action = "store",
225 type = "int",
226 help = "Reset the port back to its base VLAN",
227 nargs = 1,
228 metavar = "<port_id>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000229parser.add_option_group(port_group)
230
231# VLAN commands
232vlan_group = optparse.OptionGroup(parser, "VLAN commands")
233vlan_group.add_option("--list_all_vlans",
234 dest = "list_all_vlans",
235 action = "store_true",
236 default = False,
237 help = "List all the existing vlans in the system")
238vlan_group.add_option("--create_vlan",
239 dest = "create_vlan",
240 action = "store",
241 type = "string",
242 help = "Add a new vlan to the system",
243 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000244 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000245vlan_group.add_option("--delete_vlan",
246 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000247 action = "store",
248 type = "int",
249 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000250 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000251 nargs = 1,
252 metavar = "<vlan_id>")
253vlan_group.add_option("--show_vlan",
254 dest = "show_vlan",
255 action = "store",
256 type = "int",
257 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000258 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000259 nargs = 1,
260 metavar = "<vlan_id>")
261parser.add_option_group(vlan_group)
262
263(opts, args) = parser.parse_args()
264
Steve McIntyre844bfd42014-11-27 16:58:31 +0000265if opts.list_all_switches:
266 result = db.all_switches()
267 count = 0;
268 for line in result:
269 print line
270 count += 1
271 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000272elif opts.list_all_ports:
273 result = db.all_ports()
274 count = 0;
275 for line in result:
276 print line
277 count += 1
278 print '%d entries' % count
279elif opts.list_all_vlans:
280 result = db.all_vlans()
281 count = 0;
282 for line in result:
283 print line
284 count += 1
285 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000286elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000287 try:
288 switch_id = db.create_switch(opts.create_switch)
289 print 'Created switch_id %d' % switch_id
290 except InputError as inst:
291 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000292elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000293 try:
294 port_id = db.create_port(opts.create_port[0],
295 opts.create_port[1],
296 default_vlan_id, default_vlan_id)
297 print 'Created port_id %d' % port_id
298 except InputError as inst:
299 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000300elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000301 try:
302 vlan_id = db.create_vlan(opts.create_vlan[0],
303 opts.create_vlan[1],
Steve McIntyreae95fd62014-12-05 16:51:41 +0000304 is_positive(opts.create_vlan[2]))
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000305 print 'Created vlan_id %d' % vlan_id
306 except InputError as inst:
307 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000308elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000309 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000310 switch_id = db.delete_switch(opts.delete_switch)
Steve McIntyre64e38862014-12-02 17:19:37 +0000311 print 'Deleted switch_id %d' % switch_id
312 except InputError as inst:
313 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000314elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000315 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000316 port_id = db.delete_port(opts.delete_port)
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000317 print 'Deleted port_id %d' % port_id
318 except InputError as inst:
319 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000320elif opts.delete_vlan is not None:
321 try:
322 vlan_id = db.delete_vlan(opts.delete_vlan)
323 print 'Deleted vlan_id %d' % vlan_id
324 except InputError as inst:
325 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000326elif opts.lookup_switch_by_name is not None:
327 try:
328 switch_id = db.get_switch_id_by_name(opts.lookup_switch_by_name)
329 if switch_id is not None:
330 print 'Switch %s has switch_id %d' % (opts.lookup_switch_by_name, switch_id)
331 else:
332 print 'No switch found for name %s' % opts.lookup_switch_by_name
333 except InputError as inst:
334 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000335elif opts.show_switch is not None:
336 try:
337 switch = db.get_switch_by_id(opts.show_switch)
338 if switch is not None:
339 dump_switch(switch)
340 else:
341 print 'No switch found for switch_id %d' % opts.show_switch
342 except InputError as inst:
343 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000344elif opts.list_switch_ports is not None:
345 try:
346 ports = db.get_ports_by_switch(opts.list_switch_ports)
347 if ports is not None:
348 for port in ports:
349 dump_port(port)
350 else:
351 print 'No ports found for switch_id %d' % opts.list_switch_ports
352 except InputError as inst:
353 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000354elif opts.show_port is not None:
355 try:
356 port = db.get_port_by_id(opts.show_port)
357 if port is not None:
358 dump_port(port)
359 else:
360 print 'No port found for port_id %d' % opts.show_port
361 except InputError as inst:
362 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000363elif opts.lookup_port_by_switch_and_name is not None:
364 try:
365 port = db.get_port_by_switch_and_name(opts.lookup_port_by_switch_and_name[0], opts.lookup_port_by_switch_and_name[1])
366 if port is not None:
367 print port
368 else:
369 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])
370 except InputError as inst:
371 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000372elif opts.set_port_mode is not None:
373 try:
374 port_id = db.set_port_mode(opts.set_port_mode[0], opts.set_port_mode[1])
375 print "Updated mode for port_id %d" % port_id
376 except InputError as inst:
377 print 'Failed: %s' % inst
Steve McIntyre75dc4ec2014-12-05 17:20:42 +0000378elif opts.lock_port is not None:
379 try:
380 port_id = db.set_port_is_locked(opts.lock_port, True)
381 print "Locked port_id %d" % port_id
382 except InputError as inst:
383 print 'Failed: %s' % inst
384elif opts.unlock_port is not None:
385 try:
386 port_id = db.set_port_is_locked(opts.unlock_port, False)
387 print "Unlocked port_id %d" % port_id
388 except InputError as inst:
389 print 'Failed: %s' % inst
Steve McIntyrea2bbcda2014-12-05 17:57:36 +0000390elif opts.set_port_current_vlan is not None:
391 try:
392 port_id = db.set_current_vlan(opts.set_port_current_vlan[0], opts.set_port_current_vlan[1])
393 print "Set current VLAN on port_id %d" % port_id
394 except InputError as inst:
395 print 'Failed: %s' % inst
396elif opts.get_port_current_vlan is not None:
397 try:
398 vlan_id = db.get_current_vlan_id_by_port(opts.get_port_current_vlan)
399 if vlan_id is not None:
400 print vlan_id
401 else:
402 print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
403 except InputError as inst:
404 print 'Failed: %s' % inst
405elif opts.set_port_base_vlan is not None:
406 try:
407 port_id = db.set_base_vlan(opts.set_port_base_vlan[0], opts.set_port_base_vlan[1])
408 print "Set base VLAN on port_id %d" % port_id
409 except InputError as inst:
410 print 'Failed: %s' % inst
411elif opts.get_port_base_vlan is not None:
412 try:
413 vlan_id = db.get_base_vlan_id_by_port(opts.get_port_base_vlan)
414 if vlan_id is not None:
415 print vlan_id
416 else:
417 print "No base_vlan_id found for port_id %d" % port_id
418 except InputError as inst:
419 print 'Failed: %s' % inst
420elif opts.restore_port_to_base_vlan is not None:
421 try:
422 port_id = db.restore_base_vlan(opts.restore_port_to_base_vlan)
423 print "Restored port_id %d back to base VLAN" % port_id
424 except InputError as inst:
425 print 'Failed: %s' % inst
Steve McIntyree41e3f32014-12-05 18:08:21 +0000426elif opts.show_vlan is not None:
427 try:
428 vlan = db.get_vlan_by_id(opts.show_vlan)
429 if vlan is not None:
430 dump_vlan(vlan)
431 else:
432 print 'No vlan found for vlan_id %d' % opts.show_vlan
433 except InputError as inst:
434 print 'Failed: %s' % inst
Steve McIntyrea0534a52014-12-05 17:58:40 +0000435else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000436 print 'No recognised command given. Try -h for help'