public static Type getResourceType(LwM2mPath rscPath, LwM2mModel model) throws CodecException { ResourceModel rscDesc = model.getResourceModel(rscPath.getObjectId(), rscPath.getResourceId()); if (rscDesc == null || rscDesc.type == null) { LOG.trace("unknown type for resource : {}", rscPath); // no resource description... opaque return Type.OPAQUE; } else { return rscDesc.type; } } }
/** * The string representation of the path: /{Object ID}/{ObjectInstance ID}/{Resource ID}/{ResourceInstance ID} */ @Override public String toString() { StringBuilder b = new StringBuilder(); b.append("/"); if (getObjectId() != null) { b.append(getObjectId()); if (getObjectInstanceId() != null) { b.append("/").append(getObjectInstanceId()); if (getResourceId() != null) { b.append("/").append(getResourceId()); if (getResourceInstanceId() != null) { b.append("/").append(getResourceInstanceId()); } } } } return b.toString(); }
public static Type getResourceType(LwM2mPath rscPath, LwM2mModel model, JsonArrayEntry resourceElt) { // Use model type in priority ResourceModel rscDesc = model.getResourceModel(rscPath.getObjectId(), rscPath.getResourceId()); if (rscDesc != null && rscDesc.type != null) return rscDesc.type; // Then json type if (resourceElt != null) { Type type = resourceElt.getType(); if (type != null) return type; } // Else use String as default LOG.trace("unknown type for resource use string as default: {}", rscPath); return Type.STRING; } }
node = new LwM2mObject(baseName.getObjectId(), instances); } else if (nodeClass == LwM2mObjectInstance.class) {
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into TLV", instance); Tlv[] tlvs; if (path.isObjectInstance() || instance.getId() == LwM2mObjectInstance.UNDEFINED) { // the instanceId is part of the request path or is undefined // so the instance TLV layer is not needed. // encoded as an array of resource TLVs tlvs = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); } else { // encoded as an instance TLV Tlv[] resources = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); tlvs = new Tlv[] { new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getId()) }; } try { out.write(TlvEncoder.encode(tlvs).array()); } catch (IOException e) { throw new RuntimeException(e); } }
private static LwM2mPath extractAndValidateBaseName(JsonRootObject jsonObject, LwM2mPath requestPath) throws CodecException { // Check baseName is valid if (jsonObject.getBaseName() != null && !jsonObject.getBaseName().isEmpty()) { LwM2mPath bnPath = new LwM2mPath(jsonObject.getBaseName()); // check returned base name path is under requested path if (requestPath.getObjectId() != null && bnPath.getObjectId() != null) { if (!bnPath.getObjectId().equals(requestPath.getObjectId())) { throw new CodecException("Basename path [%s] does not match requested path [%s].", bnPath, requestPath); } if (requestPath.getObjectInstanceId() != null && bnPath.getObjectInstanceId() != null) { if (!bnPath.getObjectInstanceId().equals(requestPath.getObjectInstanceId())) { throw new CodecException("Basename path [%s] does not match requested path [%s].", bnPath, requestPath); } if (requestPath.getResourceId() != null && bnPath.getResourceId() != null) { if (!bnPath.getResourceId().equals(requestPath.getResourceId())) { throw new CodecException("Basename path [%s] does not match requested path [%s].", bnPath, requestPath); } } } } return bnPath; } return null; }
/** * Create a Object Link referencing an object instance with the given path. */ public static ObjectLink fromPath(String path) { LwM2mPath lwM2mPath = new LwM2mPath(path); if (lwM2mPath.isRoot()) { return new ObjectLink(); // create null link } else if (lwM2mPath.isObjectInstance()) { return new ObjectLink(lwM2mPath.getObjectId(), lwM2mPath.getObjectInstanceId()); } else { throw new IllegalArgumentException("Invalid path: ObjectLink should reference an object instance"); } }
ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Object val = converter.convertValue(resource.getValue(), resource.getType(), expectedType, path);
@Override protected CreateResponse doCreate(CreateRequest request) { Integer instanceId = request.getInstanceId(); if (instanceId == null) { // the client is in charge to generate the id of the new instance if (instances.isEmpty()) { instanceId = 0; } else { instanceId = Collections.max(instances.keySet()) + 1; } } LwM2mInstanceEnabler newInstance = instanceFactory.create(getObjectModel()); for (LwM2mResource resource : request.getResources()) { newInstance.write(resource.getId(), resource); } instances.put(instanceId, newInstance); listenInstance(newInstance, instanceId); return CreateResponse.success(new LwM2mPath(request.getPath().getObjectId(), instanceId).toString()); }
@Override public void visit(LwM2mResource resource) { if (resource.isMultiInstances()) { throw new CodecException("Multiple instances resource %s cannot be encoded in opaque format", path); } ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); if (rSpec != null && rSpec.type != Type.OPAQUE) { throw new CodecException("Only single opaque resource can be encoded in opaque format. [%s]", path); } LOG.trace("Encoding resource {} into text", resource); Object value = converter.convertValue(resource.getValue(), resource.getType(), Type.OPAQUE, path); encoded = (byte[]) value; } }
public static LwM2mNode decode(byte[] content, LwM2mPath path, LwM2mModel model) throws CodecException { if (!path.isResource()) throw new CodecException("Invalid path %s : TextDecoder decodes resource only", path); ResourceModel rDesc = model.getResourceModel(path.getObjectId(), path.getResourceId()); String strValue = content != null ? new String(content, StandardCharsets.UTF_8) : ""; if (rDesc != null && rDesc.type != null) { return LwM2mSingleResource.newResource(path.getResourceId(), parseTextValue(strValue, rDesc.type, path), rDesc.type); } else { // unknown resource, returning a default string value return LwM2mSingleResource.newStringResource(path.getResourceId(), strValue); } }
LwM2mPath resourcePath = new LwM2mPath(nodePath.getObjectId(), nodePath.getObjectInstanceId(), nodePath.getResourceId()); Map<Integer, JsonArrayEntry> multiResource = multiResourceMap.get(resourcePath); ResourceModel resourceModel = model.getResourceModel(baseName.getObjectId(), baseName.getResourceId());
private final void setTarget(Request coapRequest, LwM2mPath path) { EndpointContext context = EndpointContextUtil.extractContext(destination); coapRequest.setDestinationContext(context); // root path if (rootPath != null) { for (String rootPathPart : rootPath.split("/")) { if (!StringUtils.isEmpty(rootPathPart)) { coapRequest.getOptions().addUriPath(rootPathPart); } } } // objectId if (path.getObjectId() != null) { coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectId())); } // objectInstanceId if (path.getObjectInstanceId() == null) { if (path.getResourceId() != null) { coapRequest.getOptions().addUriPath("0"); // default instanceId } } else { coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectInstanceId())); } // resourceId if (path.getResourceId() != null) { coapRequest.getOptions().addUriPath(Integer.toString(path.getResourceId())); } }
public static LwM2mNode decode(byte[] content, LwM2mPath path, LwM2mModel model) throws CodecException { if (!path.isResource()) throw new CodecException("Invalid path %s : OpaqueDecoder decodes resource only", path); ResourceModel desc = model.getResourceModel(path.getObjectId(), path.getResourceId()); if (desc != null && desc.type != Type.OPAQUE) { throw new CodecException( "Invalid content format [%s] for path [%s], OPAQUE can only be used for single OPAQUE resource", desc.type, path); } return LwM2mSingleResource.newBinaryResource(path.getResourceId(), content); }
public static byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) { Validate.notNull(timestampedNodes); Validate.notNull(path); Validate.notNull(model); InternalEncoder internalEncoder = new InternalEncoder(); ArrayList<JsonArrayEntry> entries = new ArrayList<>(); for (TimestampedLwM2mNode timestampedLwM2mNode : timestampedNodes) { internalEncoder.objectId = path.getObjectId(); internalEncoder.model = model; internalEncoder.requestPath = path; internalEncoder.converter = converter; internalEncoder.resourceList = null; internalEncoder.timestamp = timestampedLwM2mNode.getTimestamp(); timestampedLwM2mNode.getNode().accept(internalEncoder); entries.addAll(internalEncoder.resourceList); } JsonRootObject jsonObject = new JsonRootObject(); jsonObject.setResourceList(entries); jsonObject.setBaseName(path.toString()); return LwM2mJson.toJsonLwM2m(jsonObject).getBytes(); }
LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId()); if (instanceEnabler == null) { doCreate(new CreateRequest(path.getObjectId(), instanceNode)); } else { doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values())); LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId()); if (instanceEnabler == null) { doCreate(new CreateRequest(path.getObjectId(), instanceNode)); } else { doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values())); LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId()); if (instanceEnabler == null) { doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource))); } else {
public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) throws CodecException { Validate.notNull(node); Validate.notNull(path); Validate.notNull(model); InternalEncoder internalEncoder = new InternalEncoder(); internalEncoder.objectId = path.getObjectId(); internalEncoder.model = model; internalEncoder.requestPath = path; internalEncoder.converter = converter; node.accept(internalEncoder); JsonRootObject jsonObject = new JsonRootObject(); jsonObject.setResourceList(internalEncoder.resourceList); jsonObject.setBaseName(path.toString()); return LwM2mJson.toJsonLwM2m(jsonObject).getBytes(); }
private Tlv encodeResource(LwM2mResource resource, LwM2mPath resourcePath) { ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Tlv rTlv; if (resource.isMultiInstances()) { Tlv[] instances = new Tlv[resource.getValues().size()]; int i = 0; for (Entry<Integer, ?> entry : resource.getValues().entrySet()) { LwM2mPath resourceInstancePath = resourcePath.append(entry.getKey()); Object convertedValue = converter.convertValue(entry.getValue(), resource.getType(), expectedType, resourceInstancePath); instances[i] = new Tlv(TlvType.RESOURCE_INSTANCE, null, this.encodeTlvValue(convertedValue, expectedType, resourceInstancePath), entry.getKey()); i++; } rTlv = new Tlv(TlvType.MULTIPLE_RESOURCE, instances, null, resource.getId()); } else { Object convertedValue = converter.convertValue(resource.getValue(), resource.getType(), expectedType, resourcePath); rTlv = new Tlv(TlvType.RESOURCE_VALUE, null, this.encodeTlvValue(convertedValue, expectedType, resourcePath), resource.getId()); } return rTlv; }
private final void setTarget(Request coapRequest, Client client, LwM2mPath path) { coapRequest.setDestination(client.getAddress()); coapRequest.setDestinationPort(client.getPort()); // root path if (client.getRootPath() != null) { for (String rootPath : client.getRootPath().split("/")) { if (!StringUtils.isEmpty(rootPath)) { coapRequest.getOptions().addUriPath(rootPath); } } } // objectId coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectId())); // objectInstanceId if (path.getObjectInstanceId() == null) { if (path.getResourceId() != null) { coapRequest.getOptions().addUriPath("0"); // default instanceId } } else { coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectInstanceId())); } // resourceId if (path.getResourceId() != null) { coapRequest.getOptions().addUriPath(Integer.toString(path.getResourceId())); } }
new LwM2mPath(path.getObjectId()), model, LwM2mObjectInstance.class); createRequest = new CreateRequest(contentFormat, path.getObjectId(), newInstance); } else { createRequest = new CreateRequest(contentFormat, path.getObjectId(), newInstance.getResources().values());