blob: 9a021a331c5df2d947128fd34218f13b7b815f96 [file] [log] [blame]
Weiweie5640c62015-07-31 01:43:01 -07001
2# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
3#
4# Copyright (C) 2014 Regents of the University of California.
5# Author: Weiwei Liu <summerwing10@gmail.com>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19# A copy of the GNU General Public License is in the file COPYING.
20
21import os.path
22from pyndn import Name
Weiweif3132ed2015-08-05 11:20:49 -070023from device_storage import DeviceStorage
Weiweie5640c62015-07-31 01:43:01 -070024from device_profile import DeviceProfile
25from hmac_key import HMACKey
26
27
28class FillDatabaseForTest(object):
29 def __init__(self, count = None):
30 if count == None:
31 self.count = 1
32 else:
33 self.count = count
34 if not "HOME" in os.environ:
35 home = '.'
36 else:
37 home = os.environ["HOME"]
38
39 dbDirectory = os.path.join(home, '.ndn')
40 self.databaseFilePath = os.path.join(dbDirectory, 'ndnhome-controller.db')
41
42 if os.path.isfile(self.databaseFilePath):
43 os.remove(self.databaseFilePath)
44
Weiweif3132ed2015-08-05 11:20:49 -070045 self.storage = DeviceStorage()
Weiweie5640c62015-07-31 01:43:01 -070046
47 def fillDatabase(self):
48 count = self.count
49 prefixStrBase = 'home/sensor/LED/'
50 serviceProfileNameBase = '/standard/sensor/simple-LED-control/v'
51 commandNameBase = 'turn_on'
52 commandNameBase2 = 'turn_off'
53 for i in range(1, count+1):
54 prefixStr = prefixStrBase + str(i)
55 self.add_a_default_device(prefixStr)
56 name = Name(prefixStr)
57 deviceId = self.storage.getDeviceId(name)
58 serviceProfileName = serviceProfileNameBase + str(i)
59 self.storage.addServiceProfile(deviceId, serviceProfileName)
60
61 commandName = commandNameBase + str(i)
62 commandName2 = commandNameBase2 + str(i)
63 commandToken = self.create_a_default_key(commandName)
64 commandToken2 = self.create_a_default_key(commandName2)
65 self.storage.addCommand(deviceId, commandName, commandToken)
66 self.storage.addCommand(deviceId,commandName2,commandToken2)
67
68 def add_a_default_device(self, prefixStr):
69 name = Name(prefixStr)
70 profile = DeviceProfile(prefix = name)
71 seed = self.create_a_default_key('')
72 configurationToken = self.create_a_default_key()
73 self.storage.addDevice(profile, seed, configurationToken)
74
75 def create_a_default_key(self, keyName = None):
76 keyContent = 'this is key content'
77 seed = HMACKey(0,0, keyContent, keyName)
78 return seed
79
80test = FillDatabaseForTest(8)
81test.fillDatabase()