X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Fclustering-test-app%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fclustering%2Fit%2Fprovider%2Fimpl%2FIdIntsDOMDataTreeLIstener.java;h=3c88f991755c10f40e83128a7a91460f450cf483;hp=3f9a3c52f6a7426d05a700dd7e6ac7056c137fb9;hb=a35607c5040d0fd561529fde3032c9f49393deeb;hpb=64dbc396e21d73a7323b3e9dbf51b31df295cfb5 diff --git a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/IdIntsDOMDataTreeLIstener.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/IdIntsDOMDataTreeLIstener.java index 3f9a3c52f6..3c88f99175 100644 --- a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/IdIntsDOMDataTreeLIstener.java +++ b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/IdIntsDOMDataTreeLIstener.java @@ -5,13 +5,18 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.clustering.it.provider.impl; import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.SettableFuture; import java.util.Collection; import java.util.Map; -import javax.annotation.Nonnull; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; import org.opendaylight.mdsal.dom.api.DOMDataTreeListener; import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException; @@ -20,25 +25,32 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Deprecated(forRemoval = true) public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener { private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class); + private static final long SECOND_AS_NANO = 1000000000; private NormalizedNode localCopy = null; + private final AtomicLong lastNotifTimestamp = new AtomicLong(0); + private ScheduledFuture scheduledFuture; + private ScheduledExecutorService executorService; @Override - public void onDataTreeChanged(@Nonnull final Collection changes, - @Nonnull final Map> subtrees) { + public void onDataTreeChanged(final Collection changes, + final Map> subtrees) { // There should only be one candidate reported Preconditions.checkState(changes.size() == 1); + lastNotifTimestamp.set(System.nanoTime()); + // do not log the change into debug, only use trace since it will lead to OOM on default heap settings LOG.debug("Received data tree changed"); changes.forEach(change -> { if (change.getRootNode().getDataAfter().isPresent()) { - LOG.trace("Received change, data before: {}, data after: ", + LOG.trace("Received change, data before: {}, data after: {}", change.getRootNode().getDataBefore().isPresent() ? change.getRootNode().getDataBefore().get() : "", change.getRootNode().getDataAfter().get()); @@ -46,7 +58,8 @@ public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener { if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) { localCopy = change.getRootNode().getDataAfter().get(); } else { - LOG.debug("Ignoring notification: {}", change); + LOG.warn("Ignoring notification."); + LOG.trace("Ignored notification content: {}", change); } } else { LOG.warn("getDataAfter() is missing from notification. change: {}", change); @@ -55,11 +68,43 @@ public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener { } @Override - public void onDataTreeFailed(@Nonnull Collection causes) { + public void onDataTreeFailed(final Collection causes) { + + } + + public boolean hasTriggered() { + return localCopy != null; + } + + public Future tryFinishProcessing() { + executorService = Executors.newSingleThreadScheduledExecutor(); + final SettableFuture settableFuture = SettableFuture.create(); + scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture), + 0, 1, TimeUnit.SECONDS); + return settableFuture; } public boolean checkEqual(final NormalizedNode expected) { return localCopy.equals(expected); } + + private class CheckFinishedTask implements Runnable { + + private final SettableFuture future; + + CheckFinishedTask(final SettableFuture future) { + this.future = future; + } + + @Override + public void run() { + if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) { + scheduledFuture.cancel(false); + future.set(null); + + executorService.shutdown(); + } + } + } }