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

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