Search in sources :

Example 1 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class ObjectEnabler method doWrite.

@Override
protected WriteResponse doWrite(ServerIdentity identity, WriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Instance case
    LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null)
        return WriteResponse.notFound();
    if (path.isObjectInstance()) {
        // instance write
        Map<Integer, LwM2mResource> writeResources = ((LwM2mObjectInstance) request.getNode()).getResources();
        if (request.isReplaceRequest()) {
            // REPLACE
            // make them modifiable
            writeResources = new HashMap<>(writeResources);
            for (ResourceModel resourceModel : getObjectModel().resources.values()) {
                if (!identity.isLwm2mServer() || resourceModel.operations.isWritable()) {
                    LwM2mResource writeResource = writeResources.remove(resourceModel.id);
                    if (null != writeResource) {
                        instance.write(resourceModel.id, writeResource);
                    } else {
                        instance.reset(resourceModel.id);
                    }
                }
            }
        }
        // UPDATE and resources currently not in the model
        for (LwM2mResource resource : writeResources.values()) {
            instance.write(resource.getId(), resource);
        }
        return WriteResponse.success();
    }
    // Manage Resource case
    return instance.write(path.getResourceId(), (LwM2mResource) request.getNode());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 2 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class ObjectEnabler method doCreate.

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

Example 3 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class ServersInfoExtractor method getInfo.

public static ServersInfo getInfo(Map<Integer, LwM2mObjectEnabler> objectEnablers) {
    LwM2mObjectEnabler securityEnabler = objectEnablers.get(SECURITY);
    LwM2mObjectEnabler serverEnabler = objectEnablers.get(SERVER);
    if (securityEnabler == null || serverEnabler == null)
        return null;
    ServersInfo infos = new ServersInfo();
    LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
    LwM2mObject servers = (LwM2mObject) serverEnabler.read(SYSTEM, new ReadRequest(SERVER)).getContent();
    for (LwM2mObjectInstance security : securities.getInstances().values()) {
        try {
            if ((boolean) security.getResource(SEC_BOOTSTRAP).getValue()) {
                if (infos.bootstrap != null) {
                    LOG.warn("There is more than one bootstrap configuration in security object.");
                } else {
                    // create bootstrap info
                    ServerInfo info = new ServerInfo();
                    LwM2mResource serverIdResource = security.getResource(SEC_SERVER_ID);
                    if (serverIdResource != null && serverIdResource.getValue() != null)
                        info.serverId = (long) serverIdResource.getValue();
                    else
                        info.serverId = 0;
                    info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                    info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                    infos.bootstrap = info;
                }
            } else {
                // create device management info
                DmServerInfo info = new DmServerInfo();
                info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
                info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                // search corresponding device management server
                for (LwM2mObjectInstance server : servers.getInstances().values()) {
                    if (info.serverId == (Long) server.getResource(SRV_SERVER_ID).getValue()) {
                        info.lifetime = (long) server.getResource(SRV_LIFETIME).getValue();
                        info.binding = BindingMode.valueOf((String) server.getResource(SRV_BINDING).getValue());
                        infos.deviceMangements.put(info.serverId, info);
                        break;
                    }
                }
            }
        } catch (URISyntaxException e) {
            LOG.error(String.format("Invalid URI %s", (String) security.getResource(SEC_SERVER_URI).getValue()), e);
        }
    }
    return infos;
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 4 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method parseJSON.

private static List<TimestampedLwM2mNode> parseJSON(JsonRootObject jsonObject, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
    LOG.trace("Parsing JSON content for path {}: {}", path, jsonObject);
    // Group JSON entry by time-stamp
    Map<Long, Collection<JsonArrayEntry>> jsonEntryByTimestamp = groupJsonEntryByTimestamp(jsonObject);
    // Extract baseName
    LwM2mPath baseName = extractAndValidateBaseName(jsonObject, path);
    if (baseName == null)
        // if no base name, use request path as base name
        baseName = path;
    // fill time-stamped nodes collection
    List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
    for (Entry<Long, Collection<JsonArrayEntry>> entryByTimestamp : jsonEntryByTimestamp.entrySet()) {
        // Group JSON entry by instance
        Map<Integer, Collection<JsonArrayEntry>> jsonEntryByInstanceId = groupJsonEntryByInstanceId(entryByTimestamp.getValue(), baseName);
        // Create lwm2m node
        LwM2mNode node;
        if (nodeClass == LwM2mObject.class) {
            Collection<LwM2mObjectInstance> instances = new ArrayList<>();
            for (Entry<Integer, Collection<JsonArrayEntry>> entryByInstanceId : jsonEntryByInstanceId.entrySet()) {
                Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(entryByInstanceId.getValue(), baseName, model);
                instances.add(new LwM2mObjectInstance(entryByInstanceId.getKey(), resourcesMap.values()));
            }
            node = new LwM2mObject(baseName.getObjectId(), instances);
        } else if (nodeClass == LwM2mObjectInstance.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() != 1)
                throw new CodecException("One instance expected in the payload [path:%s]", path);
            // Extract resources
            Entry<Integer, Collection<JsonArrayEntry>> instanceEntry = jsonEntryByInstanceId.entrySet().iterator().next();
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(instanceEntry.getValue(), baseName, model);
            // Create instance
            node = new LwM2mObjectInstance(instanceEntry.getKey(), resourcesMap.values());
        } else if (nodeClass == LwM2mResource.class) {
            // validate we have resources for only 1 instance
            if (jsonEntryByInstanceId.size() > 1)
                throw new CodecException("Only one instance expected in the payload [path:%s]", path);
            // Extract resources
            Map<Integer, LwM2mResource> resourcesMap = extractLwM2mResources(jsonEntryByInstanceId.values().iterator().next(), baseName, model);
            // validate there is only 1 resource
            if (resourcesMap.size() != 1)
                throw new CodecException("One resource should be present in the payload [path:%s]", path);
            node = resourcesMap.values().iterator().next();
        } else {
            throw new IllegalArgumentException("invalid node class: " + nodeClass);
        }
        // compute time-stamp
        Long timestamp = computeTimestamp(jsonObject.getBaseTime(), entryByTimestamp.getKey());
        // add time-stamped node
        timestampedNodes.add(new TimestampedLwM2mNode(timestamp, node));
    }
    return timestampedNodes;
}
Also used : ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry) Entry(java.util.Map.Entry) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 5 with LwM2mResource

use of org.eclipse.leshan.core.node.LwM2mResource in project leshan by eclipse.

the class WriteTest method can_write_replacing_object_instance.

@Test
public void can_write_replacing_object_instance() throws InterruptedException {
    // setup server object
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(1, 0, 3, 60));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // write server object
    LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120);
    LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10);
    LwM2mResource notificationStoring = LwM2mSingleResource.newBooleanResource(6, false);
    LwM2mResource binding = LwM2mSingleResource.newStringResource(7, "U");
    response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, 1, 0, lifetime, defaultMinPeriod, notificationStoring, binding));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read the values to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(1, 0));
    LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
    assertEquals(lifetime, instance.getResource(1));
    assertEquals(defaultMinPeriod, instance.getResource(2));
    assertEquals(notificationStoring, instance.getResource(6));
    assertEquals(binding, instance.getResource(7));
    // removed not contained optional writable resource
    assertNull(instance.getResource(3));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)33 Test (org.junit.Test)15 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)14 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)10 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)9 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 JsonObject (com.eclipsesource.json.JsonObject)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 JsonArray (com.eclipsesource.json.JsonArray)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonValue (com.eclipsesource.json.JsonValue)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2