lwm2m - Bootstrap add factory
This commit is contained in:
		
							parent
							
								
									6584393bbf
								
							
						
					
					
						commit
						8d88baceff
					
				@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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<Integer, Integer> 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<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig,
 | 
			
		||||
                                                                              ContentFormat contentFormat) {
 | 
			
		||||
        List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests = new ArrayList<>();
 | 
			
		||||
        List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requestsDelete = new ArrayList<>();
 | 
			
		||||
        Set<String> pathsDelete = new HashSet<>();
 | 
			
		||||
        List<BootstrapDownlinkRequest<? extends LwM2mResponse>> 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<Integer, Integer> 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<Integer, BootstrapConfig.ServerConfig> 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) {
 | 
			
		||||
 | 
			
		||||
@ -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.");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user