blob: 1d14c73d9f23860242e939752cfbfd65b2c8fe8c [file] [log] [blame]
Weiweie5640c62015-07-31 01:43:01 -07001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2014-2015 Regents of the University of California.
4# Author: Weiwei Liu <summerwing10@gmail.com>
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18# A copy of the GNU Lesser General Public License is in the file COPYING.
19
20"""
21This module defines the DeviceUserAccessManager class which is the interface of
22operations related to identity, keys, and certificates.
23"""
24
25import sys
26from pyndn.name import Name
27from device_profile import DeviceProfile
Weiweif3132ed2015-08-05 11:20:49 -070028from device_storage import DeviceStorage
Weiweie5640c62015-07-31 01:43:01 -070029
30class DeviceUserAccessManager(object):
31 """
32 Create a new DeviceUserAccessManager
33 """
34 def __init__(self, databaseFilePath = None):
Weiweif3132ed2015-08-05 11:20:49 -070035 deviceUserAccessStorage = DeviceStorage(databaseFilePath)
Weiweie5640c62015-07-31 01:43:01 -070036 self._deviceUserAccessStorage = deviceUserAccessStorage
37
38 def createDevice(self, deviceProfile, seed, configurationToken, commandList = None):
39 """
40 Create a device in the database, including it's corresponding commands and serrvice profiles
41 @param DeviceProfile deviceProfile: the device profile
42 @param SymmetricKey seed: the seed of the device
43 @param SymmetricKey configurationToken: the configuration Token of the device
44 @param list commandList: the command list of the device, must have following structure: each element in the list should be a two-tuple (commandName, commandToken), the commandName should be a string, and commandToken should be a SymmetricKey. Here is an example of a valid commandList : [('turn_on', commandToken1), ('turn_off', commandToken2)]
45 return True if succeed, otherwise False
46 """
47 result = False
48
49 #add device
50 deviceId = self._deviceUserAccessStorage.addDevice(deviceProfile, seed, configurationToken)
51 if deviceId == 0:
52 raise RuntimeError("device already exists in database")
53 return result
54 elif deviceId == -1:
55 raise RuntimeError("error occured during adding device into database")
56 return result
57
58 #add service profile
59 serviceProfileList = deviceProfile.getServiceProfileList()
60 for serviceProfileName in serviceProfileList:
61 serviceProfileId = self._deviceUserAccessStorage.addServiceProfile(deviceId, serviceProfileName)
62 if serviceProfileId == 0:
63 raise RuntimeError("service profile: " + serviceProfileName + " already exists in database")
64 return result
65 elif serviceProfileId == -1:
66 raise RuntimeError("error occured during adding service profile :" + serviceProfileName)
67 return result
68 #add command
69 for commandTuple in commandList:
70 commandId = self._deviceUserAccessStorage.addCommand(deviceId, commandTuple[0], commandTuple[1])
71 if commandId == 0:
72 raise RuntimeError("command: " + commandTuple[0] + " already exists in database")
73 return result
74 elif commandId == -1:
75 raise RuntimeError("error occured during adding command:" +commandTuple[0])
76 return result
77 result = True
78 return result
79
80 def getDeviceProfile(self, prefix):
81 """
82 get device profile of a specified device
83 :param Name prefix: the device prefix
84 :return device profile of the device if exists, otherwise return None
85 :rtype: DeviceProfile
86 """
87 deviceProfile = self._deviceUserAccessStorage.getDeviceProfileFromDevice(prefix)
88 if deviceProfile == None:
89 return deviceProfile
90 else:
91 # get device Id
92 deviceId = self._deviceUserAccessStorage.getDeviceId(deviceProfile.getPrefix())
93 if deviceId == 0:
94 raise RuntimeError("device doesn't exist")
95
96 serviceProfileList = self._deviceUserAccessStorage.getServiceProfilesOfDevice(deviceId)
97 deviceProfile.setServiceProfile(serviceProfileList)
98
99 return deviceProfile
100
101 def getSeed(self, prefix):
102 """
103 get seed of a specified device
104 :param Name prefix: the device prefix
105 :return seed of the device if exists, otherwise return None
106 :rtype: SymmetricKey
107 """
108 return self._deviceUserAccessStorage.getSeed(prefix)
109
110 def getConfigurationToken(self, prefix):
111 """
112 get configuration token of a specified device
113 :param Name prefix: the device prefix
114 :return seed of the device if exists, otherwise return None
115 :rtype: SymmetricKey
116 """
117 return self._deviceUserAccessStorage.getConfigurationToken(prefix)
118
119 def getCommandToken(self, prefix, commandName):
120 """
121 get command token of a specified command
122 :param Name prefix: the device prefix
123 :param str commandName: device name of the command
124 :return command token if command existsm, otherwise None
125 :rtype SymmetricKey
126 """
127 deviceId = self._deviceUserAccessStorage.getDeviceId(prefix)
128 if deviceId == 0:
129 return None
130 return self._deviceUserAccessStorage.getCommandToken(deviceId, commandName)
131
132 def getCommandsOfDevice(self, prefix):
133 """
134 get all the commands of a specified device
135 :param Name prefix: the device prefix
136 :return command name list if any commands exist, otherwise None
137 """
138 deviceId = self._deviceUserAccessStorage.getDeviceId(prefix)
139 if deviceId == 0:
140 return None
141 return self._deviceUserAccessStorage.getCommandsOfDevice(deviceId)
142
143 def getServiceProfilesOfDevice(self, prefix):
144 """
145 get all the service profiles of a specified device
146 :param Name prefix: the device prefix
147 :return service profiles name list if any service profiles exist, otherwise None
148 """
149 deviceId = self._deviceUserAccessStorage.getDeviceId(prefix)
150 if deviceId == 0:
151 return None
152 return self._deviceUserAccessStorage.getServiceProfilesOfDevice(deviceId)
153