Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # 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 | |
| 16 | |
A R Karthick | 4b72d4b | 2016-06-15 11:09:17 -0700 | [diff] [blame] | 17 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
A R Karthick | 4b72d4b | 2016-06-15 11:09:17 -0700 | [diff] [blame] | 23 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 4b72d4b | 2016-06-15 11:09:17 -0700 | [diff] [blame] | 25 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
Chetan Gaonker | 41d2e07 | 2016-03-15 16:41:31 -0700 | [diff] [blame] | 32 | import sqlite3 |
| 33 | import sys |
| 34 | |
| 35 | class SubscriberDB: |
A R Karthick | 4b72d4b | 2016-06-15 11:09:17 -0700 | [diff] [blame] | 36 | |
| 37 | default_services = ('TLS', 'DHCP', 'IGMP') |
| 38 | |
| 39 | def __init__(self, db = 'subscriber.db', create = False, services = default_services): |
Chetan Gaonker | 41d2e07 | 2016-03-15 16:41:31 -0700 | [diff] [blame] | 40 | self.db = db |
| 41 | self.con = sqlite3.connect(db) |
| 42 | self.con.row_factory = sqlite3.Row |
| 43 | self.cur = self.con.cursor() |
A R Karthick | 4b72d4b | 2016-06-15 11:09:17 -0700 | [diff] [blame] | 44 | self.services = services |
Chetan Gaonker | 41d2e07 | 2016-03-15 16:41:31 -0700 | [diff] [blame] | 45 | self.create = create |
| 46 | if create == True: |
| 47 | self.cur.execute("DROP TABLE IF EXISTS Subscriber") |
| 48 | self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);") |
| 49 | |
| 50 | def load(self, name, service): |
| 51 | self.cur.execute("INSERT INTO Subscriber(Name, Service) VALUES (?, ?);", (name, service)) |
| 52 | |
| 53 | def commit(self): |
| 54 | self.con.commit() |
| 55 | |
| 56 | def generate(self, num = 100): |
| 57 | #create db if not created |
| 58 | if self.create is False: |
| 59 | self.cur.execute("DROP TABLE IF EXISTS Subscriber") |
| 60 | self.cur.execute("CREATE TABLE Subscriber(Id INTEGER PRIMARY KEY, Name TEXT, Service TEXT);") |
| 61 | self.create = True |
| 62 | service = ' '.join(self.services) |
| 63 | for i in xrange(num): |
| 64 | name = "sub%d" %self.lastrowid() |
| 65 | self.load(name, service) |
| 66 | self.commit() |
| 67 | |
| 68 | def read(self, num = 1000000, debug = False): |
| 69 | self.cur.execute("SELECT * FROM Subscriber LIMIT ?;", (num,)) |
| 70 | rows = self.cur.fetchall() |
| 71 | if debug is True: |
| 72 | for row in rows: |
| 73 | print('Id %d, Name %s, Service %s' %(row['Id'], row['Name'], row['Service'])) |
| 74 | return rows |
| 75 | |
| 76 | def lastrowid(self): |
| 77 | return 0 if self.cur.lastrowid == None else self.cur.lastrowid |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | create = False |
| 81 | if len(sys.argv) > 1: |
| 82 | try: |
| 83 | num_subscribers = int(sys.argv[1]) |
| 84 | except: |
| 85 | num_subscribers = 100 |
| 86 | print('Creating %d subscriber records' %num_subscribers) |
| 87 | create = True |
| 88 | sub = SubscriberDB(create = create) |
| 89 | if create == True: |
| 90 | sub.generate(num_subscribers) |
| 91 | else: |
| 92 | num_subscribers = 10 |
| 93 | subscribers = sub.read(num_subscribers) |
| 94 | for s in subscribers: |
| 95 | print('Name %s, Service %s' %(s['Name'], s['Service'])) |