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