Skip to content

Commit

Permalink
Add "version" to Objects in LWM2M Model
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Persson <daniel.p.persson@husqvarnagroup.com>
  • Loading branch information
danielhqv authored and sbernard31 committed Jan 17, 2018
1 parent b699367 commit ccaccfd
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.util.StringUtils;

/**
* An Utility class which help to generate @{link Link} from {@link LwM2mObjectEnabler} and {@link LwM2mModel}.<br>
Expand Down Expand Up @@ -65,15 +66,16 @@ public int compare(LwM2mObjectEnabler o1, LwM2mObjectEnabler o2) {
continue;

List<Integer> availableInstance = objectEnabler.getAvailableInstanceIds();
if (availableInstance.isEmpty()) {
// Include an object link if there are no instances or there are object attributes (e.g. "ver")
Map<String, ?> objectAttributes = getObjectAttributes(objectEnabler.getObjectModel());
if (availableInstance.isEmpty() || (objectAttributes != null)) {
String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()));
links.add(new Link(objectInstanceUrl, objectAttributes));
}
for (Integer instanceId : objectEnabler.getAvailableInstanceIds()) {
String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()),
instanceId.toString());
links.add(new Link(objectInstanceUrl));
} else {
for (Integer instanceId : objectEnabler.getAvailableInstanceIds()) {
String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()),
instanceId.toString());
links.add(new Link(objectInstanceUrl));
}
}
}

Expand All @@ -87,8 +89,9 @@ public static Link[] getObjectDescription(ObjectModel objectModel, String root)
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));
links.add(new Link(objectURL, objectAttributes));

// sort resources
List<ResourceModel> resources = new ArrayList<>(objectModel.resources.values());
Expand Down Expand Up @@ -189,4 +192,13 @@ private static String normalize(String input, int len, int off) {
}
return sb.toString();
}

private static Map<String, ?> getObjectAttributes(ObjectModel objectModel) {
if (StringUtils.isEmpty(objectModel.version) || ObjectModel.DEFAULT_VERSION.equals(objectModel.version)) {
return null;
}
Map<String, String> attributes = new HashMap<>();
attributes.put("ver", objectModel.version);
return attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.leshan.Link;
import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectEnabler;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.junit.Test;
Expand Down Expand Up @@ -68,6 +75,17 @@ public void encode_objectModel_to_linkObject_with_explicit_empty_root_path() {
assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}

@Test
public void encode_objectModel_to_linkObject_with_version2_0() {
ObjectModel locationModel = getVersionedObjectModel(6, "2.0");

Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/");
String strLinks = Link.serialize(links);

assertEquals("</6>;ver=\"2.0\", </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>",
strLinks);
}

@Test
public void encode_objectModel_to_linkObject_with_explicit_complex_root_path() {
ObjectModel locationModel = getObjectModel(6);
Expand All @@ -80,6 +98,48 @@ public void encode_objectModel_to_linkObject_with_explicit_complex_root_path() {
strLinks);
}

@Test
public void encode_client_description_with_version_1_0() {
List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();

Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
instancesMap.put(0, new BaseInstanceEnabler());
objectEnablers.add(new ObjectEnabler(6, getObjectModel(6), instancesMap, null));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);

assertEquals("</>;rt=\"oma.lwm2m\", </6/0>", strLinks);
}

@Test
public void encode_client_description_with_version_2_0() {
List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();

Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
instancesMap.put(0, new BaseInstanceEnabler());
instancesMap.put(1, new BaseInstanceEnabler());
objectEnablers.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);

assertEquals("</>;rt=\"oma.lwm2m\", </6>;ver=\"2.0\", </6/0>, </6/1>", strLinks);
}

@Test
public void encode_client_description_with_version_2_0_no_instances() {
List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();

Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
objectEnablers.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);

assertEquals("</>;rt=\"oma.lwm2m\", </6>;ver=\"2.0\"", strLinks);
}

private ObjectModel getObjectModel(int id) {
List<ObjectModel> objectModels = ObjectLoader.loadDefault();
for (ObjectModel objectModel : objectModels) {
Expand All @@ -88,4 +148,17 @@ private ObjectModel getObjectModel(int id) {
}
return null;
}

/**
* Gets a default object model by id and manipulates its version.
*/
private ObjectModel getVersionedObjectModel(int id, String version) {
List<ObjectModel> objectModels = ObjectLoader.loadDefault();
for (ObjectModel om : objectModels) {
if (om.id == id)
return new ObjectModel(om.id, om.name, om.description, version, om.multiple, om.mandatory,
om.resources.values());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private ObjectModel parseObject(Node object) {
Integer id = null;
String name = null;
String description = null;
String version = ObjectModel.DEFAULT_VERSION;
boolean multiple = false;
boolean mandatory = false;
List<ResourceModel> resources = new ArrayList<>();
Expand All @@ -97,6 +98,9 @@ private ObjectModel parseObject(Node object) {
case "Description1":
description = field.getTextContent();
break;
case "ObjectVersion":
version = field.getTextContent();
break;
case "MultipleInstances":
multiple = "Multiple".equals(field.getTextContent());
break;
Expand All @@ -114,7 +118,7 @@ private ObjectModel parseObject(Node object) {
}
}

return new ObjectModel(id, name, description, multiple, mandatory, resources);
return new ObjectModel(id, name, description, version, multiple, mandatory, resources);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,24 +32,33 @@ public class ObjectModel {

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

public static final String DEFAULT_VERSION = "1.0";

private static final int OMA_OBJECT_MIN_ID = 0;
private static final int OMA_OBJECT_MAX_ID = 1023;

public final int id;
public final String name;
public final String description;
public final String version;
public final boolean multiple;
public final boolean mandatory;

public final Map<Integer, ResourceModel> resources; // resources by ID

public ObjectModel(int id, String name, String description, boolean multiple, boolean mandatory,
public ObjectModel(int id, String name, String description, String version, boolean multiple, boolean mandatory,
ResourceModel... resources) {
this(id, name, description, multiple, mandatory, Arrays.asList(resources));
this(id, name, description, version, multiple, mandatory, Arrays.asList(resources));
}

public ObjectModel(int id, String name, String description, boolean multiple, boolean mandatory,
public ObjectModel(int id, String name, String description, String version, boolean multiple, boolean mandatory,
Collection<ResourceModel> resources) {
Validate.notEmpty(version);

this.id = id;
this.name = name;
this.description = description;
this.version = version;
this.multiple = multiple;
this.mandatory = mandatory;

Expand All @@ -63,12 +73,16 @@ public ObjectModel(int id, String name, String description, boolean multiple, bo
this.resources = Collections.unmodifiableMap(resourcesMap);
}

public boolean isOmaObject() {
return id >= OMA_OBJECT_MIN_ID && id <= OMA_OBJECT_MAX_ID;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ObjectModel [id=").append(id).append(", name=").append(name).append(", description=")
.append(description).append(", multiple=").append(multiple).append(", mandatory=").append(mandatory)
.append(", resources=").append(resources).append("]");
.append(description).append(", version=").append(version).append(", multiple=").append(multiple)
.append(", mandatory=").append(mandatory).append(", resources=").append(resources).append("]");
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public JsonObject jSerialize(ObjectModel m) {
o.add("id", m.id);
o.add("instancetype", m.multiple ? "multiple" : "single");
o.add("mandatory", m.mandatory);
if (!ObjectModel.DEFAULT_VERSION.equals(m.version))
o.add("version", m.version);
o.add("description", m.description);

// sort resources value
Expand Down Expand Up @@ -74,8 +76,10 @@ public ObjectModel deserialize(JsonObject o) {
String instancetype = o.getString("instancetype", null);
boolean mandatory = o.getBoolean("mandatory", false);
String description = o.getString("description", null);
String version = o.getString("version", ObjectModel.DEFAULT_VERSION);
List<ResourceModel> resourceSpecs = resourceModelSerDes.deserialize(o.get("resourcedefs").asArray());

return new ObjectModel(id, name, description, "multiple".equals(instancetype), mandatory, resourceSpecs);
return new ObjectModel(id, name, description, version, "multiple".equals(instancetype), mandatory,
resourceSpecs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ protected List<ObjectModel> createObjectModels() {
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, false, false, stringfield, booleanfield,
integerfield, floatfield, timefield, opaquefield, objlnkfield, objlnkSinglefield));
objectModels.add(new ObjectModel(TEST_OBJECT_ID, "testobject", null, ObjectModel.DEFAULT_VERSION, false, false,
stringfield, booleanfield, integerfield, floatfield, timefield, opaquefield, objlnkfield,
objlnkSinglefield));

return objectModels;
}
Expand Down

0 comments on commit ccaccfd

Please sign in to comment.