blob: 494d7ea3ffc4dcd7eac6137113b50d8a932782d4 [file] [log] [blame]
Steve McIntyree5043dd2014-12-10 16:49:28 +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# Main VLANd module
21#
22
23import os, sys, types
24import time
25import logging
26
27vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
28sys.path.insert(0, vlandpath)
29
30import drivers
31from config.config import VlanConfig
32from db.db import VlanDB
33from errors import CriticalError, InputError, ConfigError
34
35version = "0.0.0-DEV"
36banner = "Linaro VLANd version %s" % version
37
38def get_switch_driver(switch):
39 logging.debug("Trying to find a driver for %s" % switch)
40 driver = config.switches[switch].driver
41 logging.debug("Driver: %s" % driver)
42 module = __import__("drivers.%s" % driver, fromlist=[driver])
43 class_ = getattr(module, driver)
44 return class_(switch)
45
46print '%s' % banner
47
48print 'Parsing Config...'
49config = VlanConfig(filenames=('./vland.cfg',))
50print ' Config knows about %d switches' % len(config.switches)
51
52print 'Connecting to DB...'
53db = VlanDB(db_name=config.database.dbname, username=config.database.username)
54
55switches = db.all_switches()
56print ' DB knows about %d switches' % len(switches)
57ports = db.all_ports()
58print ' DB knows about %d ports' % len(ports)
59vlans = db.all_vlans()
60print ' DB knows about %d vlans' % len(vlans)
61
Steve McIntyreae286192014-12-12 22:10:43 +000062# Initial startup sanity chacking
Steve McIntyree5043dd2014-12-10 16:49:28 +000063
Steve McIntyreae286192014-12-12 22:10:43 +000064if len(switches) != len(config.switches):
65 print 'You have configured access details for %d switches, ' % len(config.switches)
66 print 'but have %d switches registered in your database.' % len(switches)
67 print 'You must fix this difference for VLANd to work sensibly.'
68 print 'HINT: Running admin.py --auto-import-switch <switch_name>'
69 print 'for each of your switches will probably be useful!'
70 print
Steve McIntyree5043dd2014-12-10 16:49:28 +000071
Steve McIntyre3b330972014-12-12 22:08:29 +000072#for switch in sorted(config.switches):
73# print "Found switch %s:" % (switch)
74#
75# print " Probing:"
76#
77# s = get_switch_driver(switch)
78# s.switch_connect(config.switches[switch].username, config.switches[switch].password)
79# print " Found details of switch:"
80# s._dump_list(s._systemdata)
81# print " Switch has %d ports:" % len(s.switch_get_port_names())
82# for port in s.switch_get_port_names():
83# print " %s" % port
84# if 0 == 1:
85# mode = s.port_get_mode(port)
86# if mode == "trunk":
87# print " port %s is in trunk mode, VLAN(s):" % port
88# vlans = s.port_get_trunk_vlan_list(port)
89# for vlan in vlans:
90# name = s.vlan_get_name(vlan)
91# print " %d (%s)" % (vlan, name)
92# else:
93# vlan = s.port_get_access_vlan(port)
94# name = s.vlan_get_name(vlan)
95# print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name)
96#
97# s.switch_disconnect()
98# del(s)