Repository: eclipse/leshan
Pull request #641 (2019-01-17 14:18:00)

Add callback on instance deletion at client side
This PR aims to implement #637.


Patch:
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/BaseInstanceEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/BaseInstanceEnabler.java
index 7c3126e5..cbf3c8a5 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/BaseInstanceEnabler.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/BaseInstanceEnabler.java
@@ -150,6 +150,11 @@ public class BaseInstanceEnabler implements LwM2mInstanceEnabler {
                 readResponse.getErrorMessage());
     }
 
+    @Override
+    public void onDelete(ServerIdentity identity) {
+        // No default behavior
+    }
+
     @Override
     public void reset(int resourceid) {
         // No default behavior
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mInstanceEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mInstanceEnabler.java
index 1d46a8db..ca317c0e 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mInstanceEnabler.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/LwM2mInstanceEnabler.java
@@ -166,6 +166,14 @@ public interface LwM2mInstanceEnabler {
      */
     ObserveResponse observe(ServerIdentity identity, int resourceid);
 
+    /**
+     * A callback called when this instance is deleted
+     * 
+     * @param identity the identity of the requester. This could be an internal call in this case
+     *        <code> identity == ServerIdentity.SYSTEM</code>.
+     */
+    void onDelete(ServerIdentity identity);
+
     /**
      * @param objectModel the model of this instance
      * @return the list of the implemented resources of this instances mainly used for discover operation
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java
index 6bdce349..81d4d78e 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/ObjectEnabler.java
@@ -263,7 +263,9 @@ public class ObjectEnabler extends BaseObjectEnabler {
 
     @Override
     protected DeleteResponse doDelete(ServerIdentity identity, DeleteRequest request) {
-        if (null != instances.remove(request.getPath().getObjectInstanceId())) {
+        LwM2mInstanceEnabler deletedInstance = instances.remove(request.getPath().getObjectInstanceId());
+        if (deletedInstance != null) {
+            deletedInstance.onDelete(identity);
             return DeleteResponse.success();
         }
         return DeleteResponse.notFound();
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/SimpleInstanceEnabler.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/SimpleInstanceEnabler.java
index c2857bb8..3bd979ad 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/SimpleInstanceEnabler.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/resource/SimpleInstanceEnabler.java
@@ -72,6 +72,11 @@ public class SimpleInstanceEnabler extends BaseInstanceEnabler {
         }
     }
 
+    @Override
+    public void onDelete(ServerIdentity identity) {
+        LOG.info("Instance {} from object {} ({}) deleted.", getId(), getModel().name, getModel().id);
+    }
+
     @Override
     public void reset(int resourceid) {
         resources.remove(resourceid);


Test patch:
diff --git a/leshan-client-core/src/test/java/org/eclipse/leshan/client/util/ObjectEnablerTest.java b/leshan-client-core/src/test/java/org/eclipse/leshan/client/util/ObjectEnablerTest.java
new file mode 100644
index 00000000..15a2933f
--- /dev/null
+++ b/leshan-client-core/src/test/java/org/eclipse/leshan/client/util/ObjectEnablerTest.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2015 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
+ *     Achim Kraus (Bosch Software Innovations GmbH) - use ServerIdentity
+ *     Achim Kraus (Bosch Software Innovations GmbH) - implement REPLACE/UPDATE
+ *******************************************************************************/
+package org.eclipse.leshan.client.util;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.leshan.LwM2mId;
+import org.eclipse.leshan.client.request.ServerIdentity;
+import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
+import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
+import org.eclipse.leshan.client.resource.ObjectsInitializer;
+import org.eclipse.leshan.core.request.DeleteRequest;
+import org.junit.Test;
+
+public class ObjectEnablerTest {
+
+    @Test
+    public void check_callback_on_delete() throws InterruptedException {
+        ObjectsInitializer initializer = new ObjectsInitializer();
+        TestInstanceEnabler instanceEnabler = new TestInstanceEnabler();
+        initializer.setInstancesForObject(LwM2mId.ACCESS_CONTROL, instanceEnabler);
+        LwM2mObjectEnabler objectEnabler = initializer.create(LwM2mId.ACCESS_CONTROL);
+
+        objectEnabler.delete(ServerIdentity.SYSTEM, new DeleteRequest(LwM2mId.ACCESS_CONTROL, instanceEnabler.getId()));
+        assertTrue("callback delete should have been called", instanceEnabler.waitForDelete(2, TimeUnit.SECONDS));
+    }
+
+    private class TestInstanceEnabler extends BaseInstanceEnabler {
+
+        CountDownLatch onDelete = new CountDownLatch(1);
+
+        @Override
+        public void onDelete(ServerIdentity identity) {
+            onDelete.countDown();
+        }
+
+        public boolean waitForDelete(long timeout, TimeUnit unit) throws InterruptedException {
+            return onDelete.await(timeout, unit);
+        }
+    }
+}