Add explicit init/close methods to OpendaylightToaster 57/57057/3
authorTom Pantelis <tompantelis@gmail.com>
Mon, 15 May 2017 12:21:34 +0000 (08:21 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Tue, 16 May 2017 10:20:51 +0000 (10:20 +0000)
The blueprint XML specifies the "register" and "unregister" methods
for init/destroy however this bypasses the "close" method. Add
explicit init/close methods and call register/unregister.

I also moved the initialization code that was in setDataBroker to the
new init method. The former is a setter and should have code logic side
effects.

Change-Id: Idf46f76913c4400aaef61ff78ad8f57e3db4c4b9
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java
opendaylight/md-sal/samples/toaster-provider/src/main/resources/org/opendaylight/blueprint/toaster-provider.xml
opendaylight/md-sal/samples/toaster-provider/src/test/java/org/opendaylight/controller/sample/toaster/provider/OpenDaylightToasterTest.java

index a7c9a7818535151b87db7d7f30193ebce6811c10..e475c320d1e11c45258ebb6415083589fcf0302d 100644 (file)
@@ -15,6 +15,7 @@ import static org.opendaylight.yangtools.yang.common.RpcError.ErrorType.APPLICAT
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.AsyncFunction;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
@@ -22,7 +23,6 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collection;
 import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -102,32 +102,49 @@ public class OpendaylightToaster extends AbstractMXBean
 
     public void setDataBroker(final DataBroker dataBroker) {
         this.dataBroker = dataBroker;
-        dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(CONFIGURATION, TOASTER_IID), this);
+    }
+
+    public void init() {
+        LOG.info("Initializing...");
+
+        Preconditions.checkNotNull(dataBroker, "dataBroker must be set");
+        dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
+                new DataTreeIdentifier<>(CONFIGURATION, TOASTER_IID), this);
         setToasterStatusUp(null);
+
+        // Register our MXBean.
+        register();
     }
 
     /**
      * Implemented from the AutoCloseable interface.
      */
     @Override
-    public void close() throws ExecutionException, InterruptedException {
+    public void close() {
+        LOG.info("Closing...");
+
+        // Unregister our MXBean.
+        unregister();
+
         // When we close this service we need to shutdown our executor!
         executor.shutdown();
 
-        if (dataBroker != null) {
+        if (dataTreeChangeListenerRegistration != null) {
             dataTreeChangeListenerRegistration.close();
+        }
 
+        if (dataBroker != null) {
             WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
             tx.delete(OPERATIONAL,TOASTER_IID);
             Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
                 @Override
                 public void onSuccess(final Void result) {
-                    LOG.debug("Delete Toaster commit result: " + result);
+                    LOG.debug("Successfully deleted the operational Toaster");
                 }
 
                 @Override
                 public void onFailure(final Throwable failure) {
-                    LOG.error("Delete of Toaster failed", failure);
+                    LOG.error("Delete of the operational Toaster failed", failure);
                 }
             });
         }
@@ -327,6 +344,7 @@ public class OpendaylightToaster extends AbstractMXBean
         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
             @Override
             public void onSuccess(final Void result) {
+                LOG.info("Successfully set ToasterStatus to Up");
                 notifyCallback(true);
             }
 
index d58c26a792e10391f04ac258bc90f27d7a5a397d..892539b652ac7b446bfd6026e3c192ed3dbb2652 100644 (file)
@@ -51,7 +51,7 @@
 
   <!-- Create the OpendaylightToaster instance and inject its dependencies -->
   <bean id="toaster" class="org.opendaylight.controller.sample.toaster.provider.OpendaylightToaster"
-          init-method="register" destroy-method="unregister">
+          init-method="init" destroy-method="close">
     <argument ref="toasterAppConfig"/>
     <property name="dataBroker" ref="dataBroker"/>
     <property name="notificationProvider" ref="notificationService"/>
index c6f0dc1d3f615b8159337ed22c891216dbcd2460..23a51e1831127e17b78fc6ce28177116757e689a 100644 (file)
@@ -16,12 +16,13 @@ import static org.mockito.Mockito.mock;
 
 import com.google.common.base.Optional;
 import java.util.concurrent.Future;
+import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
+import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
@@ -31,20 +32,18 @@ import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
-public class OpenDaylightToasterTest extends AbstractDataBrokerTest {
+public class OpenDaylightToasterTest extends AbstractConcurrentDataBrokerTest {
 
     private static InstanceIdentifier<Toaster> TOASTER_IID = InstanceIdentifier.builder(Toaster.class).build();
-    OpendaylightToaster toaster;
+    private OpendaylightToaster toaster;
 
-    @Override
-    protected void setupWithDataBroker(DataBroker dataBroker) {
+    @Before
+    public void setupToaster() {
         toaster = new OpendaylightToaster();
-        toaster.setDataBroker(dataBroker);
+        toaster.setDataBroker(getDataBroker());
+        toaster.init();
 
-        /**
-         * Doesn't look like we have support for the NotificationProviderService yet, so mock it
-         * for now.
-         */
+        // We'll mock the NotificationProviderService.
         NotificationPublishService mockNotification = mock(NotificationPublishService.class);
         toaster.setNotificationProvider(mockNotification);
     }