2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.clustering.it.provider.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.Collection;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.atomic.AtomicLong;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class IdIntsListener implements ClusteredDOMDataTreeChangeListener {
29 private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
30 private static final long SECOND_AS_NANO = 1000000000;
32 private NormalizedNode<?, ?> localCopy = null;
33 private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
34 private ScheduledExecutorService executorService;
35 private ScheduledFuture<?> scheduledFuture;
38 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
40 // There should only be one candidate reported
41 Preconditions.checkState(changes.size() == 1);
43 lastNotifTimestamp.set(System.nanoTime());
45 // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
46 LOG.debug("Received data tree changed");
48 changes.forEach(change -> {
49 if (change.getRootNode().getDataAfter().isPresent()) {
50 LOG.trace("Received change, data before: {}, data after: ",
51 change.getRootNode().getDataBefore().isPresent()
52 ? change.getRootNode().getDataBefore().get() : "",
53 change.getRootNode().getDataAfter().get());
55 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
56 localCopy = change.getRootNode().getDataAfter().get();
58 LOG.warn("Ignoring notification.");
59 LOG.trace("Ignored notification content: {}", change);
62 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
67 public boolean hasTriggered() {
68 return localCopy != null;
71 public boolean checkEqual(final NormalizedNode<?, ?> expected) {
72 return localCopy.equals(expected);
75 public Future<Void> tryFinishProcessing() {
76 executorService = Executors.newSingleThreadScheduledExecutor();
77 final SettableFuture<Void> settableFuture = SettableFuture.create();
79 scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
80 0, 1, TimeUnit.SECONDS);
81 return settableFuture;
84 private class CheckFinishedTask implements Runnable {
86 private final SettableFuture<Void> future;
88 CheckFinishedTask(final SettableFuture<Void> future) {
94 if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
95 scheduledFuture.cancel(false);
98 executorService.shutdown();