Commit 073225ab9fbc142d44a8944f9aba7f3cb8aa3e2f

Authored by zbeacon
1 parent 8a8695f2

Working version with provision data only in device profile

@@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.DeviceProfileType; @@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.DeviceProfileType;
31 import org.thingsboard.server.common.data.audit.ActionType; 31 import org.thingsboard.server.common.data.audit.ActionType;
32 import org.thingsboard.server.common.data.device.data.ProvisionDeviceConfiguration; 32 import org.thingsboard.server.common.data.device.data.ProvisionDeviceConfiguration;
33 import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration; 33 import org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileConfiguration;
  34 +import org.thingsboard.server.common.data.device.profile.ProvisionRequestValidationStrategy;
34 import org.thingsboard.server.common.data.device.profile.ProvisionRequestValidationStrategyType; 35 import org.thingsboard.server.common.data.device.profile.ProvisionRequestValidationStrategyType;
35 import org.thingsboard.server.common.data.id.CustomerId; 36 import org.thingsboard.server.common.data.id.CustomerId;
36 import org.thingsboard.server.common.data.id.TenantId; 37 import org.thingsboard.server.common.data.id.TenantId;
@@ -118,75 +119,37 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { @@ -118,75 +119,37 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService {
118 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); 119 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
119 } 120 }
120 121
121 - Device targetDevice = deviceDao.findDeviceByProfileNameAndDeviceDataProvisionConfigurationPair( 122 + DeviceProfile targetProfile = deviceProfileDao.findProfileByProfileNameAndProfileDataProvisionConfigurationPair(
122 provisionRequest.getDeviceType(), 123 provisionRequest.getDeviceType(),
123 provisionRequestKey, 124 provisionRequestKey,
124 - provisionRequestSecret  
125 - ).orElse(null);  
126 -  
127 - if (targetDevice != null) {  
128 - return processProvisionDeviceWithKeySecretPairExists(provisionRequest, provisionRequestKey, provisionRequestSecret, targetDevice);  
129 - } else {  
130 - return processProvisionDeviceWithKeySecretPairNotExists(provisionRequest, provisionRequestKey, provisionRequestSecret);  
131 - }  
132 - }  
133 -  
134 - private ListenableFuture<ProvisionResponse> processProvisionDeviceWithKeySecretPairExists(ProvisionRequest provisionRequest, String provisionRequestKey, String provisionRequestSecret, Device targetDevice) {  
135 - if (targetDevice.getDeviceData().getConfiguration().getType() != DeviceProfileType.PROVISION) {  
136 - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));  
137 - }  
138 -  
139 - DeviceProfile targetProfile = deviceProfileDao.findById(targetDevice.getTenantId(), targetDevice.getDeviceProfileId().getId()); 125 + provisionRequestSecret);
140 126
141 if (targetProfile == null || targetProfile.getProfileData().getConfiguration().getType() != DeviceProfileType.PROVISION) { 127 if (targetProfile == null || targetProfile.getProfileData().getConfiguration().getType() != DeviceProfileType.PROVISION) {
142 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND)); 128 return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));
143 } 129 }
144 130
145 - ProvisionDeviceConfiguration currentDeviceConfiguration = (ProvisionDeviceConfiguration) targetDevice.getDeviceData().getConfiguration();  
146 -  
147 - if (!new ProvisionDeviceConfiguration(provisionRequestKey, provisionRequestSecret).equals(currentDeviceConfiguration)) {  
148 - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));  
149 - }  
150 -  
151 - ProvisionRequestValidationStrategyType targetStrategy = getStrategy(targetProfile);  
152 - switch (targetStrategy) {  
153 - case CHECK_NEW_DEVICE:  
154 - log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());  
155 - notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);  
156 - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));  
157 - case CHECK_PRE_PROVISIONED_DEVICE:  
158 - return processProvision(targetDevice, provisionRequest);  
159 - default:  
160 - throw new RuntimeException("Strategy is not supported - " + targetStrategy.name());  
161 - }  
162 - } 131 + ProvisionRequestValidationStrategyType validationStrategy = getStrategy(targetProfile);
163 132
164 - private ListenableFuture<ProvisionResponse> processProvisionDeviceWithKeySecretPairNotExists(ProvisionRequest provisionRequest, String provisionRequestKey, String provisionRequestSecret){  
165 - DeviceProfile targetProfile = deviceProfileDao.findProfileByProfileNameAndProfileDataProvisionConfigurationPair(  
166 - provisionRequest.getDeviceType(),  
167 - provisionRequestKey,  
168 - provisionRequestSecret  
169 - ); 133 + Device targetDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName()).orElse(null);
170 134
171 - if (targetProfile == null || targetProfile.getProfileData().getConfiguration().getType() != DeviceProfileType.PROVISION) {  
172 - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.NOT_FOUND));  
173 - }  
174 - ProvisionRequestValidationStrategyType targetStrategy = getStrategy(targetProfile);  
175 - switch (targetStrategy) { 135 + switch(validationStrategy) {
176 case CHECK_NEW_DEVICE: 136 case CHECK_NEW_DEVICE:
177 - return createDevice(provisionRequest, targetProfile); 137 + if (targetDevice != null) {
  138 + log.warn("[{}] The device is present and could not be provisioned once more!", targetDevice.getName());
  139 + notify(targetDevice, provisionRequest, DataConstants.PROVISION_FAILURE, false);
  140 + return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
  141 + } else {
  142 + return createDevice(provisionRequest, targetProfile);
  143 + }
178 case CHECK_PRE_PROVISIONED_DEVICE: 144 case CHECK_PRE_PROVISIONED_DEVICE:
179 - ProvisionDeviceProfileConfiguration currentDeviceProfileConfiguration = (ProvisionDeviceProfileConfiguration) targetProfile.getProfileData().getConfiguration();  
180 - if(new ProvisionDeviceProfileConfiguration(provisionRequestKey, provisionRequestSecret).equals(currentDeviceProfileConfiguration)) {  
181 - Optional<Device> optionalDevice = deviceDao.findDeviceByTenantIdAndName(targetProfile.getTenantId().getId(), provisionRequest.getDeviceName());  
182 - if (optionalDevice.isPresent()) {  
183 - return processProvision(optionalDevice.get(), provisionRequest);  
184 - } 145 + if (targetDevice != null){
  146 + return processProvision(targetDevice, provisionRequest);
  147 + } else {
  148 + log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());
  149 + return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));
185 } 150 }
186 - log.warn("[{}] Failed to find pre provisioned device!", provisionRequest.getDeviceName());  
187 - return Futures.immediateFuture(new ProvisionResponse(null, ProvisionResponseStatus.FAILURE));  
188 default: 151 default:
189 - throw new RuntimeException("Strategy is not supported - " + targetStrategy.name()); 152 + throw new RuntimeException("Strategy is not supported - " + validationStrategy.name());
190 } 153 }
191 } 154 }
192 155