Skip to content

Commit

Permalink
Make LwM2mModel an interface. Concrete class is now StaticModel
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 12, 2019
1 parent cad7db0 commit a8580a2
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.leshan.LwM2m;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.server.bootstrap.demo.servlet.BootstrapServlet;
import org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet;
import org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder;
Expand Down Expand Up @@ -174,7 +174,7 @@ public static void createAndStartServer(String webAddress, int webPort, String l
builder.setSecurityStore(new BootstrapConfigSecurityStore(bsStore));
builder.setLocalAddress(localAddress, localPort);
builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
builder.setModel(new LwM2mModel(models));
builder.setModel(new StaticModel(models));

// Create X509 credentials;
X509Certificate serverCertificate = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.leshan.client.servers.BootstrapHandler;
import org.eclipse.leshan.core.attributes.AttributeSet;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void handleGET(CoapExchange exchange) {
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(observeRequest, requestedContentFormat);
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
Expand All @@ -133,7 +134,7 @@ public void handleGET(CoapExchange exchange) {
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(readRequest, requestedContentFormat);
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
Expand Down Expand Up @@ -195,7 +196,7 @@ public void handlePUT(CoapExchange coapExchange) {
}
LwM2mNode lwM2mNode;
try {
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
if (identity.isLwm2mBootstrapServer()) {
BootstrapWriteResponse response = nodeEnabler.write(identity,
Expand Down Expand Up @@ -255,7 +256,7 @@ public void handlePOST(CoapExchange exchange) {
exchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
return;
}
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());

// Manage Update Instance
if (path.isObjectInstance()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.util.Validate;

Expand All @@ -41,7 +42,7 @@ public ObjectsInitializer() {

public ObjectsInitializer(LwM2mModel model) {
if (model == null) {
this.model = new LwM2mModel(ObjectLoader.loadDefault());
this.model = new StaticModel(ObjectLoader.loadDefault());
} else {
this.model = model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@

import org.eclipse.leshan.LwM2mId;
import org.eclipse.leshan.client.resource.BaseInstanceEnablerFactory;
import org.eclipse.leshan.client.resource.LwM2mInstanceEnabler;
import org.eclipse.leshan.client.resource.DummyInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mInstanceEnabler;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.StaticModel;
import org.junit.Test;

public class BaseInstanceEnablerFactoryTest {

public static final LwM2mModel model = new LwM2mModel(ObjectLoader.loadDefault());
public static final LwM2mModel model = new StaticModel(ObjectLoader.loadDefault());
public static List<Integer> emptyList = Collections.emptyList();

@Test(expected = IllegalStateException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.util.Hex;
import org.eclipse.leshan.util.SecurityUtil;
Expand Down Expand Up @@ -324,7 +324,7 @@ public static void createAndStartClient(String endpoint, String localAddress, in
models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));

// Initialize object list
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
ObjectsInitializer initializer = new ObjectsInitializer(new StaticModel(models));
if (needBootstrap) {
if (pskIdentity != null) {
initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskIdentity, pskKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,12 @@
*******************************************************************************/
package org.eclipse.leshan.core.model;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A collection of LWM2M object definitions.
* A collection of LWM2M object definitions. This collection contains only 1 version of a specific object definition.
*/
public class LwM2mModel {

private static final Logger LOG = LoggerFactory.getLogger(LwM2mModel.class);

private final Map<Integer, ObjectModel> objects; // objects by ID

public LwM2mModel(ObjectModel... objectModels) {
this(Arrays.asList(objectModels));
}

public LwM2mModel(Collection<ObjectModel> objectModels) {
if (objectModels == null) {
objects = new HashMap<>();
} else {
Map<Integer, ObjectModel> map = new HashMap<>();
for (ObjectModel model : objectModels) {
ObjectModel old = map.put(model.id, model);
if (old != null) {
LOG.debug("Model already exists for object {}. Overriding it.", model.id);
}
}
objects = Collections.unmodifiableMap(map);
}
}
public interface LwM2mModel {

/**
* Returns the description of a given resource.
Expand All @@ -59,29 +29,19 @@ public LwM2mModel(Collection<ObjectModel> objectModels) {
* @param resourceId the resource identifier
* @return the resource specification or <code>null</code> if not found
*/
public ResourceModel getResourceModel(int objectId, int resourceId) {
ObjectModel object = objects.get(objectId);
if (object != null) {
return object.resources.get(resourceId);
}
return null;
}
ResourceModel getResourceModel(int objectId, int resourceId);

/**
* Returns the description of a given object.
*
* @param objectId the object identifier
* @return the object definition or <code>null</code> if not found
*/
public ObjectModel getObjectModel(int objectId) {
return objects.get(objectId);
}
ObjectModel getObjectModel(int objectId);

/**
* @return all the objects descriptions known.
*/
public Collection<ObjectModel> getObjectModels() {
return objects.values();
}
Collection<ObjectModel> getObjectModels();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2019 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.model;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A static implementation of {@link LwM2mModel}.
*/
public class StaticModel implements LwM2mModel {

private static final Logger LOG = LoggerFactory.getLogger(StaticModel.class);

private final Map<Integer, ObjectModel> objects; // objects by ID

public StaticModel(ObjectModel... objectModels) {
this(Arrays.asList(objectModels));
}

public StaticModel(Collection<ObjectModel> objectModels) {
if (objectModels == null) {
objects = new HashMap<>();
} else {
Map<Integer, ObjectModel> map = new HashMap<>();
for (ObjectModel model : objectModels) {
ObjectModel old = map.put(model.id, model);
if (old != null) {
LOG.debug("Model already exists for object {}. Overriding it.", model.id);
}
}
objects = Collections.unmodifiableMap(map);
}
}

@Override
public ResourceModel getResourceModel(int objectId, int resourceId) {
ObjectModel object = objects.get(objectId);
if (object != null) {
return object.resources.get(resourceId);
}
return null;
}

@Override
public ObjectModel getObjectModel(int objectId) {
return objects.get(objectId);
}

@Override
public Collection<ObjectModel> getObjectModels() {
return objects.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Operations;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static void loadModel() {
objects.add(
new ObjectModel(66, "object link tests 66", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj66));

model = new LwM2mModel(objects);
model = new StaticModel(objects);
decoder = new DefaultLwM2mNodeDecoder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
Expand All @@ -49,7 +50,7 @@ public class LwM2mNodeEncoderTest {

@BeforeClass
public static void loadModel() {
model = new LwM2mModel(ObjectLoader.loadDefault());
model = new StaticModel(ObjectLoader.loadDefault());
encoder = new DefaultLwM2mNodeEncoder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
import org.eclipse.leshan.client.resource.DummyInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Operations;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.request.BindingMode;
Expand Down Expand Up @@ -147,7 +147,7 @@ public ExecuteResponse execute(ServerIdentity identity, int resourceid, String p

public void createClient(Map<String, String> additionalAttributes) {
// Create objects Enabler
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
ObjectsInitializer initializer = new ObjectsInitializer(new StaticModel(createObjectModels()));
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec(
"coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(),
12345));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.eclipse.californium.elements.RawData;
import org.eclipse.leshan.ResponseCode;
import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
Expand Down Expand Up @@ -324,7 +324,7 @@ public void can_handle_error_on_notification() throws InterruptedException {

// *** HACK send a notification with unsupported content format *** //
byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"),
new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
new LwM2mPath("/3/0/15"), new StaticModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666); // 666 is not a supported //
// contentFormat.
Expand Down Expand Up @@ -366,7 +366,7 @@ public void can_observe_timestamped_resource() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
LwM2mSingleResource.newStringResource(15, "Londres")));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3/0/15"),
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
new StaticModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
// *** Hack End *** //
Expand Down Expand Up @@ -409,7 +409,7 @@ public void can_observe_timestamped_instance() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres"))));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3/0"),
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
new StaticModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
// *** Hack End *** //
Expand Down Expand Up @@ -452,7 +452,7 @@ public void can_observe_timestamped_object() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"),
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
new StaticModel(helper.createObjectModels()), new LwM2mValueChecker());

Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.eclipse.leshan.client.resource.DummyInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.server.californium.LeshanServerBuilder;
Expand Down Expand Up @@ -61,7 +61,7 @@ public boolean accept(Registration registration) {
@Override
public void createClient() {
// Create objects Enabler
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
ObjectsInitializer initializer = new ObjectsInitializer(new StaticModel(createObjectModels()));
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec(
"coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(),
12345));
Expand Down
Loading

0 comments on commit a8580a2

Please sign in to comment.