Repository: eclipse/leshan
Pull request #1029 (2021-06-17 18:27:38)

#1026 epmin, epmax support
#1026 Evaluation attributes support

Signed-off-by: Danil Ruban intx82@gmail.com


Patch:
diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/Attribute.java b/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/Attribute.java
index 4d20f03d..afe80603 100644
--- a/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/Attribute.java
+++ b/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/Attribute.java
@@ -38,6 +38,8 @@ public class Attribute {
     public static final String GREATER_THAN = "gt";
     public static final String LESSER_THAN = "lt";
     public static final String STEP = "st";
+    public static final String EVALUATE_MINIMUM_PERIOD = "epmin";
+    public static final String EVALUATE_MAXIMUM_PERIOD = "epmax";
 
     /**
      * Metadata container for LwM2m attributes
@@ -81,6 +83,14 @@ public class Attribute {
                 EnumSet.of(AssignationLevel.RESOURCE), AccessMode.RW, Double.class));
         modelMap.put(STEP, new AttributeModel(STEP, Attachment.RESOURCE, EnumSet.of(AssignationLevel.RESOURCE),
                 AccessMode.RW, Double.class));
+        modelMap.put(EVALUATE_MINIMUM_PERIOD,
+                new Attribute.AttributeModel(EVALUATE_MINIMUM_PERIOD, Attachment.RESOURCE,
+                        EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.INSTANCE, AssignationLevel.RESOURCE),
+                        AccessMode.RW, Long.class));
+        modelMap.put(EVALUATE_MAXIMUM_PERIOD,
+                new Attribute.AttributeModel(EVALUATE_MAXIMUM_PERIOD, Attachment.RESOURCE,
+                        EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.INSTANCE, AssignationLevel.RESOURCE),
+                        AccessMode.RW, Long.class));
     }
 
     private final AttributeModel model;
diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/AttributeSet.java b/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/AttributeSet.java
index f06b4151..3fe7b9eb 100644
--- a/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/AttributeSet.java
+++ b/leshan-core/src/main/java/org/eclipse/leshan/core/attributes/AttributeSet.java
@@ -65,6 +65,13 @@ public class AttributeSet {
             throw new IllegalArgumentException(String.format("Cannot write attributes where '%s' > '%s'",
                     pmin.getCoRELinkParam(), pmax.getCoRELinkParam()));
         }
+
+        Attribute epmin = attributeMap.get(Attribute.EVALUATE_MINIMUM_PERIOD);
+        Attribute epmax = attributeMap.get(Attribute.EVALUATE_MAXIMUM_PERIOD);
+        if ((epmin != null) && (epmax != null) && (Long) epmin.getValue() > (Long) epmax.getValue()) {
+            throw new IllegalArgumentException(String.format("Cannot write attributes where '%s' > '%s'",
+                    epmin.getCoRELinkParam(), epmax.getCoRELinkParam()));
+        }
     }
 
     /**


Test patch:
diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/attributes/AttributeSetTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/attributes/AttributeSetTest.java
index 8ce2ef29..c0f7a780 100644
--- a/leshan-core/src/test/java/org/eclipse/leshan/core/attributes/AttributeSetTest.java
+++ b/leshan-core/src/test/java/org/eclipse/leshan/core/attributes/AttributeSetTest.java
@@ -28,8 +28,9 @@ public class AttributeSetTest {
     @Test
     public void should_provide_query_params() {
         AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
-                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
-        assertEquals("ver=1.1&pmin=5&pmax=60", sut.toString());
+                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
+                new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));
+        assertEquals("ver=1.1&pmin=5&pmax=60&epmin=30&epmax=45", sut.toString());
 
         AttributeSet res = AttributeSet.parse(sut.toString());
         assertEquals(sut, res);
@@ -38,8 +39,9 @@ public class AttributeSetTest {
     @Test
     public void no_value_to_unset() {
         AttributeSet sut = new AttributeSet(new Attribute(Attribute.MINIMUM_PERIOD),
-                new Attribute(Attribute.MAXIMUM_PERIOD));
-        assertEquals("pmin&pmax", sut.toString());
+                new Attribute(Attribute.MAXIMUM_PERIOD), new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD),
+                new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD));
+        assertEquals("pmin&pmax&epmin&epmax", sut.toString());
 
         AttributeSet res = AttributeSet.parse(sut.toString());
         assertEquals(sut, res);
@@ -48,11 +50,14 @@ public class AttributeSetTest {
     @Test
     public void should_get_map() {
         AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
-                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
+                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
+                new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));
         Map<String, Object> map = sut.getMap();
         assertEquals("1.1", map.get("ver"));
         assertEquals(5L, map.get("pmin"));
         assertEquals(60L, map.get("pmax"));
+        assertEquals(45L, map.get("epmax"));
+        assertEquals(30L, map.get("epmin"));
     }
 
     @Test
@@ -79,9 +84,10 @@ public class AttributeSetTest {
     @Test
     public void should_to_string() {
         AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
-                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
+                new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
+                new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));
 
-        assertEquals("ver=1.1&pmin=5&pmax=60", sut.toString());
+        assertEquals("ver=1.1&pmin=5&pmax=60&epmin=30&epmax=45", sut.toString());
     }
 
     @Test(expected = IllegalArgumentException.class)
@@ -116,4 +122,13 @@ public class AttributeSetTest {
         // pmin cannot be greater then pmax
         sut.validate(AssignationLevel.RESOURCE);
     }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void should_throw_on_invalid_epmin_epmax() {
+        AttributeSet sut = new AttributeSet(new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 50L),
+                new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 49L));
+
+        // pmin cannot be greater then pmax
+        sut.validate(AssignationLevel.RESOURCE);
+    }
 }