blob: 183042e40569c35d29d6b04355ad9b8f0b6284b5 [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
27
28vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
29sys.path.insert(0, vlandpath)
30
31import drivers
32from db.db import VlanDB
33
34version = "0.0.0-DEV"
35banner = "Linaro VLANd admin interface, version %s" % version
36
37#print '%s' % banner
38
39#print 'Connecting to DB...'
40db = VlanDB()
41
42switches = db.all_switches()
Steve McIntyreb1db7102014-11-28 17:56:12 +000043print 'The DB knows about %d switch(es)' % len(switches)
Steve McIntyre844bfd42014-11-27 16:58:31 +000044if len(switches) > 0:
45 print switches
46
47ports = db.all_ports()
Steve McIntyreb1db7102014-11-28 17:56:12 +000048print 'The DB knows about %d port(s)' % len(ports)
Steve McIntyre844bfd42014-11-27 16:58:31 +000049if len(ports) > 0:
50 print ports
51
52vlans = db.all_vlans()
Steve McIntyreb1db7102014-11-28 17:56:12 +000053print 'The DB knows about %d vlan(s)' % len(vlans)
Steve McIntyre844bfd42014-11-27 16:58:31 +000054if len(vlans) > 0:
55 print vlans
56
57usage = 'Usage: %prog --command [command options]'
58commands = ['switch_create',
59 'switch_destroy',
60 'switch_details']
61parser = optparse.OptionParser(usage=usage, description=banner)
62
63# Switch commands
64switch_group = optparse.OptionGroup(parser, "Switch commands")
65switch_group.add_option("--list_all_switches",
66 dest = "list_all_switches",
67 action = "store_true",
68 default = False,
69 help = "List all the existing switches in the system")
70switch_group.add_option("--create_switch",
71 dest = "create_switch",
72 action = "store",
73 type = "string",
74 help = "Add a new switch to the system",
75 nargs = 1,
76 metavar = "<name>")
77switch_group.add_option("--remove_switch",
78 dest = "remove_switch",
79 action = "store",
80 type = "int",
81 help = "Remove an existing switch from the system",
82 default = -1,
83 nargs = 1,
84 metavar = "<switch_id>")
85switch_group.add_option("--show_switch",
86 dest = "show_switch",
87 action = "store",
88 type = "int",
89 help = "Show the details of an existing switch in the system",
90 default = -1,
91 nargs = 1,
92 metavar = "<switch_id>")
93switch_group.add_option("--lookup_switch_by_name",
94 dest = "lookup_switch_by_name",
95 action = "store",
96 type = "string",
97 help = "Lookup a switch ID by name",
98 nargs = 1,
99 metavar = "<name>")
100switch_group.add_option("--list_switch_ports",
101 dest = "list_switch_ports",
102 action = "store",
103 type = "int",
104 help = "List the ports of an existing switch in the system",
105 default = -1,
106 nargs = 1,
107 metavar = "<switch_id>")
108parser.add_option_group(switch_group)
109
110# Port commands
111port_group = optparse.OptionGroup(parser, "Port commands")
112port_group.add_option("--list_all_ports",
113 dest = "list_all_ports",
114 action = "store_true",
115 default = False,
116 help = "List all the existing ports in the system")
117port_group.add_option("--create_port",
118 dest = "create_port",
119 action = "store",
120 type = "string",
121 help = "Add a new port to the system",
122 nargs = 2,
123 metavar = "<switch_id> <name>")
124port_group.add_option("--remove_port",
125 dest = "remove_port",
126 action = "store",
127 type = "int",
128 help = "Remove an existing port from the system",
129 default = -1,
130 nargs = 1,
131 metavar = "<port_id>")
132port_group.add_option("--show_port",
133 dest = "show_port",
134 action = "store",
135 type = "int",
136 help = "Show the details of an existing port in the system",
137 default = -1,
138 nargs = 1,
139 metavar = "<port_id>")
140port_group.add_option("--lookup_port_by_switch_and_name",
141 dest = "lookup_port_by_switch_and_name",
142 action = "store",
143 type = "string",
144 help = "Lookup a port ID by switch and port name",
145 nargs = 2,
146 metavar = "<switch_id> <name>")
147port_group.add_option("--set_port_type",
148 dest = "set_port_type",
149 action = "store",
150 type = "string",
151 help = "Set the mode of a port to 'trunk' or 'access'",
152 nargs = 2,
153 metavar = "<port_id> <mode>")
154port_group.add_option("--lock_port",
155 dest = "lock_port",
156 action = "store",
157 type = "string",
158 help = "Lock the settings on a port",
159 nargs = 1,
160 metavar = "<port_id>")
161port_group.add_option("--unlock_port",
162 dest = "unlock_port",
163 action = "store",
164 type = "string",
165 help = "Unock the settings on a port",
166 nargs = 1,
167 metavar = "<port_id>")
168port_group.add_option("--set_port_current_vlan",
169 dest = "set_port_current_vlan",
170 action = "store",
171 type = "int",
172 help = "Set the current VLAN assignment for a port",
173 nargs = 2,
174 metavar = "<port_id> <vlan_id>")
175port_group.add_option("--get_port_current_vlan",
176 dest = "get_port_current_vlan",
177 action = "store",
178 type = "int",
179 help = "Get the current VLAN assignment for a port",
180 nargs = 1,
181 metavar = "<port_id>")
182port_group.add_option("--set_port_base_vlan",
183 dest = "set_port_base_vlan",
184 action = "store",
185 type = "int",
186 help = "Set the base VLAN assignment for a port",
187 nargs = 2,
188 metavar = "<port_id> <vlan_id>")
189port_group.add_option("--get_port_base_vlan",
190 dest = "get_port_base_vlan",
191 action = "store",
192 type = "int",
193 help = "Get the base VLAN assignment for a port",
194 nargs = 1,
195 metavar = "<port_id>")
196parser.add_option_group(port_group)
197
198# VLAN commands
199vlan_group = optparse.OptionGroup(parser, "VLAN commands")
200vlan_group.add_option("--list_all_vlans",
201 dest = "list_all_vlans",
202 action = "store_true",
203 default = False,
204 help = "List all the existing vlans in the system")
205vlan_group.add_option("--create_vlan",
206 dest = "create_vlan",
207 action = "store",
208 type = "string",
209 help = "Add a new vlan to the system",
210 nargs = 3,
Steve McIntyre9f0bb602014-11-28 14:36:39 +0000211 metavar = "<name> <tag> <is_base_vlan>")
Steve McIntyre844bfd42014-11-27 16:58:31 +0000212vlan_group.add_option("--remove_vlan",
213 dest = "remove_vlan",
214 action = "store",
215 type = "int",
216 help = "Remove an existing vlan from the system",
217 default = -1,
218 nargs = 1,
219 metavar = "<vlan_id>")
220vlan_group.add_option("--show_vlan",
221 dest = "show_vlan",
222 action = "store",
223 type = "int",
224 help = "Show the details of an existing vlan in the system",
225 default = -1,
226 nargs = 1,
227 metavar = "<vlan_id>")
228parser.add_option_group(vlan_group)
229
230(opts, args) = parser.parse_args()
231
232print opts
233print args
234
235if opts.list_all_switches:
236 result = db.all_switches()
237 count = 0;
238 for line in result:
239 print line
240 count += 1
241 print '%d entries' % count
242elif opts.create_switch is not None:
243 switch_id = db.create_switch(opts.create_switch, 'bar', 'baz')
244 print 'Created switch_id %d' % switch_id
245elif opts.create_port is not None:
246 port_id = db.create_port(opts.create_port[0],
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000247 opts.create_port[1],
248 False, False, 1, 1)
Steve McIntyre844bfd42014-11-27 16:58:31 +0000249 print 'Created port_id %d' % port_id
Steve McIntyrec21d8d12014-11-28 14:42:40 +0000250elif opts.create_vlan is not None:
251 is_base = False
252 if opts.create_vlan[2] in ('1', 'y', 'Y'):
253 is_base = True
254 vlan_id = db.create_vlan(opts.create_vlan[0],
255 opts.create_vlan[1],
256 is_base)
Steve McIntyrebe61b632014-11-28 15:14:25 +0000257 print 'Created vlan_id %d' % vlan_id
Steve McIntyre844bfd42014-11-27 16:58:31 +0000258else:
259 print 'wuh?'
260