blob: e142265c9bcc8e98ea8aabb4617ea193c1f0a87d [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>")
219parser.add_option_group(port_group)
220
221# VLAN commands
222vlan_group = optparse.OptionGroup(parser, "VLAN commands")
223vlan_group.add_option("--list_all_vlans",
224 dest = "list_all_vlans",
225 action = "store_true",
226 default = False,
227 help = "List all the existing vlans in the system")
228vlan_group.add_option("--create_vlan",
229 dest = "create_vlan",
230 action = "store",
231 type = "string",
232 help = "Add a new vlan to the system",
233 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000234 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000235vlan_group.add_option("--delete_vlan",
236 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000237 action = "store",
238 type = "int",
239 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000240 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000241 nargs = 1,
242 metavar = "<vlan_id>")
243vlan_group.add_option("--show_vlan",
244 dest = "show_vlan",
245 action = "store",
246 type = "int",
247 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000248 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000249 nargs = 1,
250 metavar = "<vlan_id>")
251parser.add_option_group(vlan_group)
252
253(opts, args) = parser.parse_args()
254
Steve McIntyre844bfd42014-11-27 16:58:31 +0000255if opts.list_all_switches:
256 result = db.all_switches()
257 count = 0;
258 for line in result:
259 print line
260 count += 1
261 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000262elif opts.list_all_ports:
263 result = db.all_ports()
264 count = 0;
265 for line in result:
266 print line
267 count += 1
268 print '%d entries' % count
269elif opts.list_all_vlans:
270 result = db.all_vlans()
271 count = 0;
272 for line in result:
273 print line
274 count += 1
275 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000276elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000277 try:
278 switch_id = db.create_switch(opts.create_switch)
279 print 'Created switch_id %d' % switch_id
280 except InputError as inst:
281 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000282elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000283 try:
284 port_id = db.create_port(opts.create_port[0],
285 opts.create_port[1],
286 default_vlan_id, default_vlan_id)
287 print 'Created port_id %d' % port_id
288 except InputError as inst:
289 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000290elif opts.create_vlan is not None:
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000291 try:
292 vlan_id = db.create_vlan(opts.create_vlan[0],
293 opts.create_vlan[1],
Steve McIntyreae95fd62014-12-05 16:51:41 +0000294 is_positive(opts.create_vlan[2]))
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000295 print 'Created vlan_id %d' % vlan_id
296 except InputError as inst:
297 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000298elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000299 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000300 switch_id = db.delete_switch(opts.delete_switch)
Steve McIntyre64e38862014-12-02 17:19:37 +0000301 print 'Deleted switch_id %d' % switch_id
302 except InputError as inst:
303 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000304elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000305 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000306 port_id = db.delete_port(opts.delete_port)
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000307 print 'Deleted port_id %d' % port_id
308 except InputError as inst:
309 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000310elif opts.delete_vlan is not None:
311 try:
312 vlan_id = db.delete_vlan(opts.delete_vlan)
313 print 'Deleted vlan_id %d' % vlan_id
314 except InputError as inst:
315 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000316elif opts.lookup_switch_by_name is not None:
317 try:
318 switch_id = db.get_switch_id_by_name(opts.lookup_switch_by_name)
319 if switch_id is not None:
320 print 'Switch %s has switch_id %d' % (opts.lookup_switch_by_name, switch_id)
321 else:
322 print 'No switch found for name %s' % opts.lookup_switch_by_name
323 except InputError as inst:
324 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000325elif opts.show_switch is not None:
326 try:
327 switch = db.get_switch_by_id(opts.show_switch)
328 if switch is not None:
329 dump_switch(switch)
330 else:
331 print 'No switch found for switch_id %d' % opts.show_switch
332 except InputError as inst:
333 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000334elif opts.list_switch_ports is not None:
335 try:
336 ports = db.get_ports_by_switch(opts.list_switch_ports)
337 if ports is not None:
338 for port in ports:
339 dump_port(port)
340 else:
341 print 'No ports found for switch_id %d' % opts.list_switch_ports
342 except InputError as inst:
343 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000344elif opts.show_port is not None:
345 try:
346 port = db.get_port_by_id(opts.show_port)
347 if port is not None:
348 dump_port(port)
349 else:
350 print 'No port found for port_id %d' % opts.show_port
351 except InputError as inst:
352 print 'Failed: %s' % inst
Steve McIntyre73106572014-12-05 16:13:36 +0000353elif opts.lookup_port_by_switch_and_name is not None:
354 try:
355 port = db.get_port_by_switch_and_name(opts.lookup_port_by_switch_and_name[0], opts.lookup_port_by_switch_and_name[1])
356 if port is not None:
357 print port
358 else:
359 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])
360 except InputError as inst:
361 print 'Failed: %s' % inst
Steve McIntyre71b41022014-12-05 17:17:44 +0000362elif opts.set_port_mode is not None:
363 try:
364 port_id = db.set_port_mode(opts.set_port_mode[0], opts.set_port_mode[1])
365 print "Updated mode for port_id %d" % port_id
366 except InputError as inst:
367 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000368else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000369 print 'No recognised command given. Try -h for help'