Search in sources :

Example 6 with ResourceModel

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[] {});
}
Also used : ArrayList(java.util.ArrayList) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) Link(org.eclipse.leshan.Link)

Example 7 with ResourceModel

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);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) Link(org.eclipse.leshan.Link)

Example 8 with ResourceModel

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);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel)

Example 9 with ResourceModel

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;
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) ResourceModel(org.eclipse.leshan.core.model.ResourceModel)

Example 10 with ResourceModel

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

Aggregations

ResourceModel (org.eclipse.leshan.core.model.ResourceModel)12 ArrayList (java.util.ArrayList)4 Type (org.eclipse.leshan.core.model.ResourceModel.Type)4 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)4 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)3 CodecException (org.eclipse.leshan.core.node.codec.CodecException)3 Link (org.eclipse.leshan.Link)2 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 Operations (org.eclipse.leshan.core.model.ResourceModel.Operations)1 JsonArrayEntry (org.eclipse.leshan.json.JsonArrayEntry)1 JsonRootObject (org.eclipse.leshan.json.JsonRootObject)1