From 28f34e2ae729c0e40874ebfc2e60f3854d95f724 Mon Sep 17 00:00:00 2001 From: Ulas Kozat Date: Fri, 3 Jun 2016 22:48:03 -0700 Subject: added sqlite3 support at Domino Server for label subscriptions and client registrations Change-Id: Ia3d741adadb20eb92525a4640ad70fa3fdc435a2 Signed-off-by: Ulas Kozat --- DominoServer.py | 58 +++++++++++++++++++++++++++----- domino_conf.py | 2 +- lib/thrift/protocol/TBinaryProtocol.pyc | Bin 10720 -> 0 bytes lib/thrift/protocol/TProtocol.pyc | Bin 14621 -> 0 bytes lib/thrift/protocol/__init__.pyc | Bin 254 -> 0 bytes lib/thrift/server/TServer.pyc | Bin 8801 -> 0 bytes lib/thrift/server/__init__.pyc | Bin 177 -> 0 bytes lib/thrift/transport/TSocket.pyc | Bin 5822 -> 0 bytes lib/thrift/transport/TTransport.pyc | Bin 17217 -> 0 bytes lib/thrift/transport/__init__.pyc | Bin 213 -> 0 bytes 10 files changed, 50 insertions(+), 10 deletions(-) delete mode 100644 lib/thrift/protocol/TBinaryProtocol.pyc delete mode 100644 lib/thrift/protocol/TProtocol.pyc delete mode 100644 lib/thrift/protocol/__init__.pyc delete mode 100644 lib/thrift/server/TServer.pyc delete mode 100644 lib/thrift/server/__init__.pyc delete mode 100644 lib/thrift/transport/TSocket.pyc delete mode 100644 lib/thrift/transport/TTransport.pyc delete mode 100644 lib/thrift/transport/__init__.pyc diff --git a/DominoServer.py b/DominoServer.py index 39c4632..f2a22e2 100755 --- a/DominoServer.py +++ b/DominoServer.py @@ -14,6 +14,7 @@ import sys, os, glob, random, errno import getopt, socket import logging, json +import sqlite3 #sys.path.append('gen-py') #sys.path.insert(0, glob.glob('./lib/py/build/lib.*')[0]) sys.path.insert(0, glob.glob('./lib')[0]) @@ -131,15 +132,21 @@ class CommunicationHandler: #Logic for a new UDID assignment self.seqno = self.seqno + 1 - - # Store the Domino Client info - # TBD: check the sequence number to ensure the most recent record is saved - self.dominoServer.registration_record[reg_r.domino_udid_assigned] = reg_msg - data = {} - data[reg_r.domino_udid_assigned] = [reg_msg.ipaddr, reg_msg.tcpport, reg_msg.supported_templates, reg_msg.seq_no] - with open(SERVER_DBFILE, 'a') as f: - json.dump(data, f) - f.close() + + #commit to the database + dbconn = sqlite3.connect(SERVER_DBFILE) + c = dbconn.cursor() + try: + newrow = [(reg_r.domino_udid_assigned, reg_msg.ipaddr, reg_msg.tcpport, ','.join(reg_msg.supported_templates), reg_msg.seq_no),] + c.executemany('INSERT INTO clients VALUES (?,?,?,?,?)',newrow) + except sqlite3.OperationalError as ex: + logging.error('Could not add the new registration record into %s for Domino Client %d : %s', SERVER_DBFILE, reg_r.domino_udid_assigned, ex.message) + except: + logging.error('Could not add the new registration record into %s for Domino Client %d', SERVER_DBFILE, reg_r.domino_udid_assigned) + logging.error('Unexpected error: %s', sys.exc_info()[0]) + + dbconn.commit() + dbconn.close() return reg_r @@ -177,6 +184,23 @@ class CommunicationHandler: self.dominoServer.subscribed_labels[sub_msg.domino_udid].difference_update(set(sub_msg.labels)) logging.debug('Supported Template: %s Supported Labels: %s' , self.dominoServer.subscribed_templateformats[sub_msg.domino_udid] , self.dominoServer.subscribed_labels[sub_msg.domino_udid]) + + #commit to the database + dbconn = sqlite3.connect(SERVER_DBFILE) + c = dbconn.cursor() + newlabelset = self.dominoServer.subscribed_labels[sub_msg.domino_udid] + try: + c.execute("REPLACE INTO labels (udid, label_list) VALUES ({udid}, '{newvalue}')".\ + format(udid=sub_msg.domino_udid, newvalue=','.join(list(newlabelset)) )) + except sqlite3.OperationalError as ex1: + logging.error('Could not add the new labels to %s for Domino Client %d : %s', SERVER_DBFILE, sub_msg.domino_udid, ex1.message) + except: + logging.error('Could not add the new labels to %s for Domino Client %d', SERVER_DBFILE, sub_msg.domino_udid) + logging.error('Unexpected error: %s', sys.exc_info()[0]) + + dbconn.commit() + dbconn.close() + #Fill in the details sub_r = SubscribeResponseMessage() @@ -329,6 +353,22 @@ def main(argv): print ex.message sys.exit(2) + #start the database with schemas + dbconn = sqlite3.connect(SERVER_DBFILE) + c = dbconn.cursor() + try: + c.execute('''CREATE TABLE labels (udid INTEGER PRIMARY KEY, label_list TEXT)''') + except sqlite3.OperationalError as ex: + logging.debug('In database file %s, no table is created as %s', SERVER_DBFILE, ex.message) + + try: + c.execute('''CREATE TABLE clients (udid INTEGER PRIMARY KEY, ipaddr TEXT, tcpport INTEGER, templatetypes TEXT, seqno INTEGER)''') + except sqlite3.OperationalError as ex: + logging.debug('In database file %s, no table is created as %s', SERVER_DBFILE, ex.message) + + dbconn.commit() + dbconn.close() + logging.debug('Domino Server Starting...') server.start_communicationService() print 'done.' diff --git a/domino_conf.py b/domino_conf.py index f311d43..55f8367 100644 --- a/domino_conf.py +++ b/domino_conf.py @@ -16,4 +16,4 @@ DEFAULT_TOSCA_PUBFILE = './tosca-templates/tosca_helloworld_nfv.yaml' SERVER_UDID = 0 TOSCADIR = './toscafiles/' TOSCA_DEFAULT_FNAME = 'template1.yaml' -SERVER_DBFILE = 'dominoserver.json' +SERVER_DBFILE = 'dominoserver.db' diff --git a/lib/thrift/protocol/TBinaryProtocol.pyc b/lib/thrift/protocol/TBinaryProtocol.pyc deleted file mode 100644 index 8c89556..0000000 Binary files a/lib/thrift/protocol/TBinaryProtocol.pyc and /dev/null differ diff --git a/lib/thrift/protocol/TProtocol.pyc b/lib/thrift/protocol/TProtocol.pyc deleted file mode 100644 index 8ec8411..0000000 Binary files a/lib/thrift/protocol/TProtocol.pyc and /dev/null differ diff --git a/lib/thrift/protocol/__init__.pyc b/lib/thrift/protocol/__init__.pyc deleted file mode 100644 index a259bbd..0000000 Binary files a/lib/thrift/protocol/__init__.pyc and /dev/null differ diff --git a/lib/thrift/server/TServer.pyc b/lib/thrift/server/TServer.pyc deleted file mode 100644 index 5a9174d..0000000 Binary files a/lib/thrift/server/TServer.pyc and /dev/null differ diff --git a/lib/thrift/server/__init__.pyc b/lib/thrift/server/__init__.pyc deleted file mode 100644 index 7a11045..0000000 Binary files a/lib/thrift/server/__init__.pyc and /dev/null differ diff --git a/lib/thrift/transport/TSocket.pyc b/lib/thrift/transport/TSocket.pyc deleted file mode 100644 index ff141f3..0000000 Binary files a/lib/thrift/transport/TSocket.pyc and /dev/null differ diff --git a/lib/thrift/transport/TTransport.pyc b/lib/thrift/transport/TTransport.pyc deleted file mode 100644 index c7cf4cb..0000000 Binary files a/lib/thrift/transport/TTransport.pyc and /dev/null differ diff --git a/lib/thrift/transport/__init__.pyc b/lib/thrift/transport/__init__.pyc deleted file mode 100644 index dcbb012..0000000 Binary files a/lib/thrift/transport/__init__.pyc and /dev/null differ -- cgit 1.2.3-korg