From 05522e0190cec637f190ceb5aedf785039f08e57 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 19 Jul 2018 16:44:30 -0400 Subject: [PATCH] Convert toaster to mdsal APIs Change-Id: I5cbbd42bd3435686346a7945881dd0d31f13a796 Signed-off-by: Tom Pantelis --- .../md-sal/samples/toaster-provider/pom.xml | 12 ++-- .../toaster/provider/OpendaylightToaster.java | 58 ++++++++++--------- .../blueprint/toaster-provider.xml | 4 +- .../provider/OpenDaylightToasterTest.java | 14 ++--- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/opendaylight/md-sal/samples/toaster-provider/pom.xml b/opendaylight/md-sal/samples/toaster-provider/pom.xml index c1ba0ded63..d30b20bc4e 100644 --- a/opendaylight/md-sal/samples/toaster-provider/pom.xml +++ b/opendaylight/md-sal/samples/toaster-provider/pom.xml @@ -20,8 +20,8 @@ ${project.version} - org.opendaylight.controller - sal-binding-api + org.opendaylight.mdsal + mdsal-binding-api org.opendaylight.controller @@ -34,13 +34,13 @@ - org.opendaylight.controller - sal-binding-broker-impl + org.opendaylight.mdsal + mdsal-binding-dom-adapter test - org.opendaylight.controller - sal-binding-broker-impl + org.opendaylight.mdsal + mdsal-binding-dom-adapter test-jar test diff --git a/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java b/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java index cbac02cc96..0fcd474d9c 100644 --- a/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java +++ b/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java @@ -7,38 +7,40 @@ */ package org.opendaylight.controller.sample.toaster.provider; -import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.DELETE; -import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.WRITE; -import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION; -import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL; +import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.DELETE; +import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.WRITE; +import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION; +import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL; import static org.opendaylight.yangtools.yang.common.RpcError.ErrorType.APPLICATION; import com.google.common.base.Function; -import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.FluentFuture; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.util.Collection; +import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; -import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; -import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; -import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; -import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; -import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; -import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataObjectModification; +import org.opendaylight.mdsal.binding.api.DataTreeChangeListener; +import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; +import org.opendaylight.mdsal.binding.api.DataTreeModification; +import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.opendaylight.mdsal.binding.api.ReadWriteTransaction; +import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.CommitInfo; +import org.opendaylight.mdsal.common.api.OptimisticLockFailedException; +import org.opendaylight.mdsal.common.api.TransactionCommitFailedException; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.CancelToastInput; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.CancelToastOutput; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.CancelToastOutputBuilder; @@ -120,7 +122,7 @@ public class OpendaylightToaster extends AbstractMXBean Preconditions.checkNotNull(dataBroker, "dataBroker must be set"); dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener( - new DataTreeIdentifier<>(CONFIGURATION, TOASTER_IID), this); + DataTreeIdentifier.create(CONFIGURATION, TOASTER_IID), this); setToasterStatusUp(null); // Register our MXBean. @@ -147,9 +149,9 @@ public class OpendaylightToaster extends AbstractMXBean if (dataBroker != null) { WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); tx.delete(OPERATIONAL,TOASTER_IID); - Futures.addCallback(tx.submit(), new FutureCallback() { + Futures.addCallback(tx.commit(), new FutureCallback() { @Override - public void onSuccess(final Void result) { + public void onSuccess(final CommitInfo result) { LOG.debug("Successfully deleted the operational Toaster"); } @@ -237,9 +239,9 @@ public class OpendaylightToaster extends AbstractMXBean // If that succeeds, then we essentially have an exclusive lock and can proceed // to make toast. final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction(); - ListenableFuture> readFuture = tx.read(OPERATIONAL, TOASTER_IID); + FluentFuture> readFuture = tx.read(OPERATIONAL, TOASTER_IID); - final ListenableFuture commitFuture = + final ListenableFuture commitFuture = Futures.transformAsync(readFuture, toasterData -> { ToasterStatus toasterStatus = ToasterStatus.Up; if (toasterData.isPresent()) { @@ -252,7 +254,8 @@ public class OpendaylightToaster extends AbstractMXBean if (outOfBread()) { LOG.debug("Toaster is out of bread"); - return Futures.immediateFailedCheckedFuture( + tx.cancel(); + return Futures.immediateFailedFuture( new TransactionCommitFailedException("", makeToasterOutOfBreadError())); } @@ -262,7 +265,7 @@ public class OpendaylightToaster extends AbstractMXBean // to indicate we're going to make toast. This acts as a lock to prevent // concurrent toasting. tx.put(OPERATIONAL, TOASTER_IID, buildToaster(ToasterStatus.Down)); - return tx.submit(); + return tx.commit(); } LOG.debug("Oops - already making toast!"); @@ -270,13 +273,14 @@ public class OpendaylightToaster extends AbstractMXBean // Return an error since we are already making toast. This will get // propagated to the commitFuture below which will interpret the null // TransactionStatus in the RpcResult as an error condition. - return Futures.immediateFailedCheckedFuture( + tx.cancel(); + return Futures.immediateFailedFuture( new TransactionCommitFailedException("", makeToasterInUseError())); }, MoreExecutors.directExecutor()); - Futures.addCallback(commitFuture, new FutureCallback() { + Futures.addCallback(commitFuture, new FutureCallback() { @Override - public void onSuccess(final Void result) { + public void onSuccess(final CommitInfo result) { // OK to make toast currentMakeToastTask.set(executor.submit(new MakeToastTask(input, futureResult))); } @@ -352,9 +356,9 @@ public class OpendaylightToaster extends AbstractMXBean WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); tx.put(OPERATIONAL,TOASTER_IID, buildToaster(ToasterStatus.Up)); - Futures.addCallback(tx.submit(), new FutureCallback() { + Futures.addCallback(tx.commit(), new FutureCallback() { @Override - public void onSuccess(final Void result) { + public void onSuccess(final CommitInfo result) { LOG.info("Successfully set ToasterStatus to Up"); notifyCallback(true); } diff --git a/opendaylight/md-sal/samples/toaster-provider/src/main/resources/org/opendaylight/blueprint/toaster-provider.xml b/opendaylight/md-sal/samples/toaster-provider/src/main/resources/org/opendaylight/blueprint/toaster-provider.xml index 892539b652..4c7af5b635 100644 --- a/opendaylight/md-sal/samples/toaster-provider/src/main/resources/org/opendaylight/blueprint/toaster-provider.xml +++ b/opendaylight/md-sal/samples/toaster-provider/src/main/resources/org/opendaylight/blueprint/toaster-provider.xml @@ -46,8 +46,8 @@ via the cfg file. In this manner the toaster can be configured to use the default clustered DataBroker or the specialized "pingpong" DataBroker (or any other DataBroker implementation). --> - - + + optional = readTx.read(LogicalDatastoreType.OPERATIONAL, TOASTER_IID).get(); assertNotNull(optional); assertTrue("Operational toaster not present", optional.isPresent()); -- 2.36.6