Merge "Tests for DOMDataTreeListener"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DelayedDataTreeListenerRegistration.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.Preconditions;
11 import java.util.Collections;
12 import java.util.Map.Entry;
13 import javax.annotation.concurrent.GuardedBy;
14 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18
19 /**
20  * Intermediate proxy registration returned to the user when we cannot
21  * instantiate the registration immediately. It provides a bridge to
22  * a real registration which may materialize at some point in the future.
23  */
24 final class DelayedDataTreeListenerRegistration implements ListenerRegistration<DOMDataTreeChangeListener> {
25     private final RegisterDataTreeChangeListener registerTreeChangeListener;
26     private volatile ListenerRegistration<DOMDataTreeChangeListener> delegate;
27     @GuardedBy("this")
28     private boolean closed;
29
30     DelayedDataTreeListenerRegistration(final RegisterDataTreeChangeListener registerTreeChangeListener) {
31         this.registerTreeChangeListener = Preconditions.checkNotNull(registerTreeChangeListener);
32     }
33
34     synchronized void createDelegate(final DelegateFactory<RegisterDataTreeChangeListener, ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> factory) {
35         if (!closed) {
36             final Entry<ListenerRegistration<DOMDataTreeChangeListener>, DataTreeCandidate> res = factory.createDelegate(registerTreeChangeListener);
37             this.delegate = res.getKey();
38             if (res.getValue() != null) {
39                 delegate.getInstance().onDataTreeChanged(Collections.singletonList(res.getValue()));
40             }
41         }
42     }
43
44     @Override
45     public DOMDataTreeChangeListener getInstance() {
46         final ListenerRegistration<DOMDataTreeChangeListener> d = delegate;
47         return d == null ? null : d.getInstance();
48     }
49
50     @Override
51     public synchronized void close() {
52         if (!closed) {
53             closed = true;
54             if (delegate != null) {
55                 delegate.close();
56             }
57         }
58     }
59 }
60

©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.