BUG 5656 : Entity ownership candidates not removed consistently on leadership change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DefaultShardDataTreeChangeListenerPublisher.java
1 /*
2  * Copyright (c) 2015 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.cluster.datastore;
9
10 import com.google.common.base.Stopwatch;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.md.sal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
16 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTreeChangePublisher;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Default implementation of ShardDataTreeChangeListenerPublisher that directly generates and publishes
26  * notifications for DataTreeChangeListeners.
27  *
28  * @author Thomas Pantelis
29  */
30 @NotThreadSafe
31 final class DefaultShardDataTreeChangeListenerPublisher extends AbstractDOMStoreTreeChangePublisher
32         implements ShardDataTreeChangeListenerPublisher {
33     private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataTreeChangeListenerPublisher.class);
34
35     private final Stopwatch timer = Stopwatch.createUnstarted();
36
37     @Override
38     public void publishChanges(final DataTreeCandidate candidate, String logContext) {
39         timer.start();
40
41         try {
42             processCandidateTree(candidate);
43         } finally {
44             timer.stop();
45             long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
46             if(elapsedTime >= PUBLISH_DELAY_THRESHOLD_IN_MS) {
47                 LOG.warn("{}: Generation of DataTreeCandidateNode events took longer than expected. Elapsed time: {}",
48                         logContext, timer);
49             } else {
50                 LOG.debug("{}: Elapsed time for generation of DataTreeCandidateNode events: {}", logContext, timer);
51             }
52
53             timer.reset();
54         }
55     }
56
57     @Override
58     public ShardDataTreeChangeListenerPublisher newInstance() {
59         return new DefaultShardDataTreeChangeListenerPublisher();
60     }
61
62     @Override
63     protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
64             final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
65         final Collection<DataTreeCandidate> changes = Collections.<DataTreeCandidate>singleton(
66                 DataTreeCandidates.newDataTreeCandidate(path, node));
67
68         for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
69             reg.getInstance().onDataTreeChanged(changes);
70         }
71     }
72
73     @Override
74     protected void registrationRemoved(final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
75         LOG.debug("Registration {} removed", registration);
76     }
77 }