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

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