Bug 4327 - Fixed DataTreeChangeListener registration in PingPongDataBroker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / PingPongDataBroker.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
13 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.dom.spi.ForwardingDOMDataBroker;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19
20 /**
21  * An implementation of a {@link DOMDataBroker}, which forwards most requests to a delegate.
22  *
23  * Its interpretation of the API contract is somewhat looser, specifically it does not
24  * guarantee transaction ordering between transactions allocated directly from the broker
25  * and its transaction chains.
26  */
27 public final class PingPongDataBroker extends ForwardingDOMDataBroker implements AutoCloseable, DOMDataTreeChangeService {
28     private final DOMDataBroker delegate;
29
30     /**
31      * Instantiate a new broker, backed by the the specified delegate
32      * {@link DOMDataBroker}.
33      *
34      * @param delegate Backend broker, may not be null.
35      */
36     public PingPongDataBroker(@Nonnull final DOMDataBroker delegate) {
37         this.delegate = Preconditions.checkNotNull(delegate);
38     }
39
40     @Override
41     protected DOMDataBroker delegate() {
42         return delegate;
43     }
44
45     @Override
46     public PingPongTransactionChain createTransactionChain(final TransactionChainListener listener) {
47         return new PingPongTransactionChain(delegate, listener);
48     }
49
50     @Override
51     public void close() {
52         // intentionally NOOP
53     }
54
55     @Override
56     public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(final DOMDataTreeIdentifier treeId, final L listener) {
57         final DOMDataTreeChangeService treeService =
58                 (DOMDataTreeChangeService) delegate.getSupportedExtensions().get(DOMDataTreeChangeService.class);
59         if (treeService != null) {
60             return treeService.registerDataTreeChangeListener(treeId, listener);
61         }
62
63         throw new UnsupportedOperationException("Delegate " + delegate + " does not support required functionality");
64     }
65 }

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