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

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