4f577eecf0a02f3a739fb44865970a15c8e276cb
[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.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 javassist.ClassPool;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
23 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
24 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceAdapter;
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.BindingDOMDataBrokerAdapter;
29 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMMountPointServiceAdapter;
30 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
31 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
32 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
33 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcServiceAdapter;
34 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
37 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
38 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
39 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
40 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
41 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
42 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter;
43 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMRpcRouter;
44 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
45 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
46 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
47 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
48 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
49 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
50 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
51 import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker;
52 import org.opendaylight.controller.sal.core.api.BrokerService;
53 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
54 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
55 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
56 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
57 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
58 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
59 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
60 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
61 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
62 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64
65 @Beta
66 public class BindingTestContext implements AutoCloseable {
67
68
69     private BindingToNormalizedNodeCodec codec;
70
71     private RootBindingAwareBroker baBrokerImpl;
72
73     private HeliumNotificationProviderServiceAdapter baNotifyImpl;
74
75
76     private BrokerImpl biBrokerImpl;
77
78     private final ListeningExecutorService executor;
79     private final ClassPool classPool;
80
81     private final boolean startWithSchema;
82
83     private DOMMountPointService biMountImpl;
84
85     private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
86
87     @Deprecated
88     private DataProviderService baData;
89
90     private DOMDataBroker newDOMDataBroker;
91
92     private final MockSchemaService mockSchemaService = new MockSchemaService();
93
94     private DataBroker dataBroker;
95
96     private RpcConsumerRegistry baConsumerRpc;
97
98     private BindingDOMRpcProviderServiceAdapter baProviderRpc;
99     private DOMRpcRouter domRouter;
100
101     private NotificationPublishService publishService;
102
103     private NotificationService listenService;
104
105     private DOMNotificationPublishService domPublishService;
106
107     private DOMNotificationService domListenService;
108
109
110
111     public DOMDataBroker getDomAsyncDataBroker() {
112         return newDOMDataBroker;
113     }
114
115     public BindingToNormalizedNodeCodec getCodec() {
116         return codec;
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     }
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 BindingDOMDataBrokerAdapter(newDOMDataBroker, codec);
132     }
133
134     public void startNewDomDataBroker() {
135         checkState(executor != null, "Executor needs to be set");
136         final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
137             MoreExecutors.newDirectExecutorService());
138         final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
139             MoreExecutors.newDirectExecutorService());
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     public void startBindingToDomMappingService() {
179         checkState(classPool != null, "ClassPool needs to be present");
180
181         final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(classPool));
182         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
183         final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
184         codec = new BindingToNormalizedNodeCodec(loading,  codecRegistry);
185         mockSchemaService.registerSchemaContextListener(codec);
186     }
187
188     private void updateYangSchema(final ImmutableSet<YangModuleInfo> moduleInfos) {
189         mockSchemaService.changeSchema(getContext(moduleInfos));
190     }
191
192     private SchemaContext getContext(final ImmutableSet<YangModuleInfo> moduleInfos) {
193         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
194         ctx.addModuleInfos(moduleInfos);
195         return ctx.tryToCreateSchemaContext().get();
196     }
197
198     public void start() {
199         startNewDomDataBroker();
200
201         startDomBroker();
202         startDomMountPoint();
203         startBindingToDomMappingService();
204         startNewDataBroker();
205         startNewBindingDataBroker();
206         startBindingNotificationBroker();
207         startBindingBroker();
208
209         startForwarding();
210         if (startWithSchema) {
211             loadYangSchemaFromClasspath();
212         }
213     }
214
215     public void startNewBindingDataBroker() {
216         final HydrogenDataBrokerAdapter forwarded = new HydrogenDataBrokerAdapter(dataBroker);
217         baData = forwarded;
218     }
219
220     private void startDomMountPoint() {
221         biMountImpl = new DOMMountPointServiceImpl();
222     }
223
224     private void startDomBroker() {
225         checkState(executor != null);
226
227         domRouter = new DOMRpcRouter();
228         mockSchemaService.registerSchemaContextListener(domRouter);
229
230         final ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
231         services.put(DOMRpcService.class, domRouter);
232
233         biBrokerImpl = new BrokerImpl(domRouter,services);
234
235     }
236
237     public void startBindingNotificationBroker() {
238         checkState(executor != null);
239         final DOMNotificationRouter router = DOMNotificationRouter.create(16);
240         domPublishService = router;
241         domListenService = router;
242         publishService = new BindingDOMNotificationPublishServiceAdapter(codec, domPublishService);
243         listenService = new BindingDOMNotificationServiceAdapter(codec, domListenService);
244         baNotifyImpl = new HeliumNotificationProviderServiceAdapter(publishService,listenService);
245
246     }
247
248     public void loadYangSchemaFromClasspath() {
249         final ImmutableSet<YangModuleInfo> moduleInfos = BindingReflections.loadModuleInfos();
250         updateYangSchema(moduleInfos);
251     }
252
253     @Deprecated
254     public DataProviderService getBindingDataBroker() {
255         return baData;
256     }
257
258     public RpcProviderRegistry getBindingRpcRegistry() {
259         return baBrokerImpl.getRoot();
260     }
261
262     public DOMRpcProviderService getDomRpcRegistry() {
263         return domRouter;
264     }
265
266     public DOMRpcService getDomRpcInvoker() {
267         return domRouter;
268     }
269
270     @Override
271     public void close() throws Exception {
272
273     }
274
275     public MountProviderService getBindingMountProviderService() {
276         return baBrokerImpl.getLegacyMount();
277     }
278
279     public DOMMountPointService getDomMountProviderService() {
280         return biMountImpl;
281     }
282
283     public DataBroker getDataBroker() {
284         return dataBroker;
285     }
286
287
288 }

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