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 | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 35 | from util import VlanUtil |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 36 | |
| 37 | version = "0.0.0-DEV" |
| 38 | banner = "Linaro VLANd version %s" % version |
| 39 | |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 40 | print '%s' % banner |
| 41 | |
| 42 | print 'Parsing Config...' |
| 43 | config = VlanConfig(filenames=('./vland.cfg',)) |
| 44 | print ' Config knows about %d switches' % len(config.switches) |
| 45 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 46 | util = VlanUtil() |
| 47 | |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 48 | print 'Connecting to DB...' |
| 49 | db = VlanDB(db_name=config.database.dbname, username=config.database.username) |
| 50 | |
| 51 | switches = db.all_switches() |
| 52 | print ' DB knows about %d switches' % len(switches) |
| 53 | ports = db.all_ports() |
| 54 | print ' DB knows about %d ports' % len(ports) |
| 55 | vlans = db.all_vlans() |
| 56 | print ' DB knows about %d vlans' % len(vlans) |
| 57 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 58 | # Initial startup sanity chacking |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 59 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 60 | # For sanity, we need to know the vlan_id for the default vlan (tag |
| 61 | # 1). Make sure we know that before anybody attempts to create things |
| 62 | # that depend on it. |
| 63 | default_vlan_id = db.get_vlan_id_by_tag(config.vland.default_vlan_tag) |
| 64 | if default_vlan_id is None: |
| 65 | # It doesn't exist - create it and try again |
| 66 | default_vlan_id = db.create_vlan("DEFAULT", |
| 67 | config.vland.default_vlan_tag, |
| 68 | True) |
| 69 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 70 | if len(switches) != len(config.switches): |
Steve McIntyre | 96855db | 2014-12-15 16:52:00 +0000 | [diff] [blame] | 71 | print 'You have configured access details for %d switch(es), ' % len(config.switches) |
| 72 | print 'but have %d switch(es) registered in your database.' % len(switches) |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 73 | print 'You must fix this difference for VLANd to work sensibly.' |
| 74 | print 'HINT: Running admin.py --auto-import-switch <switch_name>' |
| 75 | print 'for each of your switches will probably be useful!' |
| 76 | print |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 77 | |
Steve McIntyre | 3b33097 | 2014-12-12 22:08:29 +0000 | [diff] [blame] | 78 | #for switch in sorted(config.switches): |
| 79 | # print "Found switch %s:" % (switch) |
| 80 | # |
| 81 | # print " Probing:" |
| 82 | # |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 83 | # s = util.get_switch_driver(switch, config) |
Steve McIntyre | 3b33097 | 2014-12-12 22:08:29 +0000 | [diff] [blame] | 84 | # s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| 85 | # print " Found details of switch:" |
| 86 | # s._dump_list(s._systemdata) |
| 87 | # print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| 88 | # for port in s.switch_get_port_names(): |
| 89 | # print " %s" % port |
| 90 | # if 0 == 1: |
| 91 | # mode = s.port_get_mode(port) |
| 92 | # if mode == "trunk": |
| 93 | # print " port %s is in trunk mode, VLAN(s):" % port |
| 94 | # vlans = s.port_get_trunk_vlan_list(port) |
| 95 | # for vlan in vlans: |
| 96 | # name = s.vlan_get_name(vlan) |
| 97 | # print " %d (%s)" % (vlan, name) |
| 98 | # else: |
| 99 | # vlan = s.port_get_access_vlan(port) |
| 100 | # name = s.vlan_get_name(vlan) |
| 101 | # print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| 102 | # |
| 103 | # s.switch_disconnect() |
| 104 | # del(s) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 105 | |
| 106 | # Now start up the core loop. Listen for command connections and |
| 107 | # process them |
| 108 | ipc = VlanIpc() |
| 109 | ipc.server_init('localhost', config.vland.port) |
| 110 | while True: |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 111 | try: |
| 112 | ipc.server_listen() |
| 113 | json_data = ipc.server_recv() |
| 114 | except SocketError as e: |
| 115 | print e |
| 116 | print 'Caught IPC error, ignoring' |
| 117 | continue |
| 118 | pass |
| 119 | except: |
| 120 | ipc.server_close() |
| 121 | raise |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 122 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 123 | print "client %s sent us:" % json_data['client_name'] |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 124 | print json_data |
| 125 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 126 | response = {} |
| 127 | |
| 128 | if json_data['type'] == 'query': |
| 129 | response['type'] = 'queryresponse' |
| 130 | try: |
| 131 | response['data'] = util.perform_query(db, json_data['command'], json_data['data']) |
| 132 | response['response'] = 'ACK' |
| 133 | except InputError as e: |
| 134 | print e |
| 135 | response['response'] = 'ERROR' |
| 136 | response['error'] = e.__str__() |
| 137 | print 'FOO?' |
| 138 | pass |
| 139 | |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 140 | print "sending reply:" |
| 141 | print response |
| 142 | |
| 143 | ipc.server_reply(response) |
| 144 | |