added sqlite3 support at Domino Server for label subscriptions and client registrations 57/15157/1
authorUlas Kozat <ulas.kozat@gmail.com>
Sat, 4 Jun 2016 05:48:03 +0000 (22:48 -0700)
committerUlas Kozat <ulas.kozat@gmail.com>
Sat, 4 Jun 2016 05:48:03 +0000 (22:48 -0700)
Change-Id: Ia3d741adadb20eb92525a4640ad70fa3fdc435a2
Signed-off-by: Ulas Kozat <ulas.kozat@gmail.com>
DominoServer.py
domino_conf.py
lib/thrift/protocol/TBinaryProtocol.pyc [deleted file]
lib/thrift/protocol/TProtocol.pyc [deleted file]
lib/thrift/protocol/__init__.pyc [deleted file]
lib/thrift/server/TServer.pyc [deleted file]
lib/thrift/server/__init__.pyc [deleted file]
lib/thrift/transport/TSocket.pyc [deleted file]
lib/thrift/transport/TTransport.pyc [deleted file]
lib/thrift/transport/__init__.pyc [deleted file]

index 39c4632..f2a22e2 100755 (executable)
@@ -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.'
index f311d43..55f8367 100644 (file)
@@ -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 (file)
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 (file)
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 (file)
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 (file)
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 (file)
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 (file)
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 (file)
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 (file)
index dcbb012..0000000
Binary files a/lib/thrift/transport/__init__.pyc and /dev/null differ