Merge "Bug 1029: Remove dead code: sal-binding-spi"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / compat / BackwardsCompatibleDataBroker.java
1 package org.opendaylight.controller.md.sal.dom.broker.impl.compat;
2
3 import org.opendaylight.controller.md.sal.common.api.RegistrationListener;
4 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
5 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
6 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
7 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandlerRegistration;
8 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
9 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
10 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
11 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
12 import org.opendaylight.controller.sal.common.DataStoreIdentifier;
13 import org.opendaylight.controller.sal.core.api.data.DataChangeListener;
14 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
15 import org.opendaylight.controller.sal.core.api.data.DataProviderService;
16 import org.opendaylight.controller.sal.core.api.data.DataValidator;
17 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
18 import org.opendaylight.yangtools.concepts.Delegator;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
27
28 public class BackwardsCompatibleDataBroker implements DataProviderService, SchemaContextListener {
29
30     DOMDataBroker backingBroker;
31     DataNormalizer normalizer;
32     private final ListenerRegistry<DataChangeListener> fakeRegistry = ListenerRegistry.create();
33
34
35     public BackwardsCompatibleDataBroker(final DOMDataBroker newBiDataImpl) {
36         backingBroker = newBiDataImpl;
37     }
38
39     @Override
40     public void onGlobalContextUpdated(final SchemaContext ctx) {
41         normalizer = new DataNormalizer(ctx);
42     }
43
44     @Override
45     public CompositeNode readConfigurationData(final InstanceIdentifier legacyPath) {
46         BackwardsCompatibleTransaction<?> tx = BackwardsCompatibleTransaction.readOnlyTransaction(backingBroker.newReadOnlyTransaction(),normalizer);
47         try {
48             return tx.readConfigurationData(legacyPath);
49         } finally {
50             tx.commit();
51         }
52     }
53
54     @Override
55     public CompositeNode readOperationalData(final InstanceIdentifier legacyPath) {
56         BackwardsCompatibleTransaction<?> tx = BackwardsCompatibleTransaction.readOnlyTransaction(backingBroker.newReadOnlyTransaction(),normalizer);
57         try {
58             return tx.readOperationalData(legacyPath);
59         } finally {
60             tx.commit();
61         }
62     }
63
64     @Override
65     public DataModificationTransaction beginTransaction() {
66         return BackwardsCompatibleTransaction.readWriteTransaction(backingBroker.newReadWriteTransaction(), normalizer);
67     }
68
69     @Override
70     public ListenerRegistration<DataChangeListener> registerDataChangeListener(final InstanceIdentifier path,
71             final DataChangeListener listener) {
72         return fakeRegistry .register(listener);
73     }
74
75     @Override
76     public Registration<DataCommitHandler<InstanceIdentifier, CompositeNode>> registerCommitHandler(
77             final InstanceIdentifier path, final DataCommitHandler<InstanceIdentifier, CompositeNode> commitHandler) {
78         // FIXME Do real forwarding
79         return new AbstractObjectRegistration<DataCommitHandler<InstanceIdentifier,CompositeNode>>(commitHandler) {
80             @Override
81             protected void removeRegistration() {
82                 // NOOP
83             }
84         };
85     }
86
87     @Override
88     public ListenerRegistration<RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier, CompositeNode>>> registerCommitHandlerListener(
89             final RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier, CompositeNode>> commitHandlerListener) {
90         return null;
91     }
92
93     // Obsolote functionality
94
95     @Override
96     public void addValidator(final DataStoreIdentifier store, final DataValidator validator) {
97         throw new UnsupportedOperationException();
98     }
99
100     @Override
101     public void removeValidator(final DataStoreIdentifier store, final DataValidator validator) {
102         throw new UnsupportedOperationException();
103     }
104
105     @Override
106     public void addRefresher(final DataStoreIdentifier store, final DataRefresher refresher) {
107         throw new UnsupportedOperationException();
108     }
109
110     @Override
111     public void removeRefresher(final DataStoreIdentifier store, final DataRefresher refresher) {
112         throw new UnsupportedOperationException();
113     }
114
115     @Override
116     public Registration<DataReader<InstanceIdentifier, CompositeNode>> registerConfigurationReader(
117             final InstanceIdentifier path, final DataReader<InstanceIdentifier, CompositeNode> reader) {
118         throw new UnsupportedOperationException("Data Reader contract is not supported.");
119     }
120
121     @Override
122     public Registration<DataReader<InstanceIdentifier, CompositeNode>> registerOperationalReader(
123             final InstanceIdentifier path, final DataReader<InstanceIdentifier, CompositeNode> reader) {
124         throw new UnsupportedOperationException("Data Reader contract is not supported.");
125     }
126
127     private final class TranslatingListenerInvoker implements DOMDataChangeListener, Delegator<DataChangeListener> {
128
129
130         private DataChangeListener delegate;
131
132         @Override
133         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange) {
134
135             DataChangeEvent<InstanceIdentifier, CompositeNode> legacyChange = null;
136             delegate.onDataChanged(legacyChange);
137         }
138
139         @Override
140         public DataChangeListener getDelegate() {
141
142             return delegate;
143         }
144
145
146     }
147
148 }