diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java index 78b25c2d42..0aa01246e3 100644 --- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java +++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java @@ -52,7 +52,6 @@ import java.util.List; import java.util.Optional; import java.util.Random; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/secure/LwM2mDefaultBootstrapSessionManager.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/secure/LwM2mDefaultBootstrapSessionManager.java index 6f13b09f56..07a41c668e 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/secure/LwM2mDefaultBootstrapSessionManager.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/secure/LwM2mDefaultBootstrapSessionManager.java @@ -16,12 +16,10 @@ package org.thingsboard.server.transport.lwm2m.bootstrap.secure; import lombok.extern.slf4j.Slf4j; -import org.eclipse.leshan.core.request.BootstrapDiscoverRequest; import org.eclipse.leshan.core.request.BootstrapDownlinkRequest; import org.eclipse.leshan.core.request.BootstrapFinishRequest; import org.eclipse.leshan.core.request.BootstrapRequest; import org.eclipse.leshan.core.request.Identity; -import org.eclipse.leshan.core.response.BootstrapDiscoverResponse; import org.eclipse.leshan.core.response.LwM2mResponse; import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore; import org.eclipse.leshan.server.bootstrap.BootstrapFailureCause; @@ -38,8 +36,9 @@ import org.thingsboard.server.common.transport.TransportService; import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MBootstrapConfigStoreTaskProvider; import org.thingsboard.server.transport.lwm2m.bootstrap.store.LwM2MBootstrapSecurityStore; import org.thingsboard.server.transport.lwm2m.server.client.LwM2MAuthException; - -import java.util.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import static org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_ERROR; import static org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_INFO; diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/store/LwM2MBootstrapConfigStoreTaskProvider.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/store/LwM2MBootstrapConfigStoreTaskProvider.java index bb84f94c59..74ca2ca92a 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/store/LwM2MBootstrapConfigStoreTaskProvider.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/bootstrap/store/LwM2MBootstrapConfigStoreTaskProvider.java @@ -32,6 +32,7 @@ import org.eclipse.leshan.server.bootstrap.BootstrapUtil; import java.net.InetSocketAddress; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.eclipse.leshan.server.bootstrap.BootstrapUtil.toWriteRequest; @@ -163,7 +164,9 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi Map filteredMap = this.serverInstances.entrySet() .stream().filter(x -> !this.securityInstances.containsKey(x.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - this.bootstrapServerIdOld = filteredMap.keySet().stream().findFirst().get(); + if (filteredMap.size() > 0) { + this.bootstrapServerIdOld = filteredMap.keySet().stream().findFirst().get(); + } } public BootstrapConfigStore getStore() { @@ -187,7 +190,7 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi public List> toRequests(BootstrapConfig bootstrapConfig, ContentFormat contentFormat) { List> requests = new ArrayList<>(); - List> requestsDelete = new ArrayList<>(); + Set pathsDelete = new HashSet<>(); List> requestsWrite = new ArrayList<>(); boolean isBsServer = false; boolean isLwServer = false; @@ -209,7 +212,20 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi instances.put(security.serverId, id); isLwServer = true; if (!isBsServer && this.securityInstances.containsKey(security.serverId) && id != this.securityInstances.get(security.serverId)) { - requestsDelete.add(new BootstrapDeleteRequest("/0/" + this.securityInstances.get(security.serverId))); + pathsDelete.add("/0/" + this.securityInstances.get(security.serverId)); + } + /** + * If there is an instance in the serverInstances with serverId which we replace in the securityInstances + */ + // find serverId in securityInstances by id (instance) + Integer serverIdOld = null; + for (Map.Entry entry : this.securityInstances.entrySet()) { + if (entry.getValue().equals(id)) { + serverIdOld = entry.getKey(); + } + } + if (!isBsServer && serverIdOld != null && this.serverInstances.containsKey(serverIdOld)) { + pathsDelete.add("/1/" + this.serverInstances.get(serverIdOld)); } id++; } @@ -218,13 +234,14 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi for (Map.Entry server : bootstrapConfig.servers.entrySet()) { int securityInstanceId = instances.get(server.getValue().shortId); requestsWrite.add(toWriteRequest(securityInstanceId, server.getValue(), contentFormat)); - if (!isLwServer && this.bootstrapServerIdNew != null && server.getValue().shortId == this.bootstrapServerIdNew && - (this.bootstrapServerIdNew != this.bootstrapServerIdOld || securityInstanceId != this.serverInstances.get(this.bootstrapServerIdOld))) { - requestsDelete.add(new BootstrapDeleteRequest("/1/" + this.serverInstances.get(this.bootstrapServerIdOld))); - - } else { - if (!isBsServer && this.serverInstances.containsKey(server.getValue().shortId) && securityInstanceId != this.serverInstances.get(server.getValue().shortId)) { - requestsDelete.add(new BootstrapDeleteRequest("/1/" + this.serverInstances.get(server.getValue().shortId))); + if (!isBsServer) { + /** Delete instance if bootstrapServerIdNew not equals bootstrapServerIdOld or securityInstanceBsIdNew not equals serverInstanceBsIdOld */ + if (this.bootstrapServerIdNew != null && server.getValue().shortId == this.bootstrapServerIdNew && + (this.bootstrapServerIdNew != this.bootstrapServerIdOld || securityInstanceId != this.serverInstances.get(this.bootstrapServerIdOld))) { + pathsDelete.add("/1/" + this.serverInstances.get(this.bootstrapServerIdOld)); + /** Delete instance if serverIdNew is present in serverInstances and securityInstanceIdOld by serverIdNew not equals serverInstanceIdOld */ + } else if (this.serverInstances.containsKey(server.getValue().shortId) && securityInstanceId != this.serverInstances.get(server.getValue().shortId)) { + pathsDelete.add("/1/" + this.serverInstances.get(server.getValue().shortId)); } } } @@ -236,8 +253,8 @@ public class LwM2MBootstrapConfigStoreTaskProvider implements BootstrapTaskProvi if (isBsServer & isLwServer) { requests.add(new BootstrapDeleteRequest("/0")); requests.add(new BootstrapDeleteRequest("/1")); - } else if (requestsDelete.size() > 0) { - requests.addAll(requestsDelete); + } else { + pathsDelete.forEach(pathDelete -> requests.add(new BootstrapDeleteRequest(pathDelete))); } // handle write if (requestsWrite.size() > 0) { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java index a368d74ded..050929c470 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/ota/DefaultLwM2MOtaUpdateService.java @@ -399,7 +399,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl log.debug("[{}] Starting update to [{}{}] using binary", client.getEndpoint(), fwInfo.getTargetName(), fwInfo.getTargetVersion()); startUpdateUsingBinary(client, fwInfo); } - } else { + } else if (fwInfo.updateState != null) { log.debug("[{}] failed to process firmware update: [{}]. Previous update failed.", client.getEndpoint(), fwInfo); logService.log(client, "Failed to process firmware update: " + fwInfo + ". Previous update failed."); } @@ -427,7 +427,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl startUpdateUsingBinary(client, swInfo); } } - } else { + } else if (swInfo.updateState != null) { log.debug("[{}] failed to update client: [{}], previous update failed.", client.getEndpoint(), swInfo); logService.log(client, "Failed to process software update: " + swInfo + ". Previous update failed."); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java index ec8afa77f0..a547a3b039 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceCredentialsServiceImpl.java @@ -47,10 +47,6 @@ import org.thingsboard.server.dao.entity.AbstractEntityService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DeviceCredentialsValidationException; import org.thingsboard.server.dao.service.DataValidator; - -import java.nio.charset.StandardCharsets; -import java.util.Arrays; - import static org.thingsboard.server.common.data.CacheConstants.DEVICE_CREDENTIALS_CACHE; import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.validateString;