Skip to content

Commit

Permalink
move from static LwM2mNode encoder/decoder to dynamic one
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 authored and Manuel Sangoi committed Jul 21, 2016
1 parent 3340178 commit f9b422f
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.eclipse.leshan.client.servers.DmServerInfo;
import org.eclipse.leshan.client.servers.RegistrationEngine;
import org.eclipse.leshan.client.servers.ServerInfo;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -144,7 +146,8 @@ protected Resource createRoot() {

// Create CoAP resources for each lwm2m Objects.
for (LwM2mObjectEnabler enabler : objectEnablers) {
final ObjectResource clientObject = new ObjectResource(enabler, bootstrapHandler);
final ObjectResource clientObject = new ObjectResource(enabler, bootstrapHandler, new LwM2mNodeEncoder(),
new LwM2mNodeDecoder());
clientSideServer.add(clientObject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ public class ObjectResource extends CoapResource implements NotifySender {

private final LwM2mObjectEnabler nodeEnabler;
private final BootstrapHandler bootstrapHandler;
private final LwM2mNodeEncoder encoder;
private final LwM2mNodeDecoder decoder;

public ObjectResource(LwM2mObjectEnabler nodeEnabler, BootstrapHandler bootstrapHandler) {
public ObjectResource(LwM2mObjectEnabler nodeEnabler, BootstrapHandler bootstrapHandler, LwM2mNodeEncoder encoder,
LwM2mNodeDecoder decoder) {
super(Integer.toString(nodeEnabler.getId()));
this.nodeEnabler = nodeEnabler;
this.nodeEnabler.setNotifySender(this);
this.bootstrapHandler = bootstrapHandler;
this.encoder = encoder;
this.decoder = decoder;
setObservable(true);
}

Expand Down Expand Up @@ -128,7 +133,7 @@ public void handleGET(CoapExchange exchange) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, LwM2mNodeEncoder.encode(content, format, path, model),
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
return;
} else {
Expand All @@ -143,7 +148,7 @@ public void handleGET(CoapExchange exchange) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, LwM2mNodeEncoder.encode(content, format, path, model),
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
return;
} else {
Expand Down Expand Up @@ -184,7 +189,7 @@ public void handlePUT(final CoapExchange coapExchange) {
LwM2mNode lwM2mNode;
try {
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
lwM2mNode = LwM2mNodeDecoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
if (identity.isLwm2mBootstrapServer()) {
BootstrapWriteResponse response = nodeEnabler.write(identity,
new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
Expand Down Expand Up @@ -231,7 +236,7 @@ public void handlePOST(final CoapExchange exchange) {
// Manage Update Instance
if (path.isObjectInstance()) {
try {
LwM2mNode lwM2mNode = LwM2mNodeDecoder.decode(exchange.getRequestPayload(), contentFormat, path, model);
LwM2mNode lwM2mNode = decoder.decode(exchange.getRequestPayload(), contentFormat, path, model);
WriteResponse response = nodeEnabler.write(identity,
new WriteRequest(Mode.UPDATE, contentFormat, URI, lwM2mNode));
exchange.respond(fromLwM2mCode(response.getCode()), response.getErrorMessage());
Expand All @@ -245,7 +250,7 @@ public void handlePOST(final CoapExchange exchange) {
// Manage Create Request
try {
// decode the payload as an instance
LwM2mObjectInstance newInstance = LwM2mNodeDecoder.decode(exchange.getRequestPayload(), contentFormat,
LwM2mObjectInstance newInstance = decoder.decode(exchange.getRequestPayload(), contentFormat,
new LwM2mPath(path.getObjectId()), model, LwM2mObjectInstance.class);

if (newInstance.getResources().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class LwM2mNodeDecoder {
* @return the resulting node
* @throws InvalidValueException
*/
public static LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath path, LwM2mModel model)
public LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath path, LwM2mModel model)
throws InvalidValueException {
return decode(content, format, path, model, nodeClassFromPath(path));
}
Expand All @@ -66,7 +66,7 @@ public static LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath p
* @throws InvalidValueException
*/
@SuppressWarnings("unchecked")
public static <T extends LwM2mNode> T decode(byte[] content, ContentFormat format, LwM2mPath path, LwM2mModel model,
public <T extends LwM2mNode> T decode(byte[] content, ContentFormat format, LwM2mPath path, LwM2mModel model,
Class<T> nodeClass) throws InvalidValueException {

LOG.debug("Decoding value for path {} and format {}: {}", path, format, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LwM2mNodeEncoder {
* @param model the collection of supported object models
* @return the encoded node as a byte array
*/
public static byte[] encode(LwM2mNode node, ContentFormat format, LwM2mPath path, LwM2mModel model) {
public byte[] encode(LwM2mNode node, ContentFormat format, LwM2mPath path, LwM2mModel model) {
Validate.notNull(node);
Validate.notNull(format);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@
public class LwM2mNodeDecoderTest {

private static LwM2mModel model;
private static LwM2mNodeDecoder decoder;

@BeforeClass
public static void loadModel() {
model = new LwM2mModel(ObjectLoader.loadDefault());
decoder = new LwM2mNodeDecoder();
}

@Test
public void text_manufacturer_resource() throws InvalidValueException {
String value = "MyManufacturer";
LwM2mSingleResource resource = (LwM2mSingleResource) LwM2mNodeDecoder.decode(value.getBytes(Charsets.UTF_8),
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode(value.getBytes(Charsets.UTF_8),
ContentFormat.TEXT, new LwM2mPath(3, 0, 0), model);

assertEquals(0, resource.getId());
Expand All @@ -65,8 +67,8 @@ public void text_manufacturer_resource() throws InvalidValueException {
@Test
public void no_model_and_no_content_type_then_fallback_to_text() throws InvalidValueException {
String value = "MyManufacturer";
LwM2mSingleResource resource = (LwM2mSingleResource) LwM2mNodeDecoder.decode(value.getBytes(Charsets.UTF_8),
null, new LwM2mPath(666, 0, 0), model);
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode(value.getBytes(Charsets.UTF_8), null,
new LwM2mPath(666, 0, 0), model);

assertEquals(0, resource.getId());
assertFalse(resource.isMultiInstances());
Expand All @@ -76,7 +78,7 @@ public void no_model_and_no_content_type_then_fallback_to_text() throws InvalidV

@Test
public void text_battery_resource() throws InvalidValueException {
LwM2mSingleResource resource = (LwM2mSingleResource) LwM2mNodeDecoder.decode("100".getBytes(Charsets.UTF_8),
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode("100".getBytes(Charsets.UTF_8),
ContentFormat.TEXT, new LwM2mPath(3, 0, 9), model);

assertEquals(9, resource.getId());
Expand All @@ -90,7 +92,7 @@ public void tlv_manufacturer_resource() throws InvalidValueException {
String value = "MyManufacturer";
byte[] content = TlvEncoder.encode(new Tlv[] { new Tlv(TlvType.RESOURCE_VALUE, null, value.getBytes(), 0) })
.array();
LwM2mSingleResource resource = (LwM2mSingleResource) LwM2mNodeDecoder.decode(content, ContentFormat.TLV,
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode(content, ContentFormat.TLV,
new LwM2mPath(3, 0, 0), model);

assertEquals(0, resource.getId());
Expand All @@ -108,7 +110,7 @@ public void tlv_manufacturer_resource() throws InvalidValueException {

@Test
public void tlv_device_object_mono_instance() throws Exception {
LwM2mObjectInstance oInstance = ((LwM2mObject) LwM2mNodeDecoder.decode(ENCODED_DEVICE, ContentFormat.TLV,
LwM2mObjectInstance oInstance = ((LwM2mObject) decoder.decode(ENCODED_DEVICE, ContentFormat.TLV,
new LwM2mPath(3), model)).getInstance(0);
assertDeviceInstance(oInstance);
}
Expand Down Expand Up @@ -142,16 +144,16 @@ private void assertDeviceInstance(LwM2mObjectInstance oInstance) {
@Test
public void tlv_device_object_instance0_from_resources_tlv() throws InvalidValueException {

LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(ENCODED_DEVICE,
ContentFormat.TLV, new LwM2mPath(3, 0), model);
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(ENCODED_DEVICE, ContentFormat.TLV,
new LwM2mPath(3, 0), model);
assertDeviceInstance(oInstance);
}

@Test
public void tlv_device_object_instance0_from_resources_tlv__instance_expected() throws InvalidValueException {

LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(ENCODED_DEVICE,
ContentFormat.TLV, new LwM2mPath(3), model, LwM2mObjectInstance.class);
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(ENCODED_DEVICE, ContentFormat.TLV,
new LwM2mPath(3), model, LwM2mObjectInstance.class);
assertDeviceInstance(oInstance);
}

Expand All @@ -163,7 +165,7 @@ public void tlv_device_object_instance0_from_instance_tlv() throws InvalidValueE
System.arraycopy(new byte[] { 8, 0, 119 }, 0, instanceTlv, 0, 3);
System.arraycopy(ENCODED_DEVICE, 0, instanceTlv, 3, ENCODED_DEVICE.length);

LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(instanceTlv, ContentFormat.TLV,
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(instanceTlv, ContentFormat.TLV,
new LwM2mPath(3, 0), model);
assertDeviceInstance(oInstance);
}
Expand All @@ -172,8 +174,8 @@ public void tlv_device_object_instance0_from_instance_tlv() throws InvalidValueE
public void tlv_power_source__array_values() throws InvalidValueException {
byte[] content = new byte[] { 65, 0, 1, 65, 1, 5 };

LwM2mResource resource = (LwM2mResource) LwM2mNodeDecoder.decode(content, ContentFormat.TLV, new LwM2mPath(3,
0, 6), model);
LwM2mResource resource = (LwM2mResource) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 6),
model);

assertEquals(6, resource.getId());
assertEquals(2, resource.getValues().size());
Expand All @@ -187,8 +189,8 @@ public void tlv_power_source__multiple_resource() throws InvalidValueException {
// is probably not compliant with the spec but it should be supported by the server
byte[] content = new byte[] { -122, 6, 65, 0, 1, 65, 1, 5 };

LwM2mResource resource = (LwM2mResource) LwM2mNodeDecoder.decode(content, ContentFormat.TLV, new LwM2mPath(3,
0, 6), model);
LwM2mResource resource = (LwM2mResource) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 6),
model);

assertEquals(6, resource.getId());
assertEquals(2, resource.getValues().size());
Expand All @@ -199,22 +201,21 @@ public void tlv_power_source__multiple_resource() throws InvalidValueException {
@Test(expected = InvalidValueException.class)
public void tlv_multi_instance_object__missing_instance_tlv() throws InvalidValueException {

byte[] content = TlvEncoder.encode(
new Tlv[] { new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 1),
new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 2) }).array();
byte[] content = TlvEncoder.encode(new Tlv[] { new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 1),
new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 2) })
.array();

LwM2mNodeDecoder.decode(content, ContentFormat.TLV, new LwM2mPath(9), model);
decoder.decode(content, ContentFormat.TLV, new LwM2mPath(9), model);
}

@Test
public void tlv_unknown_object__missing_instance_tlv() throws InvalidValueException {

byte[] content = TlvEncoder.encode(
new Tlv[] { new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 1),
new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 2) }).array();
byte[] content = TlvEncoder.encode(new Tlv[] { new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 1),
new Tlv(TlvType.RESOURCE_VALUE, null, "value1".getBytes(), 2) })
.array();

LwM2mObject obj = (LwM2mObject) LwM2mNodeDecoder
.decode(content, ContentFormat.TLV, new LwM2mPath(10234), model);
LwM2mObject obj = (LwM2mObject) decoder.decode(content, ContentFormat.TLV, new LwM2mPath(10234), model);

assertEquals(1, obj.getInstances().size());
assertEquals(2, obj.getInstance(0).getResources().size());
Expand Down Expand Up @@ -242,7 +243,7 @@ public void json_device_object_instance0() throws InvalidValueException {
b.append("{\"n\":\"14\",\"sv\":\"+02:00\"},");
b.append("{\"n\":\"15\",\"sv\":\"U\"}]}");

LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(b.toString().getBytes(),
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(),
ContentFormat.JSON, new LwM2mPath(3, 0), model);

assertDeviceInstance(oInstance);
Expand Down Expand Up @@ -271,7 +272,7 @@ public void json_device_object_instance0_with_root_basename() throws InvalidValu
b.append("{\"n\":\"3/0/14\",\"sv\":\"+02:00\"},");
b.append("{\"n\":\"3/0/15\",\"sv\":\"U\"}]}");

LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(b.toString().getBytes(),
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(),
ContentFormat.JSON, new LwM2mPath(3, 0), model);

assertDeviceInstance(oInstance);
Expand All @@ -285,7 +286,7 @@ public void json_custom_object_instance() throws InvalidValueException {
b.append("{\"n\":\"0\",\"sv\":\"a string\"},");
b.append("{\"n\":\"1\",\"v\":10.5},");
b.append("{\"n\":\"2\",\"bv\":true}]}");
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) LwM2mNodeDecoder.decode(b.toString().getBytes(),
LwM2mObjectInstance oInstance = (LwM2mObjectInstance) decoder.decode(b.toString().getBytes(),
ContentFormat.JSON, new LwM2mPath(1024, 0), model);

assertEquals(0, oInstance.getId());
Expand Down
Loading

0 comments on commit f9b422f

Please sign in to comment.