Search in sources :

Example 1 with BootstrapDownlinkRequest

use of org.eclipse.leshan.core.request.BootstrapDownlinkRequest in project thingsboard by thingsboard.

the class LwM2MBootstrapConfigStoreTaskProvider method toRequests.

public List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig, ContentFormat contentFormat) {
    List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests = new ArrayList<>();
    Set<String> pathsDelete = new HashSet<>();
    List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requestsWrite = new ArrayList<>();
    boolean isBsServer = false;
    boolean isLwServer = false;
    /**
     * Map<serverId, InstanceId>
     */
    Map<Integer, Integer> instances = new HashMap<>();
    // handle security
    int id = 0;
    for (BootstrapConfig.ServerSecurity security : new TreeMap<>(bootstrapConfig.security).values()) {
        if (security.bootstrapServer) {
            requestsWrite.add(toWriteRequest(this.securityInstances.get(0), security, contentFormat));
            isBsServer = true;
            this.bootstrapServerIdNew = security.serverId;
            instances.put(security.serverId, this.securityInstances.get(0));
        } else {
            if (id == this.securityInstances.get(0)) {
                id++;
            }
            requestsWrite.add(toWriteRequest(id, security, contentFormat));
            instances.put(security.serverId, id);
            isLwServer = true;
            if (!isBsServer && this.securityInstances.containsKey(security.serverId) && id != 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++;
        }
    }
    // handle server
    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 (!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));
            }
        }
    }
    // handle acl
    for (Map.Entry<Integer, BootstrapConfig.ACLConfig> acl : bootstrapConfig.acls.entrySet()) {
        requestsWrite.add(toWriteRequest(acl.getKey(), acl.getValue(), contentFormat));
    }
    // handle delete
    if (isBsServer && isLwServer) {
        requests.add(new BootstrapDeleteRequest("/0"));
        requests.add(new BootstrapDeleteRequest("/1"));
    } else {
        pathsDelete.forEach(pathDelete -> requests.add(new BootstrapDeleteRequest(pathDelete)));
    }
    // handle write
    if (requestsWrite.size() > 0) {
        requests.addAll(requestsWrite);
    }
    return (requests);
}
Also used : HashMap(java.util.HashMap) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) ArrayList(java.util.ArrayList) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) BigInteger(java.math.BigInteger) BootstrapDeleteRequest(org.eclipse.leshan.core.request.BootstrapDeleteRequest) BootstrapDownlinkRequest(org.eclipse.leshan.core.request.BootstrapDownlinkRequest) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) HashSet(java.util.HashSet)

Example 2 with BootstrapDownlinkRequest

use of org.eclipse.leshan.core.request.BootstrapDownlinkRequest in project thingsboard by thingsboard.

the class LwM2mDefaultBootstrapSessionManager method nextRequest.

protected BootstrapDownlinkRequest<? extends LwM2mResponse> nextRequest(BootstrapSession bsSession) {
    DefaultBootstrapSession session = (DefaultBootstrapSession) bsSession;
    List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requestsToSend = session.getRequests();
    if (!requestsToSend.isEmpty()) {
        // get next requests
        return requestsToSend.remove(0);
    } else {
        if (session.hasMoreTasks()) {
            BootstrapTaskProvider.Tasks nextTasks = tasksProvider.getTasks(session, session.getResponses());
            if (nextTasks == null) {
                session.setMoreTasks(false);
                return new BootstrapFinishRequest();
            }
            initTasks(session, nextTasks);
            return nextRequest(bsSession);
        } else {
            return new BootstrapFinishRequest();
        }
    }
}
Also used : DefaultBootstrapSession(org.eclipse.leshan.server.bootstrap.DefaultBootstrapSession) BootstrapDownlinkRequest(org.eclipse.leshan.core.request.BootstrapDownlinkRequest) BootstrapTaskProvider(org.eclipse.leshan.server.bootstrap.BootstrapTaskProvider) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse)

Aggregations

BootstrapDownlinkRequest (org.eclipse.leshan.core.request.BootstrapDownlinkRequest)2 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 BootstrapDeleteRequest (org.eclipse.leshan.core.request.BootstrapDeleteRequest)1 BootstrapFinishRequest (org.eclipse.leshan.core.request.BootstrapFinishRequest)1 BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)1 BootstrapTaskProvider (org.eclipse.leshan.server.bootstrap.BootstrapTaskProvider)1 DefaultBootstrapSession (org.eclipse.leshan.server.bootstrap.DefaultBootstrapSession)1