Merge "Bug 2820 - LLDP TLV support and testing"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DelayedListenerRegistration.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 org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
11 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
12 import org.opendaylight.yangtools.concepts.ListenerRegistration;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15
16 final class DelayedListenerRegistration implements
17     ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> {
18
19     private volatile boolean closed;
20
21     private final RegisterChangeListener registerChangeListener;
22
23     private volatile ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
24                                                          NormalizedNode<?, ?>>> delegate;
25
26     DelayedListenerRegistration(final RegisterChangeListener registerChangeListener) {
27         this.registerChangeListener = registerChangeListener;
28     }
29
30     void setDelegate( final ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
31                                         NormalizedNode<?, ?>>> registration) {
32         this.delegate = registration;
33     }
34
35     boolean isClosed() {
36         return closed;
37     }
38
39     RegisterChangeListener getRegisterChangeListener() {
40         return registerChangeListener;
41     }
42
43     @Override
44     public AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> getInstance() {
45         return delegate != null ? delegate.getInstance() : null;
46     }
47
48     @Override
49     public void close() {
50         closed = true;
51         if(delegate != null) {
52             delegate.close();
53         }
54     }
55 }