blob: 4b3b513aa15bce771e287099c801da889d65a7b9 [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 McIntyre844bfd42014-11-27 16:58:31 +000041#print '%s' % banner
42
43#print 'Connecting to DB...'
44db = VlanDB()
45
Steve McIntyre4b0193d2014-11-28 18:06:12 +000046# For sanity, we need to know the vlan_id for the default vlan (tag
47# 1). Make sure we know that before anybody attempts to create things
48# that depend on it.
49default_vlan_tag = 1
Steve McIntyre50eb0602014-12-05 15:29:04 +000050default_vlan_id = db.get_vlan_id_by_tag(default_vlan_tag)
Steve McIntyrec99e93b2014-12-02 18:21:33 +000051if default_vlan_id is None:
Steve McIntyre4b0193d2014-11-28 18:06:12 +000052 # It doesn't exist - create it and try again
53 default_vlan_id = db.create_vlan("DEFAULT",
54 default_vlan_tag,
55 True)
56
57
Steve McIntyre844bfd42014-11-27 16:58:31 +000058switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000059print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000060
61ports = db.all_ports()
Steve McIntyreb1db7102014-11-28 17:56:12 +000062print 'The DB knows about %d port(s)' % len(ports)
Steve McIntyre844bfd42014-11-27 16:58:31 +000063
64vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000065print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000066
Steve McIntyre4b0193d2014-11-28 18:06:12 +000067print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
68
Steve McIntyre844bfd42014-11-27 16:58:31 +000069usage = 'Usage: %prog --command [command options]'
70commands = ['switch_create',
71 'switch_destroy',
72 'switch_details']
73parser = optparse.OptionParser(usage=usage, description=banner)
74
75# Switch commands
76switch_group = optparse.OptionGroup(parser, "Switch commands")
77switch_group.add_option("--list_all_switches",
78 dest = "list_all_switches",
79 action = "store_true",
80 default = False,
81 help = "List all the existing switches in the system")
82switch_group.add_option("--create_switch",
83 dest = "create_switch",
84 action = "store",
85 type = "string",
86 help = "Add a new switch to the system",
87 nargs = 1,
88 metavar = "<name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +000089switch_group.add_option("--delete_switch",
90 dest = "delete_switch",
Steve McIntyre844bfd42014-11-27 16:58:31 +000091 action = "store",
92 type = "int",
93 help = "Remove an existing switch from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +000094 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +000095 nargs = 1,
96 metavar = "<switch_id>")
97switch_group.add_option("--show_switch",
98 dest = "show_switch",
99 action = "store",
100 type = "int",
101 help = "Show the details of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000102 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000103 nargs = 1,
104 metavar = "<switch_id>")
105switch_group.add_option("--lookup_switch_by_name",
106 dest = "lookup_switch_by_name",
107 action = "store",
108 type = "string",
109 help = "Lookup a switch ID by name",
110 nargs = 1,
111 metavar = "<name>")
112switch_group.add_option("--list_switch_ports",
113 dest = "list_switch_ports",
114 action = "store",
115 type = "int",
116 help = "List the ports of an existing switch in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000117 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000118 nargs = 1,
119 metavar = "<switch_id>")
120parser.add_option_group(switch_group)
121
122# Port commands
123port_group = optparse.OptionGroup(parser, "Port commands")
124port_group.add_option("--list_all_ports",
125 dest = "list_all_ports",
126 action = "store_true",
127 default = False,
128 help = "List all the existing ports in the system")
129port_group.add_option("--create_port",
130 dest = "create_port",
131 action = "store",
132 type = "string",
133 help = "Add a new port to the system",
134 nargs = 2,
135 metavar = "<switch_id> <name>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000136port_group.add_option("--delete_port",
137 dest = "delete_port",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000138 action = "store",
139 type = "int",
140 help = "Remove an existing port from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000141 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000142 nargs = 1,
143 metavar = "<port_id>")
144port_group.add_option("--show_port",
145 dest = "show_port",
146 action = "store",
147 type = "int",
148 help = "Show the details of an existing port in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000149 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000150 nargs = 1,
151 metavar = "<port_id>")
152port_group.add_option("--lookup_port_by_switch_and_name",
153 dest = "lookup_port_by_switch_and_name",
154 action = "store",
155 type = "string",
156 help = "Lookup a port ID by switch and port name",
157 nargs = 2,
158 metavar = "<switch_id> <name>")
159port_group.add_option("--set_port_type",
160 dest = "set_port_type",
161 action = "store",
162 type = "string",
163 help = "Set the mode of a port to 'trunk' or 'access'",
164 nargs = 2,
165 metavar = "<port_id> <mode>")
166port_group.add_option("--lock_port",
167 dest = "lock_port",
168 action = "store",
169 type = "string",
170 help = "Lock the settings on a port",
171 nargs = 1,
172 metavar = "<port_id>")
173port_group.add_option("--unlock_port",
174 dest = "unlock_port",
175 action = "store",
176 type = "string",
177 help = "Unock the settings on a port",
178 nargs = 1,
179 metavar = "<port_id>")
180port_group.add_option("--set_port_current_vlan",
181 dest = "set_port_current_vlan",
182 action = "store",
183 type = "int",
184 help = "Set the current VLAN assignment for a port",
185 nargs = 2,
186 metavar = "<port_id> <vlan_id>")
187port_group.add_option("--get_port_current_vlan",
188 dest = "get_port_current_vlan",
189 action = "store",
190 type = "int",
191 help = "Get the current VLAN assignment for a port",
192 nargs = 1,
193 metavar = "<port_id>")
194port_group.add_option("--set_port_base_vlan",
195 dest = "set_port_base_vlan",
196 action = "store",
197 type = "int",
198 help = "Set the base VLAN assignment for a port",
199 nargs = 2,
200 metavar = "<port_id> <vlan_id>")
201port_group.add_option("--get_port_base_vlan",
202 dest = "get_port_base_vlan",
203 action = "store",
204 type = "int",
205 help = "Get the base VLAN assignment for a port",
206 nargs = 1,
207 metavar = "<port_id>")
208parser.add_option_group(port_group)
209
210# VLAN commands
211vlan_group = optparse.OptionGroup(parser, "VLAN commands")
212vlan_group.add_option("--list_all_vlans",
213 dest = "list_all_vlans",
214 action = "store_true",
215 default = False,
216 help = "List all the existing vlans in the system")
217vlan_group.add_option("--create_vlan",
218 dest = "create_vlan",
219 action = "store",
220 type = "string",
221 help = "Add a new vlan to the system",
222 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000223 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre99feaee2014-12-02 18:22:36 +0000224vlan_group.add_option("--delete_vlan",
225 dest = "delete_vlan",
Steve McIntyre844bfd42014-11-27 16:58:31 +0000226 action = "store",
227 type = "int",
228 help = "Remove an existing vlan from the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000229 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000230 nargs = 1,
231 metavar = "<vlan_id>")
232vlan_group.add_option("--show_vlan",
233 dest = "show_vlan",
234 action = "store",
235 type = "int",
236 help = "Show the details of an existing vlan in the system",
Steve McIntyre59e04632014-12-02 18:02:16 +0000237 default = None,
Steve McIntyre844bfd42014-11-27 16:58:31 +0000238 nargs = 1,
239 metavar = "<vlan_id>")
240parser.add_option_group(vlan_group)
241
242(opts, args) = parser.parse_args()
243
Steve McIntyre844bfd42014-11-27 16:58:31 +0000244if opts.list_all_switches:
245 result = db.all_switches()
246 count = 0;
247 for line in result:
248 print line
249 count += 1
250 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000251elif opts.list_all_ports:
252 result = db.all_ports()
253 count = 0;
254 for line in result:
255 print line
256 count += 1
257 print '%d entries' % count
258elif opts.list_all_vlans:
259 result = db.all_vlans()
260 count = 0;
261 for line in result:
262 print line
263 count += 1
264 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000265elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000266 try:
267 switch_id = db.create_switch(opts.create_switch)
268 print 'Created switch_id %d' % switch_id
269 except InputError as inst:
270 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000271elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000272 try:
273 port_id = db.create_port(opts.create_port[0],
274 opts.create_port[1],
275 default_vlan_id, default_vlan_id)
276 print 'Created port_id %d' % port_id
277 except InputError as inst:
278 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000279elif opts.create_vlan is not None:
280 is_base = False
281 if opts.create_vlan[2] in ('1', 'y', 'Y'):
282 is_base = True
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000283 try:
284 vlan_id = db.create_vlan(opts.create_vlan[0],
285 opts.create_vlan[1],
286 is_base)
287 print 'Created vlan_id %d' % vlan_id
288 except InputError as inst:
289 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000290elif opts.delete_switch is not None:
Steve McIntyre64e38862014-12-02 17:19:37 +0000291 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000292 switch_id = db.delete_switch(opts.delete_switch)
Steve McIntyre64e38862014-12-02 17:19:37 +0000293 print 'Deleted switch_id %d' % switch_id
294 except InputError as inst:
295 print 'Failed: %s' % inst
Steve McIntyre99feaee2014-12-02 18:22:36 +0000296elif opts.delete_port is not None:
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000297 try:
Steve McIntyre99feaee2014-12-02 18:22:36 +0000298 port_id = db.delete_port(opts.delete_port)
Steve McIntyre6f7ee5c2014-12-02 18:02:50 +0000299 print 'Deleted port_id %d' % port_id
300 except InputError as inst:
301 print 'Failed: %s' % inst
Steve McIntyreed5cbea2014-12-02 18:23:00 +0000302elif opts.delete_vlan is not None:
303 try:
304 vlan_id = db.delete_vlan(opts.delete_vlan)
305 print 'Deleted vlan_id %d' % vlan_id
306 except InputError as inst:
307 print 'Failed: %s' % inst
Steve McIntyre8e839a62014-12-05 15:46:05 +0000308elif opts.lookup_switch_by_name is not None:
309 try:
310 switch_id = db.get_switch_id_by_name(opts.lookup_switch_by_name)
311 if switch_id is not None:
312 print 'Switch %s has switch_id %d' % (opts.lookup_switch_by_name, switch_id)
313 else:
314 print 'No switch found for name %s' % opts.lookup_switch_by_name
315 except InputError as inst:
316 print 'Failed: %s' % inst
Steve McIntyrea132c362014-12-05 15:53:21 +0000317elif opts.show_switch is not None:
318 try:
319 switch = db.get_switch_by_id(opts.show_switch)
320 if switch is not None:
321 dump_switch(switch)
322 else:
323 print 'No switch found for switch_id %d' % opts.show_switch
324 except InputError as inst:
325 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000326else:
Steve McIntyre4a808912014-12-05 15:24:39 +0000327 print 'No recognised command given. Try -h for help'
Steve McIntyre844bfd42014-11-27 16:58:31 +0000328