blob: 4001040c091d7de004dd8970f0b5566fd7709cea [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 McIntyrec12f6312014-12-15 15:00:12 +000065# 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.
68default_vlan_id = db.get_vlan_id_by_tag(config.vland.default_vlan_tag)
69if 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 McIntyreae286192014-12-12 22:10:43 +000075if len(switches) != len(config.switches):
Steve McIntyre96855db2014-12-15 16:52:00 +000076 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 McIntyreae286192014-12-12 22:10:43 +000078 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 McIntyree5043dd2014-12-10 16:49:28 +000082
Steve McIntyre3b330972014-12-12 22:08:29 +000083#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 McIntyre6c44fb22014-12-12 22:11:45 +0000110
111# Now start up the core loop. Listen for command connections and
112# process them
113ipc = VlanIpc()
114ipc.server_init('localhost', config.vland.port)
115while 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