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

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