BUG 8629: Try to allow notification processing to finish in unsubscribe of listeners.
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / IdIntsListener.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
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;
26
27 public class IdIntsListener implements ClusteredDOMDataTreeChangeListener {
28
29     private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
30     private static final long SECOND_AS_NANO = 1000000000;
31
32     private NormalizedNode<?, ?> localCopy = null;
33     private AtomicLong lastNotifTimestamp = new AtomicLong(0);
34     private ScheduledExecutorService executorService;
35     private ScheduledFuture<?> scheduledFuture;
36
37     @Override
38     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
39
40         // There should only be one candidate reported
41         Preconditions.checkState(changes.size() == 1);
42
43         lastNotifTimestamp.set(System.nanoTime());
44
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");
47
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());
54
55                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
56                     localCopy = change.getRootNode().getDataAfter().get();
57                 } else {
58                     LOG.warn("Ignoring notification.");
59                     LOG.trace("Ignored notification content: {}", change);
60                 }
61             } else {
62                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
63             }
64         });
65     }
66
67     public boolean hasTriggered() {
68         return localCopy != null;
69     }
70
71     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
72         return localCopy.equals(expected);
73     }
74
75     public Future<Void> tryFinishProcessing() {
76         executorService = Executors.newSingleThreadScheduledExecutor();
77         final SettableFuture<Void> settableFuture = SettableFuture.create();
78
79         scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture), 0, 1, TimeUnit.SECONDS);
80         return settableFuture;
81     }
82
83     private class CheckFinishedTask implements Runnable {
84
85         private final SettableFuture<Void> future;
86
87         public CheckFinishedTask(final SettableFuture<Void> future) {
88             this.future = future;
89         }
90
91         @Override
92         public void run() {
93             if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
94                 scheduledFuture.cancel(false);
95                 future.set(null);
96
97                 executorService.shutdown();
98             }
99         }
100     }
101 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.