blob: 87f86dc752664e35efb010cd4fac979eaa5396a5 [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
Steve McIntyre231354b2014-12-16 19:22:12 +000037class DaemonState:
38 """ Simple container for stuff to make for nicer syntax """
Steve McIntyree5043dd2014-12-10 16:49:28 +000039
Steve McIntyre231354b2014-12-16 19:22:12 +000040state = DaemonState()
41state.version = "0.0.0-DEV"
42state.banner = "Linaro VLANd version %s" % state.version
43state.starttime = time.time()
44
45print '%s' % state.banner
Steve McIntyree5043dd2014-12-10 16:49:28 +000046
47print 'Parsing Config...'
Steve McIntyre231354b2014-12-16 19:22:12 +000048state.config = VlanConfig(filenames=('./vland.cfg',))
49print ' Config knows about %d switches' % len(state.config.switches)
Steve McIntyree5043dd2014-12-10 16:49:28 +000050
Steve McIntyref1c04f92014-12-16 18:23:15 +000051util = VlanUtil()
52
Steve McIntyree5043dd2014-12-10 16:49:28 +000053print 'Connecting to DB...'
Steve McIntyre283d5f22014-12-17 13:12:04 +000054state.db = VlanDB(db_name=state.config.database.dbname,
55 username=state.config.database.username)
Steve McIntyree5043dd2014-12-10 16:49:28 +000056
Steve McIntyre283d5f22014-12-17 13:12:04 +000057switches = state.db.all_switches()
Steve McIntyree5043dd2014-12-10 16:49:28 +000058print ' DB knows about %d switches' % len(switches)
Steve McIntyre283d5f22014-12-17 13:12:04 +000059ports = state.db.all_ports()
Steve McIntyree5043dd2014-12-10 16:49:28 +000060print ' DB knows about %d ports' % len(ports)
Steve McIntyre283d5f22014-12-17 13:12:04 +000061vlans = state.db.all_vlans()
Steve McIntyree5043dd2014-12-10 16:49:28 +000062print ' DB knows about %d vlans' % len(vlans)
63
Steve McIntyreae286192014-12-12 22:10:43 +000064# Initial startup sanity chacking
Steve McIntyree5043dd2014-12-10 16:49:28 +000065
Steve McIntyrec12f6312014-12-15 15:00:12 +000066# For sanity, we need to know the vlan_id for the default vlan (tag
67# 1). Make sure we know that before anybody attempts to create things
68# that depend on it.
Steve McIntyre283d5f22014-12-17 13:12:04 +000069state.default_vlan_id = state.db.get_vlan_id_by_tag(state.config.vland.default_vlan_tag)
70if state.default_vlan_id is None:
Steve McIntyrec12f6312014-12-15 15:00:12 +000071 # It doesn't exist - create it and try again
Steve McIntyre283d5f22014-12-17 13:12:04 +000072 state.default_vlan_id = state.db.create_vlan("DEFAULT",
73 state.config.vland.default_vlan_tag,
74 True)
Steve McIntyrec12f6312014-12-15 15:00:12 +000075
Steve McIntyre231354b2014-12-16 19:22:12 +000076if len(switches) != len(state.config.switches):
77 print 'You have configured access details for %d switch(es), ' % len(state.config.switches)
Steve McIntyre96855db2014-12-15 16:52:00 +000078 print 'but have %d switch(es) registered in your database.' % len(switches)
Steve McIntyreae286192014-12-12 22:10:43 +000079 print 'You must fix this difference for VLANd to work sensibly.'
80 print 'HINT: Running admin.py --auto-import-switch <switch_name>'
Steve McIntyrebd7ce372014-12-17 16:27:41 +000081 print 'for each of your switches may help!'
Steve McIntyreae286192014-12-12 22:10:43 +000082 print
Steve McIntyree5043dd2014-12-10 16:49:28 +000083
Steve McIntyre6c44fb22014-12-12 22:11:45 +000084# Now start up the core loop. Listen for command connections and
85# process them
86ipc = VlanIpc()
Steve McIntyre231354b2014-12-16 19:22:12 +000087ipc.server_init('localhost', state.config.vland.port)
Steve McIntyre6c44fb22014-12-12 22:11:45 +000088while True:
Steve McIntyref1c04f92014-12-16 18:23:15 +000089 try:
90 ipc.server_listen()
91 json_data = ipc.server_recv()
92 except SocketError as e:
93 print e
94 print 'Caught IPC error, ignoring'
95 continue
96 pass
97 except:
98 ipc.server_close()
99 raise
Steve McIntyre6c44fb22014-12-12 22:11:45 +0000100
Steve McIntyref1c04f92014-12-16 18:23:15 +0000101 print "client %s sent us:" % json_data['client_name']
Steve McIntyre6c44fb22014-12-12 22:11:45 +0000102 print json_data
103
Steve McIntyref1c04f92014-12-16 18:23:15 +0000104 response = {}
105
Steve McIntyre0ebf5832014-12-16 19:23:10 +0000106 # Several types of IPC message here, with potentially different
107 # access control and safety
108
109 # First - simple queries to the database only. Should be safe!
110 if json_data['type'] == 'db_query':
111 response['type'] = 'response'
Steve McIntyref1c04f92014-12-16 18:23:15 +0000112 try:
Steve McIntyre208241a2014-12-17 13:12:26 +0000113 response['data'] = util.perform_db_query(state, json_data['command'], json_data['data'])
Steve McIntyre0ebf5832014-12-16 19:23:10 +0000114 response['response'] = 'ACK'
115 except InputError as e:
116 print e
117 response['response'] = 'ERROR'
118 response['error'] = e.__str__()
119 print 'FOO?'
120 pass
121
122 # Next - simple queries about daemon state only. Should be safe!
123 if json_data['type'] == 'daemon_query':
124 response['type'] = 'response'
125 try:
126 response['data'] = util.perform_daemon_query(state, json_data['command'], json_data['data'])
Steve McIntyref1c04f92014-12-16 18:23:15 +0000127 response['response'] = 'ACK'
128 except InputError as e:
129 print e
130 response['response'] = 'ERROR'
131 response['error'] = e.__str__()
132 print 'FOO?'
133 pass
134
Steve McIntyre995a10c2014-12-17 13:12:58 +0000135 # Next, calls that manipulate objects in the database only
136 # (switches and ports). These are safe and don't need actual
Steve McIntyrec3b659b2014-12-17 16:28:37 +0000137 # co-ordinating with hardware directly.
Steve McIntyre995a10c2014-12-17 13:12:58 +0000138 #
139 # As/when/if we add authentication, use of this function will need
140 # it.
141 if json_data['type'] == 'db_update':
142 response['type'] = 'response'
143 try:
144 response['data'] = util.perform_db_update(state, json_data['command'], json_data['data'])
145 response['response'] = 'ACK'
146 except InputError as e:
147 print e
148 response['response'] = 'ERROR'
149 response['error'] = e.__str__()
150 print 'FOO?'
151 pass
152
Steve McIntyre6c44fb22014-12-12 22:11:45 +0000153 print "sending reply:"
154 print response
155
156 ipc.server_reply(response)
157