use of org.eclipse.leshan.core.node.codec.CodecException in project leshan by eclipse.
the class LwM2mNodeTlvDecoder method parseResourceTlv.
private static LwM2mResource parseResourceTlv(Tlv tlv, int objectId, int objectInstanceId, LwM2mModel model) throws CodecException {
LwM2mPath resourcePath = new LwM2mPath(objectId, objectInstanceId, tlv.getIdentifier());
Type expectedType = getResourceType(resourcePath, model);
Integer resourceId = tlv.getIdentifier();
switch(tlv.getType()) {
case MULTIPLE_RESOURCE:
return LwM2mMultipleResource.newResource(resourceId, parseTlvValues(tlv.getChildren(), expectedType, resourcePath), expectedType);
case RESOURCE_VALUE:
return LwM2mSingleResource.newResource(resourceId, parseTlvValue(tlv.getValue(), expectedType, resourcePath), expectedType);
default:
throw new CodecException("Invalid TLV type %s for resource %s", tlv.getType(), resourcePath);
}
}
use of org.eclipse.leshan.core.node.codec.CodecException in project leshan by eclipse.
the class LwM2mNodeJsonDecoder method decode.
@SuppressWarnings("unchecked")
public static <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
try {
String jsonStrValue = content != null ? new String(content) : "";
JsonRootObject json = LwM2mJson.fromJsonLwM2m(jsonStrValue);
List<TimestampedLwM2mNode> timestampedNodes = parseJSON(json, path, model, nodeClass);
if (timestampedNodes.size() == 0) {
return null;
} else {
// return the most recent value
return (T) timestampedNodes.get(0).getNode();
}
} catch (LwM2mJsonException e) {
throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
}
}
use of org.eclipse.leshan.core.node.codec.CodecException 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.codec.CodecException in project leshan by eclipse.
the class LwM2mNodeJsonDecoder method decodeTimestamped.
public static List<TimestampedLwM2mNode> decodeTimestamped(byte[] content, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
try {
String jsonStrValue = new String(content);
JsonRootObject json = LwM2mJson.fromJsonLwM2m(jsonStrValue);
return parseJSON(json, path, model, nodeClass);
} catch (LwM2mJsonException e) {
throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
}
}
use of org.eclipse.leshan.core.node.codec.CodecException in project leshan by eclipse.
the class LwM2mNodeTlvDecoder method parseTlv.
@SuppressWarnings("unchecked")
private static <T extends LwM2mNode> T parseTlv(Tlv[] tlvs, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
LOG.trace("Parsing TLV content for path {}: {}", path, tlvs);
// Object
if (nodeClass == LwM2mObject.class) {
List<LwM2mObjectInstance> instances = new ArrayList<>();
// is it an array of TLV resources?
if (//
tlvs.length > 0 && (tlvs[0].getType() == TlvType.MULTIPLE_RESOURCE || tlvs[0].getType() == TlvType.RESOURCE_VALUE)) {
ObjectModel oModel = model.getObjectModel(path.getObjectId());
if (oModel == null) {
LOG.warn("No model for object {}. The tlv is decoded assuming this is a single instance object", path.getObjectId());
instances.add(parseObjectInstanceTlv(tlvs, path.getObjectId(), 0, model));
} else if (!oModel.multiple) {
instances.add(parseObjectInstanceTlv(tlvs, path.getObjectId(), 0, model));
} else {
throw new CodecException("Object instance TLV is mandatory for multiple instances object [path:%s]", path);
}
} else {
for (Tlv tlv : tlvs) {
if (tlv.getType() != TlvType.OBJECT_INSTANCE)
throw new CodecException("Expected TLV of type OBJECT_INSTANCE but was %s [path:%s]", tlv.getType().name(), path);
instances.add(parseObjectInstanceTlv(tlv.getChildren(), path.getObjectId(), tlv.getIdentifier(), model));
}
}
return (T) new LwM2mObject(path.getObjectId(), instances);
} else // Object instance
if (nodeClass == LwM2mObjectInstance.class) {
if (tlvs.length == 1 && tlvs[0].getType() == TlvType.OBJECT_INSTANCE) {
if (path.isObjectInstance() && tlvs[0].getIdentifier() != path.getObjectInstanceId()) {
throw new CodecException("Id conflict between path [%s] and instance TLV [%d]", path, tlvs[0].getIdentifier());
}
// object instance TLV
return (T) parseObjectInstanceTlv(tlvs[0].getChildren(), path.getObjectId(), tlvs[0].getIdentifier(), model);
} else {
// array of TLV resources
// try to retrieve the instanceId from the path or the model
Integer instanceId = path.getObjectInstanceId();
if (instanceId == null) {
// single instance object?
ObjectModel oModel = model.getObjectModel(path.getObjectId());
if (oModel != null && !oModel.multiple) {
instanceId = 0;
} else {
instanceId = LwM2mObjectInstance.UNDEFINED;
}
}
return (T) parseObjectInstanceTlv(tlvs, path.getObjectId(), instanceId, model);
}
} else // Resource
if (nodeClass == LwM2mResource.class) {
ResourceModel resourceModel = model.getResourceModel(path.getObjectId(), path.getResourceId());
if (tlvs.length == 0 && resourceModel != null && !resourceModel.multiple) {
// else we consider this is a multi-instance resource
throw new CodecException("TLV payload is mandatory for single resource %s", path);
} else if (tlvs.length == 1 && tlvs[0].getType() != TlvType.RESOURCE_INSTANCE) {
if (path.isResource() && path.getResourceId() != tlvs[0].getIdentifier()) {
throw new CodecException("Id conflict between path [%s] and resource TLV [%s]", path, tlvs[0].getIdentifier());
}
return (T) parseResourceTlv(tlvs[0], path.getObjectId(), path.getObjectInstanceId(), model);
} else {
Type expectedRscType = getResourceType(path, model);
return (T) LwM2mMultipleResource.newResource(path.getResourceId(), parseTlvValues(tlvs, expectedRscType, path), expectedRscType);
}
} else {
throw new IllegalArgumentException("invalid node class: " + nodeClass);
}
}
Aggregations