blob: 1009b97509d14c9fcc7ba56fc6d9908b584fef07 [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 McIntyref1c04f92014-12-16 18:23:15 +000035from util import VlanUtil
Steve McIntyree5043dd2014-12-10 16:49:28 +000036
37version = "0.0.0-DEV"
38banner = "Linaro VLANd version %s" % version
39
Steve McIntyree5043dd2014-12-10 16:49:28 +000040print '%s' % banner
41
42print 'Parsing Config...'
43config = VlanConfig(filenames=('./vland.cfg',))
44print ' Config knows about %d switches' % len(config.switches)
45
Steve McIntyref1c04f92014-12-16 18:23:15 +000046util = VlanUtil()
47
Steve McIntyree5043dd2014-12-10 16:49:28 +000048print 'Connecting to DB...'
49db = VlanDB(db_name=config.database.dbname, username=config.database.username)
50
51switches = db.all_switches()
52print ' DB knows about %d switches' % len(switches)
53ports = db.all_ports()
54print ' DB knows about %d ports' % len(ports)
55vlans = db.all_vlans()
56print ' DB knows about %d vlans' % len(vlans)
57
Steve McIntyreae286192014-12-12 22:10:43 +000058# Initial startup sanity chacking
Steve McIntyree5043dd2014-12-10 16:49:28 +000059
Steve McIntyrec12f6312014-12-15 15:00:12 +000060# 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.
63default_vlan_id = db.get_vlan_id_by_tag(config.vland.default_vlan_tag)
64if 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 McIntyreae286192014-12-12 22:10:43 +000070if len(switches) != len(config.switches):
Steve McIntyre96855db2014-12-15 16:52:00 +000071 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 McIntyreae286192014-12-12 22:10:43 +000073 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 McIntyree5043dd2014-12-10 16:49:28 +000077
Steve McIntyre3b330972014-12-12 22:08:29 +000078#for switch in sorted(config.switches):
79# print "Found switch %s:" % (switch)
80#
81# print " Probing:"
82#
Steve McIntyref1c04f92014-12-16 18:23:15 +000083# s = util.get_switch_driver(switch, config)
Steve McIntyre3b330972014-12-12 22:08:29 +000084# 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 McIntyre6c44fb22014-12-12 22:11:45 +0000105
106# Now start up the core loop. Listen for command connections and
107# process them
108ipc = VlanIpc()
109ipc.server_init('localhost', config.vland.port)
110while True:
Steve McIntyref1c04f92014-12-16 18:23:15 +0000111 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 McIntyre6c44fb22014-12-12 22:11:45 +0000122
Steve McIntyref1c04f92014-12-16 18:23:15 +0000123 print "client %s sent us:" % json_data['client_name']
Steve McIntyre6c44fb22014-12-12 22:11:45 +0000124 print json_data
125
Steve McIntyref1c04f92014-12-16 18:23:15 +0000126 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 McIntyre6c44fb22014-12-12 22:11:45 +0000140 print "sending reply:"
141 print response
142
143 ipc.server_reply(response)
144