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;
}
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());
}
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());
}
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);
}
});
}
}
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);
}
}
Aggregations