Replaced Helium Notification Broker with new Notifcation Broker.
[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 javassist.ClassPool;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
24 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
25 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceAdapter;
26 import org.opendaylight.controller.md.sal.binding.compat.HeliumRpcProviderRegistry;
27 import org.opendaylight.controller.md.sal.binding.compat.HydrogenDataBrokerAdapter;
28 import org.opendaylight.controller.md.sal.binding.compat.HydrogenMountProvisionServiceAdapter;
29 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
30 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMMountPointServiceAdapter;
31 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
32 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
33 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
34 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcServiceAdapter;
35 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
38 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
39 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
40 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
41 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
42 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
43 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter;
44 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
45 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
46 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
47 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
48 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
49 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
50 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
51 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
52 import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker;
53 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
54 import org.opendaylight.controller.sal.core.api.BrokerService;
55 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
56 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
57 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
58 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
59 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
60 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
61 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
62 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
63 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
64 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66
67 @Beta
68 public class BindingTestContext implements AutoCloseable {
69
70
71     private BindingToNormalizedNodeCodec codec;
72
73     private RootBindingAwareBroker baBrokerImpl;
74
75     private HeliumNotificationProviderServiceAdapter baNotifyImpl;
76
77
78     private BrokerImpl biBrokerImpl;
79
80     private final ListeningExecutorService executor;
81     private final ClassPool classPool;
82
83     private final boolean startWithSchema;
84
85     private DOMMountPointService biMountImpl;
86
87     private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
88
89     @Deprecated
90     private DataProviderService baData;
91
92     private DOMDataBroker newDOMDataBroker;
93
94     private final MockSchemaService mockSchemaService = new MockSchemaService();
95
96     private DataBroker dataBroker;
97
98     private RpcConsumerRegistry baConsumerRpc;
99
100     private BindingDOMRpcProviderServiceAdapter baProviderRpc;
101     private DOMRpcRouter domRouter;
102
103     private NotificationPublishService publishService;
104
105     private NotificationService listenService;
106
107     private DOMNotificationPublishService domPublishService;
108
109     private DOMNotificationService domListenService;
110
111
112
113     public DOMDataBroker getDomAsyncDataBroker() {
114         return newDOMDataBroker;
115     }
116
117     public BindingToNormalizedNodeCodec getCodec() {
118         return codec;
119     }
120
121     protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
122         this.executor = executor;
123         this.classPool = classPool;
124         this.startWithSchema = startWithSchema;
125     }
126
127     public void startDomDataBroker() {
128     }
129
130     public void startNewDataBroker() {
131         checkState(executor != null, "Executor needs to be set");
132         checkState(newDOMDataBroker != null, "DOM Data Broker must be set");
133         dataBroker = new BindingDOMDataBrokerAdapter(newDOMDataBroker, codec);
134     }
135
136     public void startNewDomDataBroker() {
137         checkState(executor != null, "Executor needs to be set");
138         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
139         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
140         newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
141                 .put(LogicalDatastoreType.OPERATIONAL, operStore)
142                 .put(LogicalDatastoreType.CONFIGURATION, configStore)
143                 .build();
144
145         newDOMDataBroker = new SerializedDOMDataBroker(newDatastores, executor);
146
147         mockSchemaService.registerSchemaContextListener(configStore);
148         mockSchemaService.registerSchemaContextListener(operStore);
149     }
150
151     public void startBindingDataBroker() {
152
153     }
154
155     public void startBindingBroker() {
156         checkState(executor != null, "Executor needs to be set");
157         checkState(baData != null, "Binding Data Broker must be started");
158         checkState(baNotifyImpl != null, "Notification Service must be started");
159
160         baConsumerRpc = new BindingDOMRpcServiceAdapter(getDomRpcInvoker(), codec);
161         baProviderRpc = new BindingDOMRpcProviderServiceAdapter(getDomRpcRegistry(), codec);
162
163         baBrokerImpl = new RootBindingAwareBroker("test");
164
165         final MountPointService mountService = new BindingDOMMountPointServiceAdapter(biMountImpl, codec);
166         baBrokerImpl.setMountService(mountService);
167         baBrokerImpl.setLegacyMountManager(new HydrogenMountProvisionServiceAdapter(mountService));
168         baBrokerImpl.setRpcBroker(new HeliumRpcProviderRegistry(baConsumerRpc, baProviderRpc));
169         baBrokerImpl.setLegacyDataBroker(baData);
170         baBrokerImpl.setNotificationBroker(baNotifyImpl);
171         baBrokerImpl.start();
172     }
173
174     public void startForwarding() {
175
176     }
177
178     private ProviderSession createMockContext() {
179
180         @SuppressWarnings("deprecation")
181         final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
182                 .<BrokerService> builder()
183                 //
184                 .put(DOMRpcRouter.class, biBrokerImpl.getRouter()) //
185                 .put(DOMMountPointService.class, biMountImpl)
186                 .build();
187
188         return new ProviderSession() {
189
190             @Override
191             public <T extends BrokerService> T getService(final Class<T> service) {
192                 return domBrokerServices.getInstance(service);
193             }
194
195             @Override
196             public boolean isClosed() {
197                 return false;
198             }
199
200             @Override
201             public void close() {
202             }
203         };
204     }
205
206     public void startBindingToDomMappingService() {
207         checkState(classPool != null, "ClassPool needs to be present");
208
209         final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(classPool));
210         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
211         final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
212         codec = new BindingToNormalizedNodeCodec(loading,  codecRegistry);
213         mockSchemaService.registerSchemaContextListener(codec);
214     }
215
216     private void updateYangSchema(final ImmutableSet<YangModuleInfo> moduleInfos) {
217         mockSchemaService.changeSchema(getContext(moduleInfos));
218     }
219
220     private SchemaContext getContext(final ImmutableSet<YangModuleInfo> moduleInfos) {
221         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
222         ctx.addModuleInfos(moduleInfos);
223         return ctx.tryToCreateSchemaContext().get();
224     }
225
226     public void start() {
227         startNewDomDataBroker();
228
229         startDomBroker();
230         startDomMountPoint();
231         startBindingToDomMappingService();
232         startNewDataBroker();
233         startNewBindingDataBroker();
234         startBindingNotificationBroker();
235         startBindingBroker();
236
237         startForwarding();
238         if (startWithSchema) {
239             loadYangSchemaFromClasspath();
240         }
241     }
242
243     public void startNewBindingDataBroker() {
244         final HydrogenDataBrokerAdapter forwarded = new HydrogenDataBrokerAdapter(dataBroker);
245         baData = forwarded;
246     }
247
248     private void startDomMountPoint() {
249         biMountImpl = new DOMMountPointServiceImpl();
250     }
251
252     private void startDomBroker() {
253         checkState(executor != null);
254
255         domRouter = new DOMRpcRouter();
256         mockSchemaService.registerSchemaContextListener(domRouter);
257
258         final ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
259         services.put(DOMRpcService.class, domRouter);
260
261         biBrokerImpl = new BrokerImpl(domRouter,services);
262
263     }
264
265     public void startBindingNotificationBroker() {
266         checkState(executor != null);
267         final DOMNotificationRouter router = DOMNotificationRouter.create(16);
268         domPublishService = router;
269         domListenService = router;
270         publishService = new BindingDOMNotificationPublishServiceAdapter(codec, domPublishService);
271         listenService = new BindingDOMNotificationServiceAdapter(codec, domListenService);
272         baNotifyImpl = new HeliumNotificationProviderServiceAdapter(publishService,listenService);
273
274     }
275
276     public void loadYangSchemaFromClasspath() {
277         final ImmutableSet<YangModuleInfo> moduleInfos = BindingReflections.loadModuleInfos();
278         updateYangSchema(moduleInfos);
279     }
280
281     @Deprecated
282     public DataProviderService getBindingDataBroker() {
283         return baData;
284     }
285
286     public RpcProviderRegistry getBindingRpcRegistry() {
287         return baBrokerImpl.getRoot();
288     }
289
290     public DOMRpcProviderService getDomRpcRegistry() {
291         return domRouter;
292     }
293
294     public DOMRpcService getDomRpcInvoker() {
295         return domRouter;
296     }
297
298     @Override
299     public void close() throws Exception {
300
301     }
302
303     public MountProviderService getBindingMountProviderService() {
304         return baBrokerImpl.getLegacyMount();
305     }
306
307     public DOMMountPointService getDomMountProviderService() {
308         return biMountImpl;
309     }
310
311     public DataBroker getDataBroker() {
312         return dataBroker;
313     }
314
315
316 }

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