blob: 6a8dbd155ea0864ecb79f668112732a7113071bc [file] [log] [blame]
A R Karthick4b72d4b2016-06-15 11:09:17 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
A R Karthick4b72d4b2016-06-15 11:09:17 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick4b72d4b2016-06-15 11:09:17 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Chetan Gaonker41d2e072016-03-15 16:41:31 -070016import sqlite3
17import sys
18
19class SubscriberDB:
A R Karthick4b72d4b2016-06-15 11:09:17 -070020
21 default_services = ('TLS', 'DHCP', 'IGMP')
22
23 def __init__(self, db = 'subscriber.db', create = False, services = default_services):
Chetan Gaonker41d2e072016-03-15 16:41:31 -070024 self.db = db
25 self.con = sqlite3.connect(db)
26 self.con.row_factory = sqlite3.Row
27 self.cur = self.con.cursor()
A R Karthick4b72d4b2016-06-15 11:09:17 -070028 self.services = services
Chetan Gaonker41d2e072016-03-15 16:41:31 -070029 self.create = create
30 if create == True:
31 self.cur.execute("DROP TABLE IF EXISTS Subscriber")
32 self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
33
34 def load(self, name, service):
35 self.cur.execute("INSERT INTO Subscriber(Name, Service) VALUES (?, ?);", (name, service))
36
37 def commit(self):
38 self.con.commit()
39
40 def generate(self, num = 100):
41 #create db if not created
42 if self.create is False:
43 self.cur.execute("DROP TABLE IF EXISTS Subscriber")
44 self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);")
45 self.create = True
46 service = ' '.join(self.services)
47 for i in xrange(num):
48 name = "sub%d" %self.lastrowid()
49 self.load(name, service)
50 self.commit()
51
52 def read(self, num = 1000000, debug = False):
53 self.cur.execute("SELECT * FROM Subscriber LIMIT ?;", (num,))
54 rows = self.cur.fetchall()
55 if debug is True:
56 for row in rows:
57 print('Id %d, Name %s, Service %s' %(row['Id'], row['Name'], row['Service']))
58 return rows
59
60 def lastrowid(self):
61 return 0 if self.cur.lastrowid == None else self.cur.lastrowid
62
63if __name__ == "__main__":
64 create = False
65 if len(sys.argv) > 1:
66 try:
67 num_subscribers = int(sys.argv[1])
68 except:
69 num_subscribers = 100
70 print('Creating %d subscriber records' %num_subscribers)
71 create = True
72 sub = SubscriberDB(create = create)
73 if create == True:
74 sub.generate(num_subscribers)
75 else:
76 num_subscribers = 10
77 subscribers = sub.read(num_subscribers)
78 for s in subscribers:
79 print('Name %s, Service %s' %(s['Name'], s['Service']))