3c88f991755c10f40e83128a7a91460f450cf483
[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 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.Collection;
13 import java.util.Map;
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;
27
28 @Deprecated(forRemoval = true)
29 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
30
31     private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
32     private static final long SECOND_AS_NANO = 1000000000;
33
34     private NormalizedNode<?, ?> localCopy = null;
35     private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
36     private ScheduledFuture<?> scheduledFuture;
37     private ScheduledExecutorService executorService;
38
39     @Override
40     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes,
41                                   final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
42
43         // There should only be one candidate reported
44         Preconditions.checkState(changes.size() == 1);
45
46         lastNotifTimestamp.set(System.nanoTime());
47
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");
50
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());
57
58                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
59                     localCopy = change.getRootNode().getDataAfter().get();
60                 } else {
61                     LOG.warn("Ignoring notification.");
62                     LOG.trace("Ignored notification content: {}", change);
63                 }
64             } else {
65                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
66             }
67         });
68     }
69
70     @Override
71     public void onDataTreeFailed(final Collection<DOMDataTreeListeningException> causes) {
72
73     }
74
75     public boolean hasTriggered() {
76         return localCopy != null;
77     }
78
79     public Future<Void> tryFinishProcessing() {
80         executorService = Executors.newSingleThreadScheduledExecutor();
81         final SettableFuture<Void> settableFuture = SettableFuture.create();
82
83         scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
84                 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 }