diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/Attachment.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/Attachment.java index 5a521b98ff..90ab2ebd0e 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/Attachment.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/Attachment.java @@ -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. *

- * 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. + *

+ * 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 Why we are using LWM2M v1.2.1 wording + * */ 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; + } } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/DoubleAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/DoubleAttributeModel.java index 3ce73801b3..2cf18f3b7f 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/DoubleAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/DoubleAttributeModel.java @@ -27,9 +27,9 @@ */ public class DoubleAttributeModel extends LwM2mAttributeModel { - public DoubleAttributeModel(String coRELinkParam, Attachment attachment, Set assignationLevels, - AccessMode accessMode, AttributeClass attributeClass) { - super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); + public DoubleAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { + super(coRELinkParam, attachment, accessMode, attributeClass); } @Override diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LongAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LongAttributeModel.java index a6d2da917a..016b319784 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LongAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LongAttributeModel.java @@ -24,9 +24,9 @@ */ public class LongAttributeModel extends LwM2mAttributeModel { - public LongAttributeModel(String coRELinkParam, Attachment attachment, Set assignationLevels, - AccessMode accessMode, AttributeClass attributeClass) { - super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); + public LongAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { + super(coRELinkParam, attachment, accessMode, attributeClass); } /** diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttribute.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttribute.java index 6a95f73513..733556471c 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttribute.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttribute.java @@ -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 diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributeModel.java index 2667c4e02f..5b3e9d9f4f 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributeModel.java @@ -30,16 +30,14 @@ */ public abstract class LwM2mAttributeModel extends AttributeModel> { - private final Attachment attachment; - private final Set assignationLevels; + private final Set attachment; private final AccessMode accessMode; private final AttributeClass attributeClass; - protected LwM2mAttributeModel(String coRELinkParam, Attachment attachment, Set assignationLevels, - AccessMode accessMode, AttributeClass attributeClass) { + protected LwM2mAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { super(coRELinkParam); this.attachment = attachment; - this.assignationLevels = assignationLevels; this.accessMode = accessMode; this.attributeClass = attributeClass; } @@ -56,11 +54,7 @@ public AccessMode getAccessMode() { return accessMode; } - public Set getAssignationLevels() { - return assignationLevels; - } - - public Attachment getAttachment() { + public Set getAttachment() { return attachment; } @@ -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); } /** @@ -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; } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributes.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributes.java index c7180dfa22..6e1b1d00b6 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributes.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributes.java @@ -32,8 +32,7 @@ public final class LwM2mAttributes { // dim public static final LwM2mAttributeModel DIMENSION = new PositiveLongAttributeModel(// "dim", // - Attachment.RESOURCE, // - EnumSet.of(AssignationLevel.RESOURCE), // + EnumSet.of(Attachment.RESOURCE), // AccessMode.R, // AttributeClass.PROPERTIES) { @Override @@ -63,8 +62,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) { // ssid public static final LwM2mAttributeModel SHORT_SERVER_ID = new PositiveLongAttributeModel(// "ssid", // - Attachment.OBJECT_INSTANCE, // - EnumSet.of(AssignationLevel.OBJECT_INSTANCE), // + EnumSet.of(Attachment.OBJECT_INSTANCE), // AccessMode.R, // AttributeClass.PROPERTIES) { @Override @@ -98,8 +96,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) { // uri public static final LwM2mAttributeModel SERVER_URI = new StringAttributeModel(// "uri", // - Attachment.OBJECT_INSTANCE, // - EnumSet.of(AssignationLevel.OBJECT_INSTANCE), // + EnumSet.of(Attachment.OBJECT_INSTANCE), // AccessMode.R, // AttributeClass.PROPERTIES) { @@ -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 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 @@ -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 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 @@ -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 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 @@ -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 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 @@ -247,8 +238,7 @@ public String getApplicabilityError(LwM2mPath path, LwM2mModel model) { // st public static final LwM2mAttributeModel 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 @@ -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 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 @@ -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 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 diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mVersionAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mVersionAttributeModel.java index 3db0414d83..781ce87415 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mVersionAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mVersionAttributeModel.java @@ -25,8 +25,7 @@ public class LwM2mVersionAttributeModel extends LwM2mAttributeModel 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> 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. * diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/ObjectVersionAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/ObjectVersionAttributeModel.java index f4082f2226..a47bad2dc3 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/ObjectVersionAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/ObjectVersionAttributeModel.java @@ -29,8 +29,7 @@ public class ObjectVersionAttributeModel extends LwM2mAttributeModel { public ObjectVersionAttributeModel() { super(// "ver", // - Attachment.OBJECT, // - EnumSet.of(AssignationLevel.OBJECT), // + EnumSet.of(Attachment.OBJECT), // AccessMode.R, // AttributeClass.PROPERTIES); } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java index 4f61a47566..d33b243218 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java @@ -27,9 +27,9 @@ */ public class PositiveDoubleAttributeModel extends LwM2mAttributeModel { - public PositiveDoubleAttributeModel(String coRELinkParam, Attachment attachment, - Set assignationLevels, AccessMode accessMode, AttributeClass attributeClass) { - super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); + public PositiveDoubleAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { + super(coRELinkParam, attachment, accessMode, attributeClass); } @Override diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java index 306d6a79bf..3485a21dbe 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java @@ -24,9 +24,9 @@ */ public class PositiveLongAttributeModel extends LwM2mAttributeModel { - public PositiveLongAttributeModel(String coRELinkParam, Attachment attachment, - Set assignationLevels, AccessMode accessMode, AttributeClass attributeClass) { - super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); + public PositiveLongAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { + super(coRELinkParam, attachment, accessMode, attributeClass); } @Override diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/StringAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/StringAttributeModel.java index c4b6c6fd67..5dfd0b3844 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/StringAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/StringAttributeModel.java @@ -25,9 +25,9 @@ */ public class StringAttributeModel extends LwM2mAttributeModel { - public StringAttributeModel(String coRELinkParam, Attachment attachment, Set assignationLevels, - AccessMode accessMode, AttributeClass attributeClass) { - super(coRELinkParam, attachment, assignationLevels, accessMode, attributeClass); + public StringAttributeModel(String coRELinkParam, Set attachment, AccessMode accessMode, + AttributeClass attributeClass) { + super(coRELinkParam, attachment, accessMode, attributeClass); } @Override diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeSetTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeSetTest.java index f7288700c1..25348e0d88 100644 --- a/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeSetTest.java +++ b/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeSetTest.java @@ -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; @@ -139,7 +139,7 @@ public void should_validate_assignation() { LwM2mAttributes.create(LwM2mAttributes.MAXIMUM_PERIOD, 60L)); Collection attributes = sut.asCollection(); assertEquals(2, attributes.size()); - sut.validate(AssignationLevel.RESOURCE); + sut.validate(Attachment.RESOURCE); } @Test @@ -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); }); } } diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeTest.java index 8f3a8926a8..d4cc9c93bc 100644 --- a/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeTest.java +++ b/leshan-core/src/test/java/org/eclipse/leshan/core/link/attributes/AttributeTest.java @@ -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; @@ -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()); } }