blob: 91c26979306fcacbe184dc092c1486c43ba53233 [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 McIntyrea132c362014-12-05 15:53:21 +000038def dump_switch(switch):
39 print switch
40
Steve McIntyre11e4cbd2014-12-05 16:03:03 +000041def dump_port(port):
42 print port
43
Steve McIntyre844bfd42014-11-27 16:58:31 +000044#print '%s' % banner
45
46#print 'Connecting to DB...'
47db = VlanDB()
48
Steve McIntyre4b0193d2014-11-28 18:06:12 +000049# For sanity, we need to know the vlan_id for the default vlan (tag
50# 1). Make sure we know that before anybody attempts to create things
51# that depend on it.
52default_vlan_tag = 1
Steve McIntyre50eb0602014-12-05 15:29:04 +000053default_vlan_id = db.get_vlan_id_by_tag(default_vlan_tag)
Steve McIntyrec99e93b2014-12-02 18:21:33 +000054if default_vlan_id is None:
Steve McIntyre4b0193d2014-11-28 18:06:12 +000055 # It doesn't exist - create it and try again
56 default_vlan_id = db.create_vlan("DEFAULT",
57 default_vlan_tag,
58 True)
59
60
Steve McIntyre844bfd42014-11-27 16:58:31 +000061switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000062print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000063
64ports = db.all_ports()
Steve McIntyreb1db7102014-11-28 17:56:12 +000065print 'The DB knows about %d port(s)' % len(ports)
Steve McIntyre844bfd42014-11-27 16:58:31 +000066
67vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000068print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000069
Steve McIntyre4b0193d2014-11-28 18:06:12 +000070print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
71
Steve McIntyre844bfd42014-11-27 16:58:31 +000072usage = 'Usage: %prog --command [command options]'
73commands = ['switch_create',
74 'switch_destroy',
75 'switch_details']
76parser = optparse.OptionParser(usage=usage, description=banner)
77
78# Switch commands
79switch_group = optparse.OptionGroup(parser, "Switch commands")
80switch_group.add_option("--list_all_switches",
81 dest = "list_all_switches",
82 action = "store_true",
83 default = False,
84 help = "List all the existing switches in the system")
85switch_group.add_option("--create_switch",
86 dest = "create_switch",
87 action = "store",
88 type = "string",
89 help = "Add a new switch to the system",
90 nargs = 1,
91 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +000092switch_group.add_option("--delete_switch",
93 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +000094 action = "store",
95 type = "int",
96 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +000097 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +000098 nargs = 1,
99 metavar = "<switch_id>")
100switch_group.add_option("--show_switch",
101 dest = "show_switch",
102 action = "store",
103 type = "int",
104 help = "Show the details of an existing switch in 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("--lookup_switch_by_name",
109 dest = "lookup_switch_by_name",
110 action = "store",
111 type = "string",
112 help = "Lookup a switch ID by name",
113 nargs = 1,
114 metavar = "<name>")
115switch_group.add_option("--list_switch_ports",
116 dest = "list_switch_ports",
117 action = "store",
118 type = "int",
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000119 help = "List the IDs of the ports on an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000120 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000121 nargs = 1,
122 metavar = "<switch_id>")
123parser.add_option_group(switch_group)
124
125# Port commands
126port_group = optparse.OptionGroup(parser, "Port commands")
127port_group.add_option("--list_all_ports",
128 dest = "list_all_ports",
129 action = "store_true",
130 default = False,
131 help = "List all the existing ports in the system")
132port_group.add_option("--create_port",
133 dest = "create_port",
134 action = "store",
135 type = "string",
136 help = "Add a new port to the system",
137 nargs = 2,
138 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000139port_group.add_option("--delete_port",
140 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000141 action = "store",
142 type = "int",
143 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000144 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000145 nargs = 1,
146 metavar = "<port_id>")
147port_group.add_option("--show_port",
148 dest = "show_port",
149 action = "store",
150 type = "int",
151 help = "Show the details of an existing port in 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("--lookup_port_by_switch_and_name",
156 dest = "lookup_port_by_switch_and_name",
157 action = "store",
158 type = "string",
159 help = "Lookup a port ID by switch and port name",
160 nargs = 2,
161 metavar = "<switch_id> <name>")
162port_group.add_option("--set_port_type",
163 dest = "set_port_type",
164 action = "store",
165 type = "string",
166 help = "Set the mode of a port to 'trunk' or 'access'",
167 nargs = 2,
168 metavar = "<port_id> <mode>")
169port_group.add_option("--lock_port",
170 dest = "lock_port",
171 action = "store",
172 type = "string",
173 help = "Lock the settings on a port",
174 nargs = 1,
175 metavar = "<port_id>")
176port_group.add_option("--unlock_port",
177 dest = "unlock_port",
178 action = "store",
179 type = "string",
180 help = "Unock the settings on a port",
181 nargs = 1,
182 metavar = "<port_id>")
183port_group.add_option("--set_port_current_vlan",
184 dest = "set_port_current_vlan",
185 action = "store",
186 type = "int",
187 help = "Set the current VLAN assignment for a port",
188 nargs = 2,
189 metavar = "<port_id> <vlan_id>")
190port_group.add_option("--get_port_current_vlan",
191 dest = "get_port_current_vlan",
192 action = "store",
193 type = "int",
194 help = "Get the current VLAN assignment for a port",
195 nargs = 1,
196 metavar = "<port_id>")
197port_group.add_option("--set_port_base_vlan",
198 dest = "set_port_base_vlan",
199 action = "store",
200 type = "int",
201 help = "Set the base VLAN assignment for a port",
202 nargs = 2,
203 metavar = "<port_id> <vlan_id>")
204port_group.add_option("--get_port_base_vlan",
205 dest = "get_port_base_vlan",
206 action = "store",
207 type = "int",
208 help = "Get the base VLAN assignment for a port",
209 nargs = 1,
210 metavar = "<port_id>")
211parser.add_option_group(port_group)
212
213# VLAN commands
214vlan_group = optparse.OptionGroup(parser, "VLAN commands")
215vlan_group.add_option("--list_all_vlans",
216 dest = "list_all_vlans",
217 action = "store_true",
218 default = False,
219 help = "List all the existing vlans in the system")
220vlan_group.add_option("--create_vlan",
221 dest = "create_vlan",
222 action = "store",
223 type = "string",
224 help = "Add a new vlan to the system",
225 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000226 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000227vlan_group.add_option("--delete_vlan",
228 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000229 action = "store",
230 type = "int",
231 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000232 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000233 nargs = 1,
234 metavar = "<vlan_id>")
235vlan_group.add_option("--show_vlan",
236 dest = "show_vlan",
237 action = "store",
238 type = "int",
239 help = "Show the details of an existing vlan in 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>")
243parser.add_option_group(vlan_group)
244
245(opts, args) = parser.parse_args()
246
Steve McIntyre844bfd42014-11-27 16:58:31 +0000247if opts.list_all_switches:
248 result = db.all_switches()
249 count = 0;
250 for line in result:
251 print line
252 count += 1
253 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000254elif opts.list_all_ports:
255 result = db.all_ports()
256 count = 0;
257 for line in result:
258 print line
259 count += 1
260 print '%d entries' % count
261elif opts.list_all_vlans:
262 result = db.all_vlans()
263 count = 0;
264 for line in result:
265 print line
266 count += 1
267 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000268elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000269 try:
270 switch_id = db.create_switch(opts.create_switch)
271 print 'Created switch_id %d' % switch_id
272 except InputError as inst:
273 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000274elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000275 try:
276 port_id = db.create_port(opts.create_port[0],
277 opts.create_port[1],
278 default_vlan_id, default_vlan_id)
279 print 'Created port_id %d' % port_id
280 except InputError as inst:
281 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000282elif opts.create_vlan is not None:
283 is_base = False
284 if opts.create_vlan[2] in ('1', 'y', 'Y'):
285 is_base = True
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000286 try:
287 vlan_id = db.create_vlan(opts.create_vlan[0],
288 opts.create_vlan[1],
289 is_base)
290 print 'Created vlan_id %d' % vlan_id
291 except InputError as inst:
292 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000293elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000294 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000295 switch_id = db.delete_switch(opts.delete_switch)
Steve McIntyre64e38862014-12-02 17:19:37 +0000296 print 'Deleted switch_id %d' % switch_id
297 except InputError as inst:
298 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000299elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000300 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000301 port_id = db.delete_port(opts.delete_port)
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000302 print 'Deleted port_id %d' % port_id
303 except InputError as inst:
304 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000305elif opts.delete_vlan is not None:
306 try:
307 vlan_id = db.delete_vlan(opts.delete_vlan)
308 print 'Deleted vlan_id %d' % vlan_id
309 except InputError as inst:
310 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000311elif opts.lookup_switch_by_name is not None:
312 try:
313 switch_id = db.get_switch_id_by_name(opts.lookup_switch_by_name)
314 if switch_id is not None:
315 print 'Switch %s has switch_id %d' % (opts.lookup_switch_by_name, switch_id)
316 else:
317 print 'No switch found for name %s' % opts.lookup_switch_by_name
318 except InputError as inst:
319 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000320elif opts.show_switch is not None:
321 try:
322 switch = db.get_switch_by_id(opts.show_switch)
323 if switch is not None:
324 dump_switch(switch)
325 else:
326 print 'No switch found for switch_id %d' % opts.show_switch
327 except InputError as inst:
328 print 'Failed: %s' % inst
Steve McIntyre11e4cbd2014-12-05 16:03:03 +0000329elif opts.list_switch_ports is not None:
330 try:
331 ports = db.get_ports_by_switch(opts.list_switch_ports)
332 if ports is not None:
333 for port in ports:
334 dump_port(port)
335 else:
336 print 'No ports found for switch_id %d' % opts.list_switch_ports
337 except InputError as inst:
338 print 'Failed: %s' % inst
Steve McIntyre08dd8392014-12-05 16:07:20 +0000339elif opts.show_port is not None:
340 try:
341 port = db.get_port_by_id(opts.show_port)
342 if port is not None:
343 dump_port(port)
344 else:
345 print 'No port found for port_id %d' % opts.show_port
346 except InputError as inst:
347 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000348else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000349 print 'No recognised command given. Try -h for help'
Steve McIntyre844bfd42014-11-27 16:58:31 +0000350