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

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