Commit fa5c86f434f315610ec2500dcfa9445e142cf1a6

Authored by ShvaykaD
Committed by Andrew Shvayka
1 parent c8abaf1a

add-device-entity-to-claim-result (#1907)

* add-device-to-claim-result

* code update

* code-update
@@ -46,6 +46,7 @@ import org.thingsboard.server.common.data.page.TextPageLink; @@ -46,6 +46,7 @@ import org.thingsboard.server.common.data.page.TextPageLink;
46 import org.thingsboard.server.common.data.security.DeviceCredentials; 46 import org.thingsboard.server.common.data.security.DeviceCredentials;
47 import org.thingsboard.server.controller.claim.data.ClaimRequest; 47 import org.thingsboard.server.controller.claim.data.ClaimRequest;
48 import org.thingsboard.server.dao.device.claim.ClaimResponse; 48 import org.thingsboard.server.dao.device.claim.ClaimResponse;
  49 +import org.thingsboard.server.dao.device.claim.ClaimResult;
49 import org.thingsboard.server.dao.exception.IncorrectParameterException; 50 import org.thingsboard.server.dao.exception.IncorrectParameterException;
50 import org.thingsboard.server.dao.model.ModelConstants; 51 import org.thingsboard.server.dao.model.ModelConstants;
51 import org.thingsboard.server.service.security.model.SecurityUser; 52 import org.thingsboard.server.service.security.model.SecurityUser;
@@ -406,19 +407,27 @@ public class DeviceController extends BaseController { @@ -406,19 +407,27 @@ public class DeviceController extends BaseController {
406 device.getId(), device); 407 device.getId(), device);
407 String secretKey = getSecretKey(claimRequest); 408 String secretKey = getSecretKey(claimRequest);
408 409
409 - ListenableFuture<ClaimResponse> future = claimDevicesService.claimDevice(device, customerId, secretKey);  
410 - Futures.addCallback(future, new FutureCallback<ClaimResponse>() { 410 + ListenableFuture<ClaimResult> future = claimDevicesService.claimDevice(device, customerId, secretKey);
  411 + Futures.addCallback(future, new FutureCallback<ClaimResult>() {
411 @Override 412 @Override
412 - public void onSuccess(@Nullable ClaimResponse result) { 413 + public void onSuccess(@Nullable ClaimResult result) {
413 HttpStatus status; 414 HttpStatus status;
414 - if (result.equals(ClaimResponse.SUCCESS)) {  
415 - status = HttpStatus.OK; 415 + if (result != null) {
  416 + if (result.getResponse().equals(ClaimResponse.SUCCESS)) {
  417 + status = HttpStatus.OK;
  418 + deferredResult.setResult(new ResponseEntity<>(result, status));
  419 + } else {
  420 + status = HttpStatus.BAD_REQUEST;
  421 + if (result.getResponse().equals(ClaimResponse.FAILURE)) {
  422 + deferredResult.setResult(new ResponseEntity<>(result.getResponse(), status));
  423 + } else {
  424 + deferredResult.setResult(new ResponseEntity<>(result, status));
  425 + }
  426 + }
416 } else { 427 } else {
417 - status = HttpStatus.BAD_REQUEST; 428 + deferredResult.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
418 } 429 }
419 - deferredResult.setResult(new ResponseEntity<>(result, status));  
420 } 430 }
421 -  
422 @Override 431 @Override
423 public void onFailure(Throwable t) { 432 public void onFailure(Throwable t) {
424 deferredResult.setErrorResult(t); 433 deferredResult.setErrorResult(t);
@@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.Device; @@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.Device;
20 import org.thingsboard.server.common.data.id.CustomerId; 20 import org.thingsboard.server.common.data.id.CustomerId;
21 import org.thingsboard.server.common.data.id.DeviceId; 21 import org.thingsboard.server.common.data.id.DeviceId;
22 import org.thingsboard.server.common.data.id.TenantId; 22 import org.thingsboard.server.common.data.id.TenantId;
23 -import org.thingsboard.server.dao.device.claim.ClaimResponse; 23 +import org.thingsboard.server.dao.device.claim.ClaimResult;
24 24
25 import java.util.List; 25 import java.util.List;
26 26
@@ -28,7 +28,7 @@ public interface ClaimDevicesService { @@ -28,7 +28,7 @@ public interface ClaimDevicesService {
28 28
29 ListenableFuture<Void> registerClaimingInfo(TenantId tenantId, DeviceId deviceId, String secretKey, long durationMs); 29 ListenableFuture<Void> registerClaimingInfo(TenantId tenantId, DeviceId deviceId, String secretKey, long durationMs);
30 30
31 - ListenableFuture<ClaimResponse> claimDevice(Device device, CustomerId customerId, String secretKey); 31 + ListenableFuture<ClaimResult> claimDevice(Device device, CustomerId customerId, String secretKey);
32 32
33 ListenableFuture<List<Void>> reClaimDevice(TenantId tenantId, Device device); 33 ListenableFuture<List<Void>> reClaimDevice(TenantId tenantId, Device device);
34 34
@@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.kv.BooleanDataEntry; @@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.kv.BooleanDataEntry;
34 import org.thingsboard.server.dao.attributes.AttributesService; 34 import org.thingsboard.server.dao.attributes.AttributesService;
35 import org.thingsboard.server.dao.device.claim.ClaimData; 35 import org.thingsboard.server.dao.device.claim.ClaimData;
36 import org.thingsboard.server.dao.device.claim.ClaimResponse; 36 import org.thingsboard.server.dao.device.claim.ClaimResponse;
  37 +import org.thingsboard.server.dao.device.claim.ClaimResult;
37 import org.thingsboard.server.dao.model.ModelConstants; 38 import org.thingsboard.server.dao.model.ModelConstants;
38 39
39 import java.util.Collections; 40 import java.util.Collections;
@@ -95,7 +96,7 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { @@ -95,7 +96,7 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService {
95 } 96 }
96 97
97 @Override 98 @Override
98 - public ListenableFuture<ClaimResponse> claimDevice(Device device, CustomerId customerId, String secretKey) { 99 + public ListenableFuture<ClaimResult> claimDevice(Device device, CustomerId customerId, String secretKey) {
99 List<Object> key = constructCacheKey(device.getId()); 100 List<Object> key = constructCacheKey(device.getId());
100 Cache cache = cacheManager.getCache(CLAIM_DEVICES_CACHE); 101 Cache cache = cacheManager.getCache(CLAIM_DEVICES_CACHE);
101 ClaimData claimData = cache.get(key, ClaimData.class); 102 ClaimData claimData = cache.get(key, ClaimData.class);
@@ -104,18 +105,18 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { @@ -104,18 +105,18 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService {
104 if (currTs > claimData.getExpirationTime() || !secretKey.equals(claimData.getSecretKey())) { 105 if (currTs > claimData.getExpirationTime() || !secretKey.equals(claimData.getSecretKey())) {
105 log.warn("The claiming timeout occurred or wrong 'secretKey' provided for the device [{}]", device.getName()); 106 log.warn("The claiming timeout occurred or wrong 'secretKey' provided for the device [{}]", device.getName());
106 cache.evict(key); 107 cache.evict(key);
107 - return Futures.immediateFuture(ClaimResponse.FAILURE); 108 + return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.FAILURE));
108 } else { 109 } else {
109 if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { 110 if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
110 device.setCustomerId(customerId); 111 device.setCustomerId(customerId);
111 - deviceService.saveDevice(device);  
112 - return Futures.transform(removeClaimingSavedData(cache, key, device), result -> ClaimResponse.SUCCESS); 112 + Device savedDevice = deviceService.saveDevice(device);
  113 + return Futures.transform(removeClaimingSavedData(cache, key, device), result -> new ClaimResult(savedDevice, ClaimResponse.SUCCESS));
113 } 114 }
114 - return Futures.transform(removeClaimingSavedData(cache, key, device), result -> ClaimResponse.CLAIMED); 115 + return Futures.transform(removeClaimingSavedData(cache, key, device), result -> new ClaimResult(device, ClaimResponse.CLAIMED));
115 } 116 }
116 } else { 117 } else {
117 log.warn("Failed to find the device's claiming message![{}]", device.getName()); 118 log.warn("Failed to find the device's claiming message![{}]", device.getName());
118 - return Futures.immediateFuture(ClaimResponse.CLAIMED); 119 + return Futures.immediateFuture(new ClaimResult(device, ClaimResponse.CLAIMED));
119 } 120 }
120 } 121 }
121 122
  1 +/**
  2 + * Copyright © 2016-2019 The Thingsboard Authors
  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 +package org.thingsboard.server.dao.device.claim;
  17 +
  18 +
  19 +import lombok.AllArgsConstructor;
  20 +import lombok.Data;
  21 +import org.thingsboard.server.common.data.Device;
  22 +
  23 +@AllArgsConstructor
  24 +@Data
  25 +public class ClaimResult {
  26 +
  27 + private Device device;
  28 + private ClaimResponse response;
  29 +
  30 +}