d94b6abbd88e7480f2d92468ac084589fef80a4e
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMDataBrokerAdapter.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.binding.impl;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
13 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeService;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMService;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28
29 /**
30  * The DataBrokerImpl simply defers to the DOMDataBroker for all its operations.
31  * All transactions and listener registrations are wrapped by the DataBrokerImpl
32  * to allow binding aware components to use the DataBroker transparently.
33  *
34  * <p>
35  * Besides this the DataBrokerImpl and it's collaborators also cache data that
36  * is already transformed from the binding independent to binding aware format
37  */
38 @Deprecated
39 public class BindingDOMDataBrokerAdapter extends AbstractForwardedDataBroker
40         implements DataBroker, DataTreeChangeService {
41     static final Factory<DataBroker> BUILDER_FACTORY = Builder::new;
42     private final DataTreeChangeService treeChangeService;
43
44     public BindingDOMDataBrokerAdapter(final DOMDataBroker domDataBroker, final BindingToNormalizedNodeCodec codec) {
45         super(domDataBroker, codec);
46         final DOMDataTreeChangeService domTreeChange = (DOMDataTreeChangeService) domDataBroker
47                 .getSupportedExtensions().get(DOMDataTreeChangeService.class);
48         if (domTreeChange != null) {
49             treeChangeService = BindingDOMDataTreeChangeServiceAdapter.create(codec, domTreeChange);
50         } else {
51             treeChangeService = null;
52         }
53     }
54
55     @Override
56
57     public ReadOnlyTransaction newReadOnlyTransaction() {
58         return new BindingDOMReadTransactionAdapter(getDelegate().newReadOnlyTransaction(),getCodec());
59     }
60
61     @Override
62     public ReadWriteTransaction newReadWriteTransaction() {
63         return new BindingDOMReadWriteTransactionAdapter(getDelegate().newReadWriteTransaction(),getCodec());
64     }
65
66     @Override
67     public WriteTransaction newWriteOnlyTransaction() {
68         return new BindingDOMWriteTransactionAdapter<>(getDelegate().newWriteOnlyTransaction(),getCodec());
69     }
70
71     @Override
72     public BindingTransactionChain createTransactionChain(final TransactionChainListener listener) {
73         return new BindingDOMTransactionChainAdapter(getDelegate(), getCodec(), listener);
74     }
75
76     @Override
77     public String toString() {
78         return "BindingDOMDataBrokerAdapter for " + getDelegate();
79     }
80
81     private static class Builder extends BindingDOMAdapterBuilder<DataBroker> {
82
83         @Override
84         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
85             return ImmutableSet.of(DOMDataBroker.class);
86         }
87
88         @Override
89         protected DataBroker createInstance(final BindingToNormalizedNodeCodec codec,
90                 final ClassToInstanceMap<DOMService> delegates) {
91             final DOMDataBroker domDataBroker = delegates.getInstance(DOMDataBroker.class);
92             return new BindingDOMDataBrokerAdapter(domDataBroker, codec);
93         }
94
95     }
96
97     @Override
98     public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L>
99             registerDataTreeChangeListener(final DataTreeIdentifier<T> treeId, final L listener) {
100         if (treeChangeService == null) {
101             throw new UnsupportedOperationException("Underlying data broker does not expose DOMDataTreeChangeService.");
102         }
103         return treeChangeService.registerDataTreeChangeListener(treeId, listener);
104     }
105 }

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