blob: c74324ccf8eff2d4d8430650d3fa6ae4b339509e [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
Steve McIntyre6c44fb22014-12-12 22:11:45 +000033from ipc.ipc import VlanIpc
34from errors import CriticalError, InputError, ConfigError, SocketError
Steve McIntyree5043dd2014-12-10 16:49:28 +000035
36version = "0.0.0-DEV"
37banner = "Linaro VLANd version %s" % version
38
39def 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
47print '%s' % banner
48
49print 'Parsing Config...'
50config = VlanConfig(filenames=('./vland.cfg',))
51print ' Config knows about %d switches' % len(config.switches)
52
53print 'Connecting to DB...'
54db = VlanDB(db_name=config.database.dbname, username=config.database.username)
55
56switches = db.all_switches()
57print ' DB knows about %d switches' % len(switches)
58ports = db.all_ports()
59print ' DB knows about %d ports' % len(ports)
60vlans = db.all_vlans()
61print ' DB knows about %d vlans' % len(vlans)
62
Steve McIntyreae286192014-12-12 22:10:43 +000063# Initial startup sanity chacking
Steve McIntyree5043dd2014-12-10 16:49:28 +000064
Steve McIntyreae286192014-12-12 22:10:43 +000065if len(switches) != len(config.switches):
66 print 'You have configured access details for %d switches, ' % len(config.switches)
67 print 'but have %d switches registered in your database.' % len(switches)
68 print 'You must fix this difference for VLANd to work sensibly.'
69 print 'HINT: Running admin.py --auto-import-switch <switch_name>'
70 print 'for each of your switches will probably be useful!'
71 print
Steve McIntyree5043dd2014-12-10 16:49:28 +000072
Steve McIntyre3b330972014-12-12 22:08:29 +000073#for switch in sorted(config.switches):
74# print "Found switch %s:" % (switch)
75#
76# print " Probing:"
77#
78# s = get_switch_driver(switch)
79# s.switch_connect(config.switches[switch].username, config.switches[switch].password)
80# print " Found details of switch:"
81# s._dump_list(s._systemdata)
82# print " Switch has %d ports:" % len(s.switch_get_port_names())
83# for port in s.switch_get_port_names():
84# print " %s" % port
85# if 0 == 1:
86# mode = s.port_get_mode(port)
87# if mode == "trunk":
88# print " port %s is in trunk mode, VLAN(s):" % port
89# vlans = s.port_get_trunk_vlan_list(port)
90# for vlan in vlans:
91# name = s.vlan_get_name(vlan)
92# print " %d (%s)" % (vlan, name)
93# else:
94# vlan = s.port_get_access_vlan(port)
95# name = s.vlan_get_name(vlan)
96# print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name)
97#
98# s.switch_disconnect()
99# del(s)
Steve McIntyre6c44fb22014-12-12 22:11:45 +0000100
101# Now start up the core loop. Listen for command connections and
102# process them
103ipc = VlanIpc()
104ipc.server_init('localhost', config.vland.port)
105while True:
106 ipc.server_listen()
107 json_data = ipc.server_recv()
108
109 print "client sent us:"
110 print json_data
111
112 response = {'response': 'ack'}
113 print "sending reply:"
114 print response
115
116 ipc.server_reply(response)
117