Search in sources :

Example 66 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method extractLwM2mResources.

private static Map<Integer, LwM2mResource> extractLwM2mResources(Collection<JsonArrayEntry> jsonArrayEntries, LwM2mPath baseName, LwM2mModel model) throws CodecException {
    if (jsonArrayEntries == null)
        return Collections.emptyMap();
    // Extract LWM2M resources from JSON resource list
    Map<Integer, LwM2mResource> lwM2mResourceMap = new HashMap<>();
    Map<LwM2mPath, Map<Integer, JsonArrayEntry>> multiResourceMap = new HashMap<>();
    for (JsonArrayEntry resourceElt : jsonArrayEntries) {
        // Build resource path
        LwM2mPath nodePath = baseName.append(resourceElt.getName());
        // handle LWM2M resources
        if (nodePath.isResourceInstance()) {
            // Multi-instance resource
            // Store multi-instance resource values in a map
            // we will deal with it later
            LwM2mPath resourcePath = new LwM2mPath(nodePath.getObjectId(), nodePath.getObjectInstanceId(), nodePath.getResourceId());
            Map<Integer, JsonArrayEntry> multiResource = multiResourceMap.get(resourcePath);
            if (multiResource == null) {
                multiResource = new HashMap<>();
                multiResourceMap.put(resourcePath, multiResource);
            }
            multiResource.put(nodePath.getResourceInstanceId(), resourceElt);
        } else if (nodePath.isResource()) {
            // Single resource
            Type expectedType = getResourceType(nodePath, model, resourceElt);
            LwM2mResource res = LwM2mSingleResource.newResource(nodePath.getResourceId(), parseJsonValue(resourceElt.getResourceValue(), expectedType, nodePath), expectedType);
            lwM2mResourceMap.put(nodePath.getResourceId(), res);
        } else {
            throw new CodecException("Invalid path [%s] for resource, it should be a resource or a resource instance path", nodePath);
        }
    }
    // Handle multi-instance resource.
    for (Map.Entry<LwM2mPath, Map<Integer, JsonArrayEntry>> entry : multiResourceMap.entrySet()) {
        LwM2mPath resourcePath = entry.getKey();
        Map<Integer, JsonArrayEntry> jsonEntries = entry.getValue();
        if (jsonEntries != null && !jsonEntries.isEmpty()) {
            Type expectedType = getResourceType(resourcePath, model, jsonEntries.values().iterator().next());
            Map<Integer, Object> values = new HashMap<>();
            for (Entry<Integer, JsonArrayEntry> e : jsonEntries.entrySet()) {
                Integer resourceInstanceId = e.getKey();
                values.put(resourceInstanceId, parseJsonValue(e.getValue().getResourceValue(), expectedType, resourcePath));
            }
            LwM2mResource resource = LwM2mMultipleResource.newResource(resourcePath.getResourceId(), values, expectedType);
            lwM2mResourceMap.put(resourcePath.getResourceId(), resource);
        }
    }
    // If we found nothing, we try to create an empty multi-instance resource
    if (lwM2mResourceMap.isEmpty() && baseName.isResource()) {
        ResourceModel resourceModel = model.getResourceModel(baseName.getObjectId(), baseName.getResourceId());
        // We create it only if this respect the model
        if (resourceModel == null || resourceModel.multiple) {
            Type resourceType = getResourceType(baseName, model, null);
            lwM2mResourceMap.put(baseName.getResourceId(), LwM2mMultipleResource.newResource(baseName.getResourceId(), new HashMap<Integer, Object>(), resourceType));
        }
    }
    return lwM2mResourceMap;
}
Also used : HashMap(java.util.HashMap) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Type(org.eclipse.leshan.core.model.ResourceModel.Type) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) CodecException(org.eclipse.leshan.core.node.codec.CodecException) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 67 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class ObservationServiceTest method observe_twice_cancels_first.

@Test
public void observe_twice_cancels_first() {
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    // check the presence of only one observation.
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(1, observations.size());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 68 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class ObservationServiceTest method cancel_by_observation.

@Test
public void cancel_by_observation() throws UnknownHostException {
    // create some observations
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
    Observation observationToCancel = givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    // check its presence
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(2, observations.size());
    // cancel it
    observationService.cancelObservation(observationToCancel);
    // check its absence
    observations = observationService.getObservations(support.registration);
    Assert.assertEquals(1, observations.size());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 69 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class BootstrapHandler method sendServers.

private void sendServers(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // get next config
        Integer key = toSend.remove(0);
        ServerConfig serverConfig = cfg.servers.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(1, key);
        final LwM2mNode serverInstance = convertToServerInstance(key, serverConfig);
        final BootstrapWriteRequest writeServerRequest = new BootstrapWriteRequest(path, serverInstance, session.getContentFormat());
        send(session, writeServerRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendServers(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.warn(String.format("Error during bootstrap write of server instance %s on %s", serverInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SERVER_FAILED, writeServerRequest);
            }
        });
    } else {
        final BootstrapFinishRequest finishBootstrapRequest = new BootstrapFinishRequest();
        send(session, finishBootstrapRequest, new ResponseCallback<BootstrapFinishResponse>() {

            @Override
            public void onResponse(BootstrapFinishResponse response) {
                LOG.trace("Bootstrap Finished {} return code {}", session.getEndpoint(), response.getCode());
                if (response.isSuccess()) {
                    sessionManager.end(session);
                } else {
                    sessionManager.failed(session, FINISHED_WITH_ERROR, finishBootstrapRequest);
                }
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap finished on %s", session.getEndpoint()), e);
                sessionManager.failed(session, SEND_FINISH_FAILED, finishBootstrapRequest);
            }
        });
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest) ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) BootstrapFinishResponse(org.eclipse.leshan.core.response.BootstrapFinishResponse) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Example 70 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class BootstrapHandler method sendBootstrap.

private void sendBootstrap(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // 1st encode them into a juicy TLV binary
        Integer key = toSend.remove(0);
        ServerSecurity securityConfig = cfg.security.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(0, key);
        final LwM2mNode securityInstance = convertToSecurityInstance(key, securityConfig);
        final BootstrapWriteRequest writeBootstrapRequest = new BootstrapWriteRequest(path, securityInstance, session.getContentFormat());
        send(session, writeBootstrapRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendBootstrap(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap write of security instance %s on %s", securityInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SECURITY_FAILED, writeBootstrapRequest);
            }
        });
    } else {
        // we are done, send the servers
        List<Integer> serversToSend = new ArrayList<>(cfg.servers.keySet());
        sendServers(session, cfg, serversToSend);
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)70 Test (org.junit.Test)51 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 Observation (org.eclipse.leshan.core.observation.Observation)11 ArrayList (java.util.ArrayList)10 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)9 Tlv (org.eclipse.leshan.tlv.Tlv)8 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)7 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)6 CodecException (org.eclipse.leshan.core.node.codec.CodecException)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 HashMap (java.util.HashMap)4 Response (org.eclipse.californium.core.coap.Response)4 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4