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;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicLong;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
32 private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
33 private static final long SECOND_AS_NANO = 1000000000;
35 private NormalizedNode<?, ?> localCopy = null;
36 private AtomicLong lastNotifTimestamp = new AtomicLong(0);
37 private ScheduledFuture<?> scheduledFuture;
38 private ScheduledExecutorService executorService;
41 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes,
42 @Nonnull final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
44 // There should only be one candidate reported
45 Preconditions.checkState(changes.size() == 1);
47 lastNotifTimestamp.set(System.nanoTime());
49 // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
50 LOG.debug("Received data tree changed");
52 changes.forEach(change -> {
53 if (change.getRootNode().getDataAfter().isPresent()) {
54 LOG.trace("Received change, data before: {}, data after: {}",
55 change.getRootNode().getDataBefore().isPresent()
56 ? change.getRootNode().getDataBefore().get() : "",
57 change.getRootNode().getDataAfter().get());
59 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
60 localCopy = change.getRootNode().getDataAfter().get();
62 LOG.warn("Ignoring notification.");
63 LOG.trace("Ignored notification content: {}", change);
66 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
72 public void onDataTreeFailed(@Nonnull Collection<DOMDataTreeListeningException> causes) {
76 public boolean hasTriggered() {
77 return localCopy != null;
80 public Future<Void> tryFinishProcessing() {
81 executorService = Executors.newSingleThreadScheduledExecutor();
82 final SettableFuture<Void> settableFuture = SettableFuture.create();
84 scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture), 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();