| #! /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. |
| # |
| # Main VLANd module |
| # |
| |
| import os, sys, types |
| import time |
| import logging |
| |
| vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| sys.path.insert(0, vlandpath) |
| |
| import drivers |
| from config.config import VlanConfig |
| from db.db import VlanDB |
| from ipc.ipc import VlanIpc |
| from errors import CriticalError, InputError, ConfigError, SocketError |
| |
| version = "0.0.0-DEV" |
| banner = "Linaro VLANd version %s" % version |
| |
| def get_switch_driver(switch): |
| logging.debug("Trying to find a driver for %s" % switch) |
| driver = config.switches[switch].driver |
| logging.debug("Driver: %s" % driver) |
| module = __import__("drivers.%s" % driver, fromlist=[driver]) |
| class_ = getattr(module, driver) |
| return class_(switch) |
| |
| print '%s' % banner |
| |
| print 'Parsing Config...' |
| config = VlanConfig(filenames=('./vland.cfg',)) |
| print ' Config knows about %d switches' % len(config.switches) |
| |
| print 'Connecting to DB...' |
| db = VlanDB(db_name=config.database.dbname, username=config.database.username) |
| |
| switches = db.all_switches() |
| print ' DB knows about %d switches' % len(switches) |
| ports = db.all_ports() |
| print ' DB knows about %d ports' % len(ports) |
| vlans = db.all_vlans() |
| print ' DB knows about %d vlans' % len(vlans) |
| |
| # Initial startup sanity chacking |
| |
| # 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_id = db.get_vlan_id_by_tag(config.vland.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", |
| config.vland.default_vlan_tag, |
| True) |
| |
| if len(switches) != len(config.switches): |
| print 'You have configured access details for %d switches, ' % len(config.switches) |
| print 'but have %d switches registered in your database.' % len(switches) |
| print 'You must fix this difference for VLANd to work sensibly.' |
| print 'HINT: Running admin.py --auto-import-switch <switch_name>' |
| print 'for each of your switches will probably be useful!' |
| print |
| |
| #for switch in sorted(config.switches): |
| # print "Found switch %s:" % (switch) |
| # |
| # print " Probing:" |
| # |
| # s = get_switch_driver(switch) |
| # s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| # print " Found details of switch:" |
| # s._dump_list(s._systemdata) |
| # print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| # for port in s.switch_get_port_names(): |
| # print " %s" % port |
| # if 0 == 1: |
| # mode = s.port_get_mode(port) |
| # if mode == "trunk": |
| # print " port %s is in trunk mode, VLAN(s):" % port |
| # vlans = s.port_get_trunk_vlan_list(port) |
| # for vlan in vlans: |
| # name = s.vlan_get_name(vlan) |
| # print " %d (%s)" % (vlan, name) |
| # else: |
| # vlan = s.port_get_access_vlan(port) |
| # name = s.vlan_get_name(vlan) |
| # print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| # |
| # s.switch_disconnect() |
| # del(s) |
| |
| # Now start up the core loop. Listen for command connections and |
| # process them |
| ipc = VlanIpc() |
| ipc.server_init('localhost', config.vland.port) |
| while True: |
| ipc.server_listen() |
| json_data = ipc.server_recv() |
| |
| print "client sent us:" |
| print json_data |
| |
| response = {'response': 'ack'} |
| print "sending reply:" |
| print response |
| |
| ipc.server_reply(response) |
| |