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

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