blob: 22b196e4d99f458bbc7609df00609e83b4b924a2 [file] [log] [blame]
#! /usr/bin/python
# Copyright 2014 Linaro Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# VLANd admin interface
#
import os, sys, types
import time
import logging
import optparse
from errors import CriticalError, InputError
vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
sys.path.insert(0, vlandpath)
import drivers
from db.db import VlanDB
version = "0.0.0-DEV"
banner = "Linaro VLANd admin interface, version %s" % version
#print '%s' % banner
#print 'Connecting to DB...'
db = VlanDB()
# For sanity, we need to know the vlan_id for the default vlan (tag
# 1). Make sure we know that before anybody attempts to create things
# that depend on it.
default_vlan_tag = 1
default_vlan_id = db.get_vlan_id_from_tag(default_vlan_tag)
if default_vlan_id is None:
# It doesn't exist - create it and try again
default_vlan_id = db.create_vlan("DEFAULT",
default_vlan_tag,
True)
switches = db.all_switches()
print 'The DB knows about %d switch(es)' % len(switches)
if len(switches) > 0:
print switches
ports = db.all_ports()
print 'The DB knows about %d port(s)' % len(ports)
if len(ports) > 0:
print ports
vlans = db.all_vlans()
print 'The DB knows about %d vlan(s)' % len(vlans)
if len(vlans) > 0:
print vlans
print 'The default vlan tag (%d) is vlan_id %d' % (default_vlan_tag, default_vlan_id)
usage = 'Usage: %prog --command [command options]'
commands = ['switch_create',
'switch_destroy',
'switch_details']
parser = optparse.OptionParser(usage=usage, description=banner)
# Switch commands
switch_group = optparse.OptionGroup(parser, "Switch commands")
switch_group.add_option("--list_all_switches",
dest = "list_all_switches",
action = "store_true",
default = False,
help = "List all the existing switches in the system")
switch_group.add_option("--create_switch",
dest = "create_switch",
action = "store",
type = "string",
help = "Add a new switch to the system",
nargs = 1,
metavar = "<name>")
switch_group.add_option("--remove_switch",
dest = "remove_switch",
action = "store",
type = "int",
help = "Remove an existing switch from the system",
default = None,
nargs = 1,
metavar = "<switch_id>")
switch_group.add_option("--show_switch",
dest = "show_switch",
action = "store",
type = "int",
help = "Show the details of an existing switch in the system",
default = None,
nargs = 1,
metavar = "<switch_id>")
switch_group.add_option("--lookup_switch_by_name",
dest = "lookup_switch_by_name",
action = "store",
type = "string",
help = "Lookup a switch ID by name",
nargs = 1,
metavar = "<name>")
switch_group.add_option("--list_switch_ports",
dest = "list_switch_ports",
action = "store",
type = "int",
help = "List the ports of an existing switch in the system",
default = None,
nargs = 1,
metavar = "<switch_id>")
parser.add_option_group(switch_group)
# Port commands
port_group = optparse.OptionGroup(parser, "Port commands")
port_group.add_option("--list_all_ports",
dest = "list_all_ports",
action = "store_true",
default = False,
help = "List all the existing ports in the system")
port_group.add_option("--create_port",
dest = "create_port",
action = "store",
type = "string",
help = "Add a new port to the system",
nargs = 2,
metavar = "<switch_id> <name>")
port_group.add_option("--remove_port",
dest = "remove_port",
action = "store",
type = "int",
help = "Remove an existing port from the system",
default = None,
nargs = 1,
metavar = "<port_id>")
port_group.add_option("--show_port",
dest = "show_port",
action = "store",
type = "int",
help = "Show the details of an existing port in the system",
default = None,
nargs = 1,
metavar = "<port_id>")
port_group.add_option("--lookup_port_by_switch_and_name",
dest = "lookup_port_by_switch_and_name",
action = "store",
type = "string",
help = "Lookup a port ID by switch and port name",
nargs = 2,
metavar = "<switch_id> <name>")
port_group.add_option("--set_port_type",
dest = "set_port_type",
action = "store",
type = "string",
help = "Set the mode of a port to 'trunk' or 'access'",
nargs = 2,
metavar = "<port_id> <mode>")
port_group.add_option("--lock_port",
dest = "lock_port",
action = "store",
type = "string",
help = "Lock the settings on a port",
nargs = 1,
metavar = "<port_id>")
port_group.add_option("--unlock_port",
dest = "unlock_port",
action = "store",
type = "string",
help = "Unock the settings on a port",
nargs = 1,
metavar = "<port_id>")
port_group.add_option("--set_port_current_vlan",
dest = "set_port_current_vlan",
action = "store",
type = "int",
help = "Set the current VLAN assignment for a port",
nargs = 2,
metavar = "<port_id> <vlan_id>")
port_group.add_option("--get_port_current_vlan",
dest = "get_port_current_vlan",
action = "store",
type = "int",
help = "Get the current VLAN assignment for a port",
nargs = 1,
metavar = "<port_id>")
port_group.add_option("--set_port_base_vlan",
dest = "set_port_base_vlan",
action = "store",
type = "int",
help = "Set the base VLAN assignment for a port",
nargs = 2,
metavar = "<port_id> <vlan_id>")
port_group.add_option("--get_port_base_vlan",
dest = "get_port_base_vlan",
action = "store",
type = "int",
help = "Get the base VLAN assignment for a port",
nargs = 1,
metavar = "<port_id>")
parser.add_option_group(port_group)
# VLAN commands
vlan_group = optparse.OptionGroup(parser, "VLAN commands")
vlan_group.add_option("--list_all_vlans",
dest = "list_all_vlans",
action = "store_true",
default = False,
help = "List all the existing vlans in the system")
vlan_group.add_option("--create_vlan",
dest = "create_vlan",
action = "store",
type = "string",
help = "Add a new vlan to the system",
nargs = 3,
metavar = "<name> <tag> <is_base_vlan>")
vlan_group.add_option("--remove_vlan",
dest = "remove_vlan",
action = "store",
type = "int",
help = "Remove an existing vlan from the system",
default = None,
nargs = 1,
metavar = "<vlan_id>")
vlan_group.add_option("--show_vlan",
dest = "show_vlan",
action = "store",
type = "int",
help = "Show the details of an existing vlan in the system",
default = None,
nargs = 1,
metavar = "<vlan_id>")
parser.add_option_group(vlan_group)
(opts, args) = parser.parse_args()
print opts
print args
if opts.list_all_switches:
result = db.all_switches()
count = 0;
for line in result:
print line
count += 1
print '%d entries' % count
elif opts.list_all_ports:
result = db.all_ports()
count = 0;
for line in result:
print line
count += 1
print '%d entries' % count
elif opts.list_all_vlans:
result = db.all_vlans()
count = 0;
for line in result:
print line
count += 1
print '%d entries' % count
elif opts.create_switch is not None:
try:
switch_id = db.create_switch(opts.create_switch)
print 'Created switch_id %d' % switch_id
except InputError as inst:
print 'Failed: %s' % inst
elif opts.create_port is not None:
try:
port_id = db.create_port(opts.create_port[0],
opts.create_port[1],
default_vlan_id, default_vlan_id)
print 'Created port_id %d' % port_id
except InputError as inst:
print 'Failed: %s' % inst
elif opts.create_vlan is not None:
is_base = False
if opts.create_vlan[2] in ('1', 'y', 'Y'):
is_base = True
try:
vlan_id = db.create_vlan(opts.create_vlan[0],
opts.create_vlan[1],
is_base)
print 'Created vlan_id %d' % vlan_id
except InputError as inst:
print 'Failed: %s' % inst
elif opts.remove_switch is not None:
try:
switch_id = db.delete_switch(opts.remove_switch)
print 'Deleted switch_id %d' % switch_id
except InputError as inst:
print 'Failed: %s' % inst
elif opts.remove_port is not None:
try:
port_id = db.delete_port(opts.remove_port)
print 'Deleted port_id %d' % port_id
except InputError as inst:
print 'Failed: %s' % inst
else:
print 'No recognised command give. Try -h for help'