Reduce JSR305 proliferation
[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 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
29
30     private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
31     private static final long SECOND_AS_NANO = 1000000000;
32
33     private NormalizedNode<?, ?> localCopy = null;
34     private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
35     private ScheduledFuture<?> scheduledFuture;
36     private ScheduledExecutorService executorService;
37
38     @Override
39     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes,
40                                   final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
41
42         // There should only be one candidate reported
43         Preconditions.checkState(changes.size() == 1);
44
45         lastNotifTimestamp.set(System.nanoTime());
46
47         // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
48         LOG.debug("Received data tree changed");
49
50         changes.forEach(change -> {
51             if (change.getRootNode().getDataAfter().isPresent()) {
52                 LOG.trace("Received change, data before: {}, data after: {}",
53                         change.getRootNode().getDataBefore().isPresent()
54                                 ? change.getRootNode().getDataBefore().get() : "",
55                         change.getRootNode().getDataAfter().get());
56
57                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
58                     localCopy = change.getRootNode().getDataAfter().get();
59                 } else {
60                     LOG.warn("Ignoring notification.");
61                     LOG.trace("Ignored notification content: {}", change);
62                 }
63             } else {
64                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
65             }
66         });
67     }
68
69     @Override
70     public void onDataTreeFailed(Collection<DOMDataTreeListeningException> causes) {
71
72     }
73
74     public boolean hasTriggered() {
75         return localCopy != null;
76     }
77
78     public Future<Void> tryFinishProcessing() {
79         executorService = Executors.newSingleThreadScheduledExecutor();
80         final SettableFuture<Void> settableFuture = SettableFuture.create();
81
82         scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
83                 0, 1, TimeUnit.SECONDS);
84         return settableFuture;
85     }
86
87     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
88         return localCopy.equals(expected);
89     }
90
91     private class CheckFinishedTask implements Runnable {
92
93         private final SettableFuture<Void> future;
94
95         CheckFinishedTask(final SettableFuture<Void> future) {
96             this.future = future;
97         }
98
99         @Override
100         public void run() {
101             if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
102                 scheduledFuture.cancel(false);
103                 future.set(null);
104
105                 executorService.shutdown();
106             }
107         }
108     }
109 }