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

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