blob: e466d0da78f54b89b674049c25009b1440812c7c [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
38#print '%s' % banner
39
40#print 'Connecting to DB...'
41db = VlanDB()
42
Steve McIntyre4b0193d2014-11-28 18:06:12 +000043# For sanity, we need to know the vlan_id for the default vlan (tag
44# 1). Make sure we know that before anybody attempts to create things
45# that depend on it.
46default_vlan_tag = 1
47try:
48 default_vlan_id = db.get_vlan_id_from_tag(default_vlan_tag)
49except:
50 # It doesn't exist - create it and try again
51 default_vlan_id = db.create_vlan("DEFAULT",
52 default_vlan_tag,
53 True)
54
55
Steve McIntyre844bfd42014-11-27 16:58:31 +000056switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000057print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000058if len(switches) > 0:
59 print switches
60
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 +000063if len(ports) > 0:
64 print ports
65
66vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000067print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000068if len(vlans) > 0:
69 print vlans
70
Steve McIntyre4b0193d2014-11-28 18:06:12 +000071print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
72
Steve McIntyre844bfd42014-11-27 16:58:31 +000073usage = 'Usage: %prog --command [command options]'
74commands = ['switch_create',
75 'switch_destroy',
76 'switch_details']
77parser = optparse.OptionParser(usage=usage, description=banner)
78
79# Switch commands
80switch_group = optparse.OptionGroup(parser, "Switch commands")
81switch_group.add_option("--list_all_switches",
82 dest = "list_all_switches",
83 action = "store_true",
84 default = False,
85 help = "List all the existing switches in the system")
86switch_group.add_option("--create_switch",
87 dest = "create_switch",
88 action = "store",
89 type = "string",
90 help = "Add a new switch to the system",
91 nargs = 1,
92 metavar = "<name>")
93switch_group.add_option("--remove_switch",
94 dest = "remove_switch",
95 action = "store",
96 type = "int",
97 help = "Remove an existing switch from the system",
98 default = -1,
99 nargs = 1,
100 metavar = "<switch_id>")
101switch_group.add_option("--show_switch",
102 dest = "show_switch",
103 action = "store",
104 type = "int",
105 help = "Show the details of an existing switch in the system",
106 default = -1,
107 nargs = 1,
108 metavar = "<switch_id>")
109switch_group.add_option("--lookup_switch_by_name",
110 dest = "lookup_switch_by_name",
111 action = "store",
112 type = "string",
113 help = "Lookup a switch ID by name",
114 nargs = 1,
115 metavar = "<name>")
116switch_group.add_option("--list_switch_ports",
117 dest = "list_switch_ports",
118 action = "store",
119 type = "int",
120 help = "List the ports of an existing switch in the system",
121 default = -1,
122 nargs = 1,
123 metavar = "<switch_id>")
124parser.add_option_group(switch_group)
125
126# Port commands
127port_group = optparse.OptionGroup(parser, "Port commands")
128port_group.add_option("--list_all_ports",
129 dest = "list_all_ports",
130 action = "store_true",
131 default = False,
132 help = "List all the existing ports in the system")
133port_group.add_option("--create_port",
134 dest = "create_port",
135 action = "store",
136 type = "string",
137 help = "Add a new port to the system",
138 nargs = 2,
139 metavar = "<switch_id> <name>")
140port_group.add_option("--remove_port",
141 dest = "remove_port",
142 action = "store",
143 type = "int",
144 help = "Remove an existing port from the system",
145 default = -1,
146 nargs = 1,
147 metavar = "<port_id>")
148port_group.add_option("--show_port",
149 dest = "show_port",
150 action = "store",
151 type = "int",
152 help = "Show the details of an existing port in the system",
153 default = -1,
154 nargs = 1,
155 metavar = "<port_id>")
156port_group.add_option("--lookup_port_by_switch_and_name",
157 dest = "lookup_port_by_switch_and_name",
158 action = "store",
159 type = "string",
160 help = "Lookup a port ID by switch and port name",
161 nargs = 2,
162 metavar = "<switch_id> <name>")
163port_group.add_option("--set_port_type",
164 dest = "set_port_type",
165 action = "store",
166 type = "string",
167 help = "Set the mode of a port to 'trunk' or 'access'",
168 nargs = 2,
169 metavar = "<port_id> <mode>")
170port_group.add_option("--lock_port",
171 dest = "lock_port",
172 action = "store",
173 type = "string",
174 help = "Lock the settings on a port",
175 nargs = 1,
176 metavar = "<port_id>")
177port_group.add_option("--unlock_port",
178 dest = "unlock_port",
179 action = "store",
180 type = "string",
181 help = "Unock the settings on a port",
182 nargs = 1,
183 metavar = "<port_id>")
184port_group.add_option("--set_port_current_vlan",
185 dest = "set_port_current_vlan",
186 action = "store",
187 type = "int",
188 help = "Set the current VLAN assignment for a port",
189 nargs = 2,
190 metavar = "<port_id> <vlan_id>")
191port_group.add_option("--get_port_current_vlan",
192 dest = "get_port_current_vlan",
193 action = "store",
194 type = "int",
195 help = "Get the current VLAN assignment for a port",
196 nargs = 1,
197 metavar = "<port_id>")
198port_group.add_option("--set_port_base_vlan",
199 dest = "set_port_base_vlan",
200 action = "store",
201 type = "int",
202 help = "Set the base VLAN assignment for a port",
203 nargs = 2,
204 metavar = "<port_id> <vlan_id>")
205port_group.add_option("--get_port_base_vlan",
206 dest = "get_port_base_vlan",
207 action = "store",
208 type = "int",
209 help = "Get the base VLAN assignment for a port",
210 nargs = 1,
211 metavar = "<port_id>")
212parser.add_option_group(port_group)
213
214# VLAN commands
215vlan_group = optparse.OptionGroup(parser, "VLAN commands")
216vlan_group.add_option("--list_all_vlans",
217 dest = "list_all_vlans",
218 action = "store_true",
219 default = False,
220 help = "List all the existing vlans in the system")
221vlan_group.add_option("--create_vlan",
222 dest = "create_vlan",
223 action = "store",
224 type = "string",
225 help = "Add a new vlan to the system",
226 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000227 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000228vlan_group.add_option("--remove_vlan",
229 dest = "remove_vlan",
230 action = "store",
231 type = "int",
232 help = "Remove an existing vlan from the system",
233 default = -1,
234 nargs = 1,
235 metavar = "<vlan_id>")
236vlan_group.add_option("--show_vlan",
237 dest = "show_vlan",
238 action = "store",
239 type = "int",
240 help = "Show the details of an existing vlan in the system",
241 default = -1,
242 nargs = 1,
243 metavar = "<vlan_id>")
244parser.add_option_group(vlan_group)
245
246(opts, args) = parser.parse_args()
247
248print opts
249print args
250
251if opts.list_all_switches:
252 result = db.all_switches()
253 count = 0;
254 for line in result:
255 print line
256 count += 1
257 print '%d entries' % count
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000258elif opts.list_all_ports:
259 result = db.all_ports()
260 count = 0;
261 for line in result:
262 print line
263 count += 1
264 print '%d entries' % count
265elif opts.list_all_vlans:
266 result = db.all_vlans()
267 count = 0;
268 for line in result:
269 print line
270 count += 1
271 print '%d entries' % count
Steve McIntyre844bfd42014-11-27 16:58:31 +0000272elif opts.create_switch is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000273 try:
274 switch_id = db.create_switch(opts.create_switch)
275 print 'Created switch_id %d' % switch_id
276 except InputError as inst:
277 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000278elif opts.create_port is not None:
Steve McIntyreaf2d5ed2014-12-02 12:42:28 +0000279 try:
280 port_id = db.create_port(opts.create_port[0],
281 opts.create_port[1],
282 default_vlan_id, default_vlan_id)
283 print 'Created port_id %d' % port_id
284 except InputError as inst:
285 print 'Failed: %s' % inst
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000286elif opts.create_vlan is not None:
287 is_base = False
288 if opts.create_vlan[2] in ('1', 'y', 'Y'):
289 is_base = True
Steve McIntyrec8aba4c2014-12-02 12:48:51 +0000290 try:
291 vlan_id = db.create_vlan(opts.create_vlan[0],
292 opts.create_vlan[1],
293 is_base)
294 print 'Created vlan_id %d' % vlan_id
295 except InputError as inst:
296 print 'Failed: %s' % inst
Steve McIntyre844bfd42014-11-27 16:58:31 +0000297else:
298 print 'wuh?'
299