use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.
the class LinkFormatHelper method getObjectDescription.
public static Link[] getObjectDescription(ObjectModel objectModel, String root) {
List<Link> links = new ArrayList<>();
// clean root path
String rootPath = root == null ? "" : root;
// create link for "object"
Map<String, ?> objectAttributes = getObjectAttributes(objectModel);
String objectURL = getPath("/", rootPath, Integer.toString(objectModel.id));
links.add(new Link(objectURL, objectAttributes));
// sort resources
List<ResourceModel> resources = new ArrayList<>(objectModel.resources.values());
Collections.sort(resources, new Comparator<ResourceModel>() {
@Override
public int compare(ResourceModel o1, ResourceModel o2) {
return o1.id - o2.id;
}
});
// create links for resource
for (ResourceModel resourceModel : resources) {
String resourceURL = getPath("/", rootPath, Integer.toString(objectModel.id), "0", Integer.toString(resourceModel.id));
links.add(new Link(resourceURL));
}
return links.toArray(new Link[] {});
}
use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.
the class BaseObjectEnabler method discover.
@Override
public synchronized DiscoverResponse discover(ServerIdentity identity, DiscoverRequest request) {
if (identity.isLwm2mBootstrapServer()) {
// discover is not supported for bootstrap
return DiscoverResponse.methodNotAllowed();
}
if (id == LwM2mId.SECURITY) {
return DiscoverResponse.notFound();
}
LwM2mPath path = request.getPath();
if (path.isObject()) {
// Manage discover on object
Link[] ObjectLinks = LinkFormatHelper.getObjectDescription(getObjectModel(), null);
return DiscoverResponse.success(ObjectLinks);
} else if (path.isObjectInstance()) {
// Manage discover on instance
if (!getAvailableInstanceIds().contains(path.getObjectInstanceId()))
return DiscoverResponse.notFound();
Link instanceLink = LinkFormatHelper.getInstanceDescription(getObjectModel(), path.getObjectInstanceId(), null);
return DiscoverResponse.success(new Link[] { instanceLink });
} else if (path.isResource()) {
// Manage discover on resource
if (!getAvailableInstanceIds().contains(path.getObjectInstanceId()))
return DiscoverResponse.notFound();
ResourceModel resourceModel = getObjectModel().resources.get(path.getResourceId());
if (resourceModel == null)
return DiscoverResponse.notFound();
Link resourceLink = LinkFormatHelper.getResourceDescription(getObjectModel().id, path.getObjectInstanceId(), resourceModel, null);
return DiscoverResponse.success(new Link[] { resourceLink });
}
return DiscoverResponse.badRequest(null);
}
use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.
the class BaseObjectEnabler method execute.
@Override
public synchronized ExecuteResponse execute(ServerIdentity identity, ExecuteRequest request) {
LwM2mPath path = request.getPath();
// execute is not supported for bootstrap
if (identity.isLwm2mBootstrapServer()) {
return ExecuteResponse.methodNotAllowed();
}
// execute on security object is forbidden
if (id == LwM2mId.SECURITY) {
return ExecuteResponse.notFound();
}
// only resource could be executed
if (!path.isResource()) {
return ExecuteResponse.badRequest(null);
}
// check if the resource is writable
ResourceModel resourceModel = objectModel.resources.get(path.getResourceId());
if (resourceModel != null && !resourceModel.operations.isExecutable()) {
return ExecuteResponse.methodNotAllowed();
}
return doExecute(request);
}
use of org.eclipse.leshan.core.model.ResourceModel in project leshan by eclipse.
the class IntegrationTestHelper method createObjectModels.
protected List<ObjectModel> createObjectModels() {
// load default object from the spec
List<ObjectModel> objectModels = ObjectLoader.loadDefault();
// define custom model for testing purpose
ResourceModel stringfield = new ResourceModel(STRING_RESOURCE_ID, "stringres", Operations.RW, false, false, Type.STRING, null, null, null);
ResourceModel booleanfield = new ResourceModel(BOOLEAN_RESOURCE_ID, "booleanres", Operations.RW, false, false, Type.BOOLEAN, null, null, null);
ResourceModel integerfield = new ResourceModel(INTEGER_RESOURCE_ID, "integerres", Operations.RW, false, false, Type.INTEGER, null, null, null);
ResourceModel floatfield = new ResourceModel(FLOAT_RESOURCE_ID, "floatres", Operations.RW, false, false, Type.FLOAT, null, null, null);
ResourceModel timefield = new ResourceModel(TIME_RESOURCE_ID, "timeres", Operations.RW, false, false, Type.TIME, null, null, null);
ResourceModel opaquefield = new ResourceModel(OPAQUE_RESOURCE_ID, "opaque", Operations.RW, false, false, Type.OPAQUE, null, null, null);
ResourceModel objlnkfield = new ResourceModel(OBJLNK_MULTI_INSTANCE_RESOURCE_ID, "objlnk", Operations.RW, true, false, Type.OBJLNK, null, null, null);
ResourceModel objlnkSinglefield = new ResourceModel(OBJLNK_SINGLE_INSTANCE_RESOURCE_ID, "objlnk", Operations.RW, false, false, Type.OBJLNK, null, null, null);
objectModels.add(new ObjectModel(TEST_OBJECT_ID, "testobject", null, ObjectModel.DEFAULT_VERSION, false, false, stringfield, booleanfield, integerfield, floatfield, timefield, opaquefield, objlnkfield, objlnkSinglefield));
return objectModels;
}
use of org.eclipse.leshan.core.model.ResourceModel 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;
}
Aggregations