Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 1 | #! /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 | |
| 23 | import os, sys, types |
| 24 | import time |
| 25 | import logging |
| 26 | |
| 27 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 28 | sys.path.insert(0, vlandpath) |
| 29 | |
| 30 | import drivers |
| 31 | from config.config import VlanConfig |
| 32 | from db.db import VlanDB |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 33 | from ipc.ipc import VlanIpc |
| 34 | from errors import CriticalError, InputError, ConfigError, SocketError |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 35 | |
| 36 | version = "0.0.0-DEV" |
| 37 | banner = "Linaro VLANd version %s" % version |
| 38 | |
| 39 | def get_switch_driver(switch): |
| 40 | logging.debug("Trying to find a driver for %s" % switch) |
| 41 | driver = config.switches[switch].driver |
| 42 | logging.debug("Driver: %s" % driver) |
| 43 | module = __import__("drivers.%s" % driver, fromlist=[driver]) |
| 44 | class_ = getattr(module, driver) |
| 45 | return class_(switch) |
| 46 | |
| 47 | print '%s' % banner |
| 48 | |
| 49 | print 'Parsing Config...' |
| 50 | config = VlanConfig(filenames=('./vland.cfg',)) |
| 51 | print ' Config knows about %d switches' % len(config.switches) |
| 52 | |
| 53 | print 'Connecting to DB...' |
| 54 | db = VlanDB(db_name=config.database.dbname, username=config.database.username) |
| 55 | |
| 56 | switches = db.all_switches() |
| 57 | print ' DB knows about %d switches' % len(switches) |
| 58 | ports = db.all_ports() |
| 59 | print ' DB knows about %d ports' % len(ports) |
| 60 | vlans = db.all_vlans() |
| 61 | print ' DB knows about %d vlans' % len(vlans) |
| 62 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 63 | # Initial startup sanity chacking |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 64 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 65 | # For sanity, we need to know the vlan_id for the default vlan (tag |
| 66 | # 1). Make sure we know that before anybody attempts to create things |
| 67 | # that depend on it. |
| 68 | default_vlan_id = db.get_vlan_id_by_tag(config.vland.default_vlan_tag) |
| 69 | if default_vlan_id is None: |
| 70 | # It doesn't exist - create it and try again |
| 71 | default_vlan_id = db.create_vlan("DEFAULT", |
| 72 | config.vland.default_vlan_tag, |
| 73 | True) |
| 74 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 75 | if len(switches) != len(config.switches): |
Steve McIntyre | 96855db | 2014-12-15 16:52:00 +0000 | [diff] [blame^] | 76 | print 'You have configured access details for %d switch(es), ' % len(config.switches) |
| 77 | print 'but have %d switch(es) registered in your database.' % len(switches) |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 78 | print 'You must fix this difference for VLANd to work sensibly.' |
| 79 | print 'HINT: Running admin.py --auto-import-switch <switch_name>' |
| 80 | print 'for each of your switches will probably be useful!' |
| 81 | print |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 82 | |
Steve McIntyre | 3b33097 | 2014-12-12 22:08:29 +0000 | [diff] [blame] | 83 | #for switch in sorted(config.switches): |
| 84 | # print "Found switch %s:" % (switch) |
| 85 | # |
| 86 | # print " Probing:" |
| 87 | # |
| 88 | # s = get_switch_driver(switch) |
| 89 | # s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| 90 | # print " Found details of switch:" |
| 91 | # s._dump_list(s._systemdata) |
| 92 | # print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| 93 | # for port in s.switch_get_port_names(): |
| 94 | # print " %s" % port |
| 95 | # if 0 == 1: |
| 96 | # mode = s.port_get_mode(port) |
| 97 | # if mode == "trunk": |
| 98 | # print " port %s is in trunk mode, VLAN(s):" % port |
| 99 | # vlans = s.port_get_trunk_vlan_list(port) |
| 100 | # for vlan in vlans: |
| 101 | # name = s.vlan_get_name(vlan) |
| 102 | # print " %d (%s)" % (vlan, name) |
| 103 | # else: |
| 104 | # vlan = s.port_get_access_vlan(port) |
| 105 | # name = s.vlan_get_name(vlan) |
| 106 | # print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| 107 | # |
| 108 | # s.switch_disconnect() |
| 109 | # del(s) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 110 | |
| 111 | # Now start up the core loop. Listen for command connections and |
| 112 | # process them |
| 113 | ipc = VlanIpc() |
| 114 | ipc.server_init('localhost', config.vland.port) |
| 115 | while True: |
| 116 | ipc.server_listen() |
| 117 | json_data = ipc.server_recv() |
| 118 | |
| 119 | print "client sent us:" |
| 120 | print json_data |
| 121 | |
| 122 | response = {'response': 'ack'} |
| 123 | print "sending reply:" |
| 124 | print response |
| 125 | |
| 126 | ipc.server_reply(response) |
| 127 | |