Skip to content

Commit

Permalink
GH-1588: Remove Assignation Level to Attribute Model.
Browse files Browse the repository at this point in the history
Replaced by Attachment.
  • Loading branch information
sbernard31 committed Feb 23, 2024
1 parent a7e591c commit 6c03a3d
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@
*******************************************************************************/
package org.eclipse.leshan.core.link.lwm2m.attributes;

import org.eclipse.leshan.core.node.LwM2mPath;

/**
* The attachment level of an LwM2m attribute.
* <p>
* This indicates the level (object, instance or resource) where an attribute can be applied. E.g. the 'pmin' attribute
* can only be applied on the Resource level, but it can be assigned on all levels. 'pmin' attributes that are assigned
* to the object or instance level are then inherited by all resources that don't have their own 'pmin' attribute.
* The Level (Object, Object Instance, Resource, Resource Instance) to which an Attribute is attached.
* <p>
* In LWM2M v1.1.1, there is some confusion between assignation level and attachement. This is attachement in a LWM2M
* v1.2.1 meaning.
*
* @see <a href="https://github.com/eclipse-leshan/leshan/issues/1588">Why we are using LWM2M v1.2.1 wording</a>
*
*/
public enum Attachment {
ROOT, OBJECT, OBJECT_INSTANCE, RESOURCE,
ROOT, OBJECT, OBJECT_INSTANCE, RESOURCE, RESOURCE_INTANCE;

public static Attachment fromPath(LwM2mPath path) {
Attachment attachement = null;
if (path.isRoot()) {
attachement = Attachment.ROOT;
} else if (path.isObject()) {
attachement = Attachment.OBJECT;
} else if (path.isObjectInstance()) {
attachement = Attachment.OBJECT_INSTANCE;
} else if (path.isResource()) {
attachement = Attachment.RESOURCE;
} else if (path.isResourceInstance()) {
attachement = Attachment.RESOURCE_INTANCE;
}
return attachement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public class DoubleAttributeModel extends LwM2mAttributeModel<Double> {

public DoubleAttributeModel(String coRELinkParam, Attachment attachment, Set<AssignationLevel> assignationLevels,
AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
public DoubleAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam, attachment, accessMode, attributeClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
public class LongAttributeModel extends LwM2mAttributeModel<Long> {

public LongAttributeModel(String coRELinkParam, Attachment attachment, Set<AssignationLevel> assignationLevels,
AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
public LongAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam, attachment, accessMode, attributeClass);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,12 @@ public String toCoreLinkFormat() {
}
}

public Attachment getAttachment() {
return model.getAttachment();
}

public boolean isWritable() {
return model.getAccessMode() == AccessMode.W || model.getAccessMode() == AccessMode.RW;
}

public boolean canBeAssignedTo(AssignationLevel assignationLevel) {
return model.canBeAssignedTo(assignationLevel);
public boolean canBeAttachedTo(Attachment attachement) {
return model.canBeAttachedTo(attachement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@
*/
public abstract class LwM2mAttributeModel<T> extends AttributeModel<LwM2mAttribute<T>> {

private final Attachment attachment;
private final Set<AssignationLevel> assignationLevels;
private final Set<Attachment> attachment;
private final AccessMode accessMode;
private final AttributeClass attributeClass;

protected LwM2mAttributeModel(String coRELinkParam, Attachment attachment, Set<AssignationLevel> assignationLevels,
AccessMode accessMode, AttributeClass attributeClass) {
protected LwM2mAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam);
this.attachment = attachment;
this.assignationLevels = assignationLevels;
this.accessMode = accessMode;
this.attributeClass = attributeClass;
}
Expand All @@ -56,11 +54,7 @@ public AccessMode getAccessMode() {
return accessMode;
}

public Set<AssignationLevel> getAssignationLevels() {
return assignationLevels;
}

public Attachment getAttachment() {
public Set<Attachment> getAttachment() {
return attachment;
}

Expand Down Expand Up @@ -92,10 +86,10 @@ public String getInvalidValueCause(T value) {
}

/**
* return true if the attribute can be assigned to the given assignation level.
* return true if the attribute can be assigned to the given level.
*/
public boolean canBeAssignedTo(AssignationLevel assignation) {
return getAssignationLevels().contains(assignation);
public boolean canBeAttachedTo(Attachment attachement) {
return getAttachment().contains(attachement);
}

/**
Expand All @@ -104,9 +98,9 @@ public boolean canBeAssignedTo(AssignationLevel assignation) {
* @return null is the attribute can be applied to the LWM2M node identified by the given path.
*/
public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
if (!canBeAssignedTo(AssignationLevel.fromPath(path))) {
return String.format("%s attribute is only applicable to %s, and so can not be assigned to %s", getName(),
getAssignationLevels(), path);
if (!canBeAttachedTo(Attachment.fromPath(path))) {
return String.format("%s attribute is only applicable to %s, and so can not be attached to %s", getName(),
getAttachment(), path);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public final class LwM2mAttributes {
// dim
public static final LwM2mAttributeModel<Long> DIMENSION = new PositiveLongAttributeModel(//
"dim", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE), //
EnumSet.of(Attachment.RESOURCE), //
AccessMode.R, //
AttributeClass.PROPERTIES) {
@Override
Expand Down Expand Up @@ -63,8 +62,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// ssid
public static final LwM2mAttributeModel<Long> SHORT_SERVER_ID = new PositiveLongAttributeModel(//
"ssid", //
Attachment.OBJECT_INSTANCE, //
EnumSet.of(AssignationLevel.OBJECT_INSTANCE), //
EnumSet.of(Attachment.OBJECT_INSTANCE), //
AccessMode.R, //
AttributeClass.PROPERTIES) {
@Override
Expand Down Expand Up @@ -98,8 +96,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// uri
public static final LwM2mAttributeModel<String> SERVER_URI = new StringAttributeModel(//
"uri", //
Attachment.OBJECT_INSTANCE, //
EnumSet.of(AssignationLevel.OBJECT_INSTANCE), //
EnumSet.of(Attachment.OBJECT_INSTANCE), //
AccessMode.R, //
AttributeClass.PROPERTIES) {

Expand All @@ -125,9 +122,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> MINIMUM_PERIOD = new PositiveLongAttributeModel(//
"pmin", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE,
AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.OBJECT, Attachment.OBJECT_INSTANCE, Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand All @@ -154,9 +149,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> MAXIMUM_PERIOD = new PositiveLongAttributeModel( //
"pmax", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE,
AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.OBJECT, Attachment.OBJECT_INSTANCE, Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand All @@ -183,8 +176,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Double> GREATER_THAN = new DoubleAttributeModel(//
"gt", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE, AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand Down Expand Up @@ -216,8 +208,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Double> LESSER_THAN = new DoubleAttributeModel( //
"lt", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE, AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand Down Expand Up @@ -247,8 +238,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// st
public static final LwM2mAttributeModel<Double> STEP = new PositiveDoubleAttributeModel(//
"st", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.RESOURCE, AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand Down Expand Up @@ -280,9 +270,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> EVALUATE_MINIMUM_PERIOD = new PositiveLongAttributeModel(//
"epmin", //
Attachment.RESOURCE, //
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE,
AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.OBJECT, Attachment.OBJECT_INSTANCE, Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand All @@ -309,9 +297,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) {
// See : https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/563
public static final LwM2mAttributeModel<Long> EVALUATE_MAXIMUM_PERIOD = new PositiveLongAttributeModel( //
"epmax", //
Attachment.RESOURCE,
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.OBJECT_INSTANCE, AssignationLevel.RESOURCE,
AssignationLevel.RESOURCE_INTANCE), //
EnumSet.of(Attachment.OBJECT, Attachment.OBJECT_INSTANCE, Attachment.RESOURCE, Attachment.RESOURCE_INTANCE), //
AccessMode.RW, //
AttributeClass.NOTIFICATION) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public class LwM2mVersionAttributeModel extends LwM2mAttributeModel<LwM2mVersion

public LwM2mVersionAttributeModel() {
super("lwm2m", //
Attachment.ROOT, //
EnumSet.of(AssignationLevel.ROOT), //
EnumSet.of(Attachment.ROOT), //
AccessMode.R, //
AttributeClass.PROPERTIES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*******************************************************************************/
package org.eclipse.leshan.core.link.lwm2m.attributes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -102,32 +101,16 @@ public void validate(LwM2mPath path) {
}

// TODO not sure we still need this function
public void validate(AssignationLevel assignationLevel) {
public void validate(Attachment attachment) {
// Can all attributes be assigned to this level?
for (LwM2mAttribute<?> attr : getLwM2mAttributes()) {
if (!attr.canBeAssignedTo(assignationLevel)) {
throw new IllegalArgumentException(String.format("Attribute '%s' cannot be assigned to level %s",
attr.getName(), assignationLevel.name()));
if (!attr.canBeAttachedTo(attachment)) {
throw new IllegalArgumentException(String.format("Attribute '%s' cannot be attached to level %s",
attr.getName(), attachment.name()));
}
}
}

/**
* Returns a new AttributeSet, containing only the attributes that have a matching Attachment level.
*
* @param attachment the Attachment level to filter by
* @return a new {@link LwM2mAttributeSet} containing the filtered attributes
*/
public LwM2mAttributeSet filter(Attachment attachment) {
List<LwM2mAttribute<?>> attrs = new ArrayList<>();
for (LwM2mAttribute<?> attr : getLwM2mAttributes()) {
if (attr.getAttachment() == attachment) {
attrs.add(attr);
}
}
return new LwM2mAttributeSet(attrs);
}

/**
* Creates a new AttributeSet by merging another AttributeSet onto this instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class ObjectVersionAttributeModel extends LwM2mAttributeModel<Version> {
public ObjectVersionAttributeModel() {
super(//
"ver", //
Attachment.OBJECT, //
EnumSet.of(AssignationLevel.OBJECT), //
EnumSet.of(Attachment.OBJECT), //
AccessMode.R, //
AttributeClass.PROPERTIES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public class PositiveDoubleAttributeModel extends LwM2mAttributeModel<Double> {

public PositiveDoubleAttributeModel(String coRELinkParam, Attachment attachment,
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
public PositiveDoubleAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam, attachment, accessMode, attributeClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
public class PositiveLongAttributeModel extends LwM2mAttributeModel<Long> {

public PositiveLongAttributeModel(String coRELinkParam, Attachment attachment,
Set<AssignationLevel> assignationLevels, AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
public PositiveLongAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam, attachment, accessMode, attributeClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/
public class StringAttributeModel extends LwM2mAttributeModel<String> {

public StringAttributeModel(String coRELinkParam, Attachment attachment, Set<AssignationLevel> assignationLevels,
AccessMode accessMode, AttributeClass attributeClass) {
super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass);
public StringAttributeModel(String coRELinkParam, Set<Attachment> attachment, AccessMode accessMode,
AttributeClass attributeClass) {
super(coRELinkParam, attachment, accessMode, attributeClass);
}

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

import org.eclipse.leshan.core.LwM2m.Version;
import org.eclipse.leshan.core.link.lwm2m.attributes.AssignationLevel;
import org.eclipse.leshan.core.link.lwm2m.attributes.Attachment;
import org.eclipse.leshan.core.link.lwm2m.attributes.DefaultLwM2mAttributeParser;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeSet;
Expand Down Expand Up @@ -139,7 +139,7 @@ public void should_validate_assignation() {
LwM2mAttributes.create(LwM2mAttributes.MAXIMUM_PERIOD, 60L));
Collection<Attribute> attributes = sut.asCollection();
assertEquals(2, attributes.size());
sut.validate(AssignationLevel.RESOURCE);
sut.validate(Attachment.RESOURCE);
}

@Test
Expand All @@ -151,7 +151,7 @@ public void should_throw_on_invalid_assignation_level() {
LwM2mAttributes.create(LwM2mAttributes.MAXIMUM_PERIOD, 60L));

// OBJECT_VERSION cannot be assigned on resource level
sut.validate(AssignationLevel.RESOURCE);
sut.validate(Attachment.RESOURCE);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.eclipse.leshan.core.LwM2m.Version;
import org.eclipse.leshan.core.link.lwm2m.attributes.AssignationLevel;
import org.eclipse.leshan.core.link.lwm2m.attributes.Attachment;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttribute;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributes;
import org.junit.jupiter.api.Test;
Expand All @@ -33,7 +33,7 @@ public void should_pick_correct_model() {
new Version("1.0"));
assertEquals("ver", verAttribute.getName());
assertEquals(new Version("1.0"), verAttribute.getValue());
assertTrue(verAttribute.canBeAssignedTo(AssignationLevel.OBJECT));
assertTrue(verAttribute.canBeAttachedTo(Attachment.OBJECT));
assertFalse(verAttribute.isWritable());
}
}

0 comments on commit 6c03a3d

Please sign in to comment.