Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / util / BindingTestContext.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.sal.binding.test.util;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ClassToInstanceMap;
14 import com.google.common.collect.ImmutableClassToInstanceMap;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.MutableClassToInstanceMap;
18 import com.google.common.util.concurrent.ListeningExecutorService;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Set;
21 import java.util.concurrent.Future;
22 import javassist.ClassPool;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
25 import org.opendaylight.controller.md.sal.binding.compat.HeliumRpcProviderRegistry;
26 import org.opendaylight.controller.md.sal.binding.compat.HydrogenDataBrokerAdapter;
27 import org.opendaylight.controller.md.sal.binding.compat.HydrogenMountProvisionServiceAdapter;
28 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMMountPointServiceAdapter;
29 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
30 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcServiceAdapter;
31 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
32 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
35 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
38 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
39 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
40 import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
41 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
42 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
43 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
44 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
45 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
46 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
47 import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl;
48 import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker;
49 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
50 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
51 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
52 import org.opendaylight.controller.sal.core.api.BrokerService;
53 import org.opendaylight.controller.sal.core.api.RpcImplementation;
54 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
55 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
56 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
57 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
58 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
59 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
60 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
61 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
62 import org.opendaylight.yangtools.concepts.ListenerRegistration;
63 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
64 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
65 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
66 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
67 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
68 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
69 import org.opendaylight.yangtools.yang.common.QName;
70 import org.opendaylight.yangtools.yang.common.RpcResult;
71 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
72 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
73 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
74
75 @Beta
76 public class BindingTestContext implements AutoCloseable {
77
78
79     private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
80     private BindingToNormalizedNodeCodec codec;
81
82     private RootBindingAwareBroker baBrokerImpl;
83
84     private NotificationBrokerImpl baNotifyImpl;
85
86
87     @Deprecated
88     private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker;
89     private BrokerImpl biBrokerImpl;
90
91     private final ListeningExecutorService executor;
92     private final ClassPool classPool;
93
94     private final boolean startWithSchema;
95
96     private DOMMountPointService biMountImpl;
97
98     private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
99
100     @Deprecated
101     private BackwardsCompatibleDataBroker biCompatibleBroker;
102
103     @Deprecated
104     private DataProviderService baData;
105
106     private DOMDataBroker newDOMDataBroker;
107
108     private final MockSchemaService mockSchemaService = new MockSchemaService();
109
110     private DataBroker dataBroker;
111
112     private RpcConsumerRegistry baConsumerRpc;
113
114     private BindingDOMRpcProviderServiceAdapter baProviderRpc;
115     private DOMRpcRouter domRouter;
116
117
118
119     public DOMDataBroker getDomAsyncDataBroker() {
120         return newDOMDataBroker;
121     }
122
123     public BindingToNormalizedNodeCodec getCodec() {
124         return codec;
125     }
126
127     protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
128         this.executor = executor;
129         this.classPool = classPool;
130         this.startWithSchema = startWithSchema;
131     }
132
133     public void startDomDataBroker() {
134     }
135
136     public void startNewDataBroker() {
137         checkState(executor != null, "Executor needs to be set");
138         checkState(newDOMDataBroker != null, "DOM Data Broker must be set");
139         dataBroker = new ForwardedBindingDataBroker(newDOMDataBroker, codec);
140     }
141
142     public void startNewDomDataBroker() {
143         checkState(executor != null, "Executor needs to be set");
144         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
145         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
146         newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
147                 .put(LogicalDatastoreType.OPERATIONAL, operStore)
148                 .put(LogicalDatastoreType.CONFIGURATION, configStore)
149                 .build();
150
151         newDOMDataBroker = new SerializedDOMDataBroker(newDatastores, executor);
152
153         biCompatibleBroker = new BackwardsCompatibleDataBroker(newDOMDataBroker,mockSchemaService);
154
155         mockSchemaService.registerSchemaContextListener(configStore);
156         mockSchemaService.registerSchemaContextListener(operStore);
157         biDataLegacyBroker = biCompatibleBroker;
158     }
159
160     public void startBindingDataBroker() {
161
162     }
163
164     public void startBindingBroker() {
165         checkState(executor != null, "Executor needs to be set");
166         checkState(baData != null, "Binding Data Broker must be started");
167         checkState(baNotifyImpl != null, "Notification Service must be started");
168
169         baConsumerRpc = new BindingDOMRpcServiceAdapter(getDomRpcInvoker(), codec);
170         baProviderRpc = new BindingDOMRpcProviderServiceAdapter(getDomRpcRegistry(), codec);
171
172         baBrokerImpl = new RootBindingAwareBroker("test");
173
174         final MountPointService mountService = new BindingDOMMountPointServiceAdapter(biMountImpl, codec);
175         baBrokerImpl.setMountService(mountService);
176         baBrokerImpl.setLegacyMountManager(new HydrogenMountProvisionServiceAdapter(mountService));
177         baBrokerImpl.setRpcBroker(new HeliumRpcProviderRegistry(baConsumerRpc,baProviderRpc));
178         baBrokerImpl.setLegacyDataBroker(baData);
179         baBrokerImpl.setNotificationBroker(baNotifyImpl);
180         baBrokerImpl.start();
181     }
182
183     public void startForwarding() {
184
185     }
186
187     private ProviderSession createMockContext() {
188
189         @SuppressWarnings("deprecation")
190         final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
191                 .<BrokerService> builder()
192                 //
193                 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) //
194                 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
195                 .put(DOMMountPointService.class, biMountImpl)
196                 .build();
197
198         return new ProviderSession() {
199
200             @Override
201             public Future<RpcResult<CompositeNode>> rpc(final QName rpc, final CompositeNode input) {
202                 throw new UnsupportedOperationException();
203             }
204
205             @Override
206             public <T extends BrokerService> T getService(final Class<T> service) {
207                 return domBrokerServices.getInstance(service);
208             }
209
210             @Override
211             public boolean isClosed() {
212                 return false;
213             }
214
215             @Override
216             public Set<QName> getSupportedRpcs() {
217                 return null;
218             }
219
220             @Override
221             public void close() {
222             }
223
224             @Override
225             public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
226                     final RpcRegistrationListener listener) {
227                 return null;
228             }
229
230             @Override
231             public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation)
232                     throws IllegalArgumentException {
233                 return null;
234             }
235
236             @Override
237             public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
238                 return null;
239             }
240
241             @Override
242             public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
243                 return null;
244             }
245         };
246     }
247
248     public void startBindingToDomMappingService() {
249         checkState(classPool != null, "ClassPool needs to be present");
250         mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(classPool);
251         mockSchemaService.registerSchemaContextListener(mappingServiceImpl);
252
253         final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(classPool));
254         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
255         final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
256         codec = new BindingToNormalizedNodeCodec(loading, mappingServiceImpl, codecRegistry);
257         mockSchemaService.registerSchemaContextListener(codec);
258     }
259
260     private void updateYangSchema(final ImmutableSet<YangModuleInfo> moduleInfos) {
261         mockSchemaService.changeSchema(getContext(moduleInfos));
262     }
263
264     private SchemaContext getContext(final ImmutableSet<YangModuleInfo> moduleInfos) {
265         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
266         ctx.addModuleInfos(moduleInfos);
267         return ctx.tryToCreateSchemaContext().get();
268     }
269
270     public void start() {
271         startNewDomDataBroker();
272
273         startDomBroker();
274         startDomMountPoint();
275         startBindingToDomMappingService();
276         startNewDataBroker();
277         startNewBindingDataBroker();
278         startBindingNotificationBroker();
279         startBindingBroker();
280
281         startForwarding();
282         if (startWithSchema) {
283             loadYangSchemaFromClasspath();
284         }
285     }
286
287     public void startNewBindingDataBroker() {
288         final HydrogenDataBrokerAdapter forwarded = new HydrogenDataBrokerAdapter(dataBroker);
289         baData = forwarded;
290     }
291
292     private void startDomMountPoint() {
293         biMountImpl = new DOMMountPointServiceImpl();
294     }
295
296     private void startDomBroker() {
297         checkState(executor != null);
298
299         final SchemaAwareRpcBroker router = new SchemaAwareRpcBroker("/", mockSchemaService);
300
301         domRouter = new DOMRpcRouter();
302         mockSchemaService.registerSchemaContextListener(domRouter);
303
304         final ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
305         services.put(DOMRpcService.class, domRouter);
306
307         biBrokerImpl = new BrokerImpl(router,services);
308
309     }
310
311     public void startBindingNotificationBroker() {
312         checkState(executor != null);
313         baNotifyImpl = new NotificationBrokerImpl(executor);
314
315     }
316
317     public void loadYangSchemaFromClasspath() {
318         final ImmutableSet<YangModuleInfo> moduleInfos = BindingReflections.loadModuleInfos();
319         updateYangSchema(moduleInfos);
320     }
321
322     @Deprecated
323     public DataProviderService getBindingDataBroker() {
324         return baData;
325     }
326
327     @Deprecated
328     public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
329         return biDataLegacyBroker;
330     }
331
332     public BindingIndependentMappingService getBindingToDomMappingService() {
333         return mappingServiceImpl;
334     }
335
336     public RpcProviderRegistry getBindingRpcRegistry() {
337         return baBrokerImpl.getRoot();
338     }
339
340     public DOMRpcProviderService getDomRpcRegistry() {
341         return domRouter;
342     }
343
344     public DOMRpcService getDomRpcInvoker() {
345         return domRouter;
346     }
347
348     @Override
349     public void close() throws Exception {
350
351     }
352
353     public MountProviderService getBindingMountProviderService() {
354         return baBrokerImpl.getLegacyMount();
355     }
356
357     public DOMMountPointService getDomMountProviderService() {
358         return biMountImpl;
359     }
360
361     public DataBroker getDataBroker() {
362         return dataBroker;
363     }
364
365
366 }

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