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