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
8 package org.opendaylight.controller.clustering.it.provider.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.SettableFuture;
12 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 org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 @Deprecated(forRemoval = true)
29 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
31 private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
32 private static final long SECOND_AS_NANO = 1000000000;
34 private NormalizedNode<?, ?> localCopy = null;
35 private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
36 private ScheduledFuture<?> scheduledFuture;
37 private ScheduledExecutorService executorService;
40 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes,
41 final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
43 // There should only be one candidate reported
44 Preconditions.checkState(changes.size() == 1);
46 lastNotifTimestamp.set(System.nanoTime());
48 // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
49 LOG.debug("Received data tree changed");
51 changes.forEach(change -> {
52 if (change.getRootNode().getDataAfter().isPresent()) {
53 LOG.trace("Received change, data before: {}, data after: {}",
54 change.getRootNode().getDataBefore().isPresent()
55 ? change.getRootNode().getDataBefore().get() : "",
56 change.getRootNode().getDataAfter().get());
58 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
59 localCopy = change.getRootNode().getDataAfter().get();
61 LOG.warn("Ignoring notification.");
62 LOG.trace("Ignored notification content: {}", change);
65 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
71 public void onDataTreeFailed(final Collection<DOMDataTreeListeningException> causes) {
75 public boolean hasTriggered() {
76 return localCopy != null;
79 public Future<Void> tryFinishProcessing() {
80 executorService = Executors.newSingleThreadScheduledExecutor();
81 final SettableFuture<Void> settableFuture = SettableFuture.create();
83 scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
84 0, 1, TimeUnit.SECONDS);
85 return settableFuture;
88 public boolean checkEqual(final NormalizedNode<?, ?> expected) {
89 return localCopy.equals(expected);
92 private class CheckFinishedTask implements Runnable {
94 private final SettableFuture<Void> future;
96 CheckFinishedTask(final SettableFuture<Void> future) {
102 if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
103 scheduledFuture.cancel(false);
106 executorService.shutdown();