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