Sea change in how admin code calls the database layer

Add IPC to VLANd

All read-only admin DB query calls are now routed to the core VLANd
process via IPC, and responses come back from there. Part-way to
removing direct DB calls from admin.py, next is the DB calls that make
changes. They're more involved.

Change-Id: I6a993cd4c7b4e85c2a489ac96f9f0f58f087ca1d
diff --git a/vland.py b/vland.py
index 4001040..1009b97 100644
--- a/vland.py
+++ b/vland.py
@@ -32,24 +32,19 @@
 from db.db import VlanDB
 from ipc.ipc import VlanIpc
 from errors import CriticalError, InputError, ConfigError, SocketError
+from util import VlanUtil
 
 version = "0.0.0-DEV"
 banner = "Linaro VLANd version %s" % version
 
-def get_switch_driver(switch):
-    logging.debug("Trying to find a driver for %s" % switch)
-    driver = config.switches[switch].driver
-    logging.debug("Driver: %s" % driver)
-    module = __import__("drivers.%s" % driver, fromlist=[driver])
-    class_ = getattr(module, driver)
-    return class_(switch)
-
 print '%s' % banner
 
 print 'Parsing Config...'
 config = VlanConfig(filenames=('./vland.cfg',))
 print '  Config knows about %d switches' % len(config.switches)
 
+util = VlanUtil()
+
 print 'Connecting to DB...'
 db = VlanDB(db_name=config.database.dbname, username=config.database.username)
 
@@ -85,7 +80,7 @@
 #
 #    print "  Probing:"
 #
-#    s = get_switch_driver(switch)
+#    s = util.get_switch_driver(switch, config)
 #    s.switch_connect(config.switches[switch].username, config.switches[switch].password)
 #    print "  Found details of switch:"
 #    s._dump_list(s._systemdata)
@@ -113,13 +108,35 @@
 ipc = VlanIpc()
 ipc.server_init('localhost', config.vland.port)
 while True:
-    ipc.server_listen()
-    json_data = ipc.server_recv()
+    try:
+        ipc.server_listen()
+        json_data = ipc.server_recv()
+    except SocketError as e:
+        print e
+        print 'Caught IPC error, ignoring'
+        continue
+        pass
+    except:
+        ipc.server_close()
+        raise
 
-    print "client sent us:"
+    print "client %s sent us:" % json_data['client_name']
     print json_data
 
-    response = {'response': 'ack'}
+    response = {}
+
+    if json_data['type'] == 'query':
+        response['type'] = 'queryresponse'
+        try:
+            response['data'] = util.perform_query(db, json_data['command'], json_data['data'])
+            response['response'] = 'ACK'
+        except InputError as e:
+            print e
+            response['response'] = 'ERROR'
+            response['error'] = e.__str__()
+            print 'FOO?'
+            pass
+
     print "sending reply:"
     print response