Search in sources :

Example 6 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.

the class CreateTest method can_create_specific_instance_of_object.

@Test
public void can_create_specific_instance_of_object() throws InterruptedException {
    // create ACL instance
    LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, instance));
    // verify result
    assertEquals(ResponseCode.CREATED, response.getCode());
    assertEquals("2/12", response.getLocation());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) Test(org.junit.Test)

Example 7 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.

the class DownlinkRequestSerDes method deserialize.

public static DownlinkRequest<?> deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    String path = o.getString("path", null);
    switch(kind) {
        case "observe":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ObserveRequest(ContentFormat.fromCode(format), path);
            }
        case "delete":
            return new DeleteRequest(path);
        case "discover":
            return new DiscoverRequest(path);
        case "create":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonArray jResources = (JsonArray) o.get("resources");
                for (JsonValue jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
                    resources.add(resource);
                }
                return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
            }
        case "execute":
            String parameters = o.getString("parameters", null);
            return new ExecuteRequest(path, parameters);
        case "writeAttributes":
            {
                String observeSpec = o.getString("observeSpec", null);
                return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
            }
        case "write":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
                LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
                return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
            }
        case "read":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ReadRequest(ContentFormat.fromCode(format), path);
            }
        default:
            throw new IllegalStateException("Invalid request missing kind attribute");
    }
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Collection(java.util.Collection) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 8 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.

the class ObjectEnabler method doWrite.

@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
            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()));
            }
        }
        return BootstrapWriteResponse.success();
    }
    // Manage Instance case
    if (path.isObjectInstance()) {
        LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
        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()));
        }
        return BootstrapWriteResponse.success();
    }
    // Manage resource case
    LwM2mResource resource = (LwM2mResource) request.getNode();
    LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
    if (instanceEnabler == null) {
        doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
    } else {
        instanceEnabler.write(path.getResourceId(), resource);
    }
    return BootstrapWriteResponse.success();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 9 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.

the class CreateTest method cannot_create_mandatory_single_object.

@Test
public void cannot_create_mandatory_single_object() throws InterruptedException {
    // try to create another instance of device object
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(3, new LwM2mResource[] { LwM2mSingleResource.newIntegerResource(3, 123) }));
    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Test(org.junit.Test)

Example 10 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.

the class CreateTest method cannot_create_instance_with_extraneous_resources.

// TODO we must probably implement this.
@Ignore
@Test
public void cannot_create_instance_with_extraneous_resources() throws InterruptedException {
    // create ACL instance
    LwM2mObjectInstance instance = new LwM2mObjectInstance(0, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123), LwM2mSingleResource.newIntegerResource(50, 123)));
    CreateRequest request = new CreateRequest(2, instance);
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), request);
    // verify result
    assertEquals(ResponseCode.BAD_REQUEST, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // try to read to check if the instance is not created
    // client registration
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(2, 0));
    assertEquals(ResponseCode.NOT_FOUND, readResponse.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CreateRequest (org.eclipse.leshan.core.request.CreateRequest)17 Test (org.junit.Test)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)10 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)10 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)6 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)4 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 Registration (org.eclipse.leshan.server.registration.Registration)3 JsonArray (com.eclipsesource.json.JsonArray)2 JsonObject (com.eclipsesource.json.JsonObject)2 Request (org.eclipse.californium.core.coap.Request)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2