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