f5b55fd0775eaf3f3442fcc98de9c06d8b467ba1
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / IdIntsDOMDataTreeLIstener.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.Map;
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;
29
30 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
31
32     private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
33     private static final long SECOND_AS_NANO = 1000000000;
34
35     private NormalizedNode<?, ?> localCopy = null;
36     private AtomicLong lastNotifTimestamp = new AtomicLong(0);
37     private ScheduledFuture<?> scheduledFuture;
38     private ScheduledExecutorService executorService;
39
40     @Override
41     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes,
42                                   @Nonnull final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
43
44         // There should only be one candidate reported
45         Preconditions.checkState(changes.size() == 1);
46
47         lastNotifTimestamp.set(System.nanoTime());
48
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");
51
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());
58
59                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
60                     localCopy = change.getRootNode().getDataAfter().get();
61                 } else {
62                     LOG.warn("Ignoring notification.");
63                     LOG.trace("Ignored notification content: {}", change);
64                 }
65             } else {
66                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
67             }
68         });
69     }
70
71     @Override
72     public void onDataTreeFailed(@Nonnull Collection<DOMDataTreeListeningException> causes) {
73
74     }
75
76     public boolean hasTriggered() {
77         return localCopy != null;
78     }
79
80     public Future<Void> tryFinishProcessing() {
81         executorService = Executors.newSingleThreadScheduledExecutor();
82         final SettableFuture<Void> settableFuture = SettableFuture.create();
83
84         scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture), 0, 1, TimeUnit.SECONDS);
85         return settableFuture;
86     }
87
88     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
89         return localCopy.equals(expected);
90     }
91
92     private class CheckFinishedTask implements Runnable {
93
94         private final SettableFuture<Void> future;
95
96         CheckFinishedTask(final SettableFuture<Void> future) {
97             this.future = future;
98         }
99
100         @Override
101         public void run() {
102             if (System.nanoTime() - lastNotifTimestamp.get() > (SECOND_AS_NANO * 4)) {
103                 scheduledFuture.cancel(false);
104                 future.set(null);
105
106                 executorService.shutdown();
107             }
108         }
109     }
110
111 }