2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.test.util;
10 import static com.google.common.base.Preconditions.checkState;
13 import java.util.concurrent.Future;
15 import javassist.ClassPool;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBackwardsCompatibleDataBroker;
19 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
23 import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
24 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
27 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
28 import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
29 import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl;
30 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
31 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer;
32 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
33 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBindingBrokerImpl;
34 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
35 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
36 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
37 import org.opendaylight.controller.sal.core.api.BrokerService;
38 import org.opendaylight.controller.sal.core.api.RpcImplementation;
39 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
40 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
41 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
43 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
44 import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
45 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
48 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
49 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
50 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
54 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import com.google.common.annotations.Beta;
60 import com.google.common.collect.ClassToInstanceMap;
61 import com.google.common.collect.ImmutableClassToInstanceMap;
62 import com.google.common.collect.ImmutableMap;
63 import com.google.common.collect.ImmutableSet;
64 import com.google.common.collect.MutableClassToInstanceMap;
65 import com.google.common.util.concurrent.ListeningExecutorService;
66 import com.google.common.util.concurrent.MoreExecutors;
69 public class BindingTestContext implements AutoCloseable {
71 public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier
72 .builder().toInstance();
74 private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
76 private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
78 private DomForwardedBindingBrokerImpl baBrokerImpl;
79 private DataBrokerImpl baDataImpl;
80 private NotificationBrokerImpl baNotifyImpl;
81 private BindingIndependentConnector baConnectImpl;
83 private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
84 @SuppressWarnings("deprecation")
85 private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker;
86 private BrokerImpl biBrokerImpl;
88 private final ListeningExecutorService executor;
89 private final ClassPool classPool;
91 private final boolean startWithSchema;
93 private MountPointManagerImpl biMountImpl;
97 private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
99 private BackwardsCompatibleDataBroker biCompatibleBroker;
101 @SuppressWarnings("deprecation")
102 private DataProviderService baData;
104 private DOMDataBroker newDOMDataBroker;
106 private final MockSchemaService mockSchemaService = new MockSchemaService();
108 private DataBroker dataBroker;
112 public DOMDataBroker getDomAsyncDataBroker() {
113 return newDOMDataBroker;
116 protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
117 this.executor = executor;
118 this.classPool = classPool;
119 this.startWithSchema = startWithSchema;
122 public void startDomDataBroker() {
123 checkState(executor != null, "Executor needs to be set");
124 biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
125 biDataImpl.setExecutor(executor);
126 biDataLegacyBroker = biDataImpl;
129 public void startNewDataBroker() {
130 checkState(executor != null, "Executor needs to be set");
131 checkState(newDOMDataBroker != null, "DOM Data Broker must be set");
132 dataBroker = new ForwardedBindingDataBroker(newDOMDataBroker, mappingServiceImpl, mockSchemaService);
135 public void startNewDomDataBroker() {
136 checkState(executor != null, "Executor needs to be set");
137 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", executor,
138 MoreExecutors.sameThreadExecutor());
139 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", executor,
140 MoreExecutors.sameThreadExecutor());
141 newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
142 .put(LogicalDatastoreType.OPERATIONAL, operStore)
143 .put(LogicalDatastoreType.CONFIGURATION, configStore)
146 newDOMDataBroker = new DOMDataBrokerImpl(newDatastores, executor);
148 biCompatibleBroker = new BackwardsCompatibleDataBroker(newDOMDataBroker,mockSchemaService);
150 mockSchemaService.registerSchemaContextListener(configStore);
151 mockSchemaService.registerSchemaContextListener(operStore);
152 biDataLegacyBroker = biCompatibleBroker;
155 public void startBindingDataBroker() {
156 checkState(executor != null, "Executor needs to be set");
157 baDataImpl = new DataBrokerImpl();
158 baDataImpl.setExecutor(executor);
162 public void startBindingBroker() {
163 checkState(executor != null, "Executor needs to be set");
164 checkState(baData != null, "Binding Data Broker must be started");
165 checkState(baNotifyImpl != null, "Notification Service must be started");
166 baBrokerImpl = new DomForwardedBindingBrokerImpl("test");
168 baBrokerImpl.getMountManager().setDataCommitExecutor(executor);
169 baBrokerImpl.getMountManager().setNotificationExecutor(executor);
170 baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test"));
171 baBrokerImpl.setLegacyDataBroker(baData);
172 baBrokerImpl.setNotificationBroker(baNotifyImpl);
173 baBrokerImpl.start();
176 public void startForwarding() {
177 checkState(baData != null, "Binding Data Broker needs to be started");
178 checkState(biDataLegacyBroker != null, "DOM Data Broker needs to be started.");
179 checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
181 baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
182 baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
183 baBrokerImpl.setConnector(baConnectImpl);
184 baBrokerImpl.setDomProviderContext(createMockContext());
185 baBrokerImpl.startForwarding();
188 private ProviderSession createMockContext() {
190 @SuppressWarnings("deprecation")
191 final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
192 .<BrokerService> builder()
194 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) //
195 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
196 .put(MountProvisionService.class, biMountImpl) //
199 return new ProviderSession() {
202 public Future<RpcResult<CompositeNode>> rpc(final QName rpc, final CompositeNode input) {
203 throw new UnsupportedOperationException();
207 public <T extends BrokerService> T getService(final Class<T> service) {
208 return domBrokerServices.getInstance(service);
212 public boolean isClosed() {
217 public Set<QName> getSupportedRpcs() {
222 public void close() {
226 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
227 final RpcRegistrationListener listener) {
232 public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation)
233 throws IllegalArgumentException {
238 public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
243 public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
249 public void startBindingToDomMappingService() {
250 checkState(classPool != null, "ClassPool needs to be present");
251 mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(classPool);
252 mockSchemaService.registerSchemaContextListener(mappingServiceImpl);
255 private void updateYangSchema(final ImmutableSet<YangModuleInfo> moduleInfos) {
256 mockSchemaService.changeSchema(getContext(moduleInfos));
259 private SchemaContext getContext(final ImmutableSet<YangModuleInfo> moduleInfos) {
260 ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
261 ctx.addModuleInfos(moduleInfos);
262 return ctx.tryToCreateSchemaContext().get();
265 public void start() {
266 startNewDomDataBroker();
269 startDomMountPoint();
270 startBindingToDomMappingService();
271 startNewDataBroker();
272 startNewBindingDataBroker();
273 startBindingNotificationBroker();
274 startBindingBroker();
277 if (startWithSchema) {
278 loadYangSchemaFromClasspath();
282 public void startNewBindingDataBroker() {
283 ForwardedBackwardsCompatibleDataBroker forwarded = new ForwardedBackwardsCompatibleDataBroker(newDOMDataBroker, mappingServiceImpl,mockSchemaService, executor);
287 private void startDomMountPoint() {
288 biMountImpl = new MountPointManagerImpl();
289 biMountImpl.setDataBroker(getDomDataBroker());
292 private void startDomBroker() {
293 checkState(executor != null);
295 SchemaAwareRpcBroker router = new SchemaAwareRpcBroker("/", mockSchemaService);
296 ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
297 biBrokerImpl = new BrokerImpl(router,services);
301 public void startBindingNotificationBroker() {
302 checkState(executor != null);
303 baNotifyImpl = new NotificationBrokerImpl(executor);
307 public void loadYangSchemaFromClasspath() {
308 ImmutableSet<YangModuleInfo> moduleInfos = BindingReflections.loadModuleInfos();
309 updateYangSchema(moduleInfos);
312 @SuppressWarnings("deprecation")
313 public DataProviderService getBindingDataBroker() {
317 @SuppressWarnings("deprecation")
318 public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
319 return biDataLegacyBroker;
322 public BindingIndependentMappingService getBindingToDomMappingService() {
323 return mappingServiceImpl;
326 public RpcProviderRegistry getBindingRpcRegistry() {
327 return baBrokerImpl.getRoot();
330 public RpcProvisionRegistry getDomRpcRegistry() {
331 if (biBrokerImpl == null) {
334 return biBrokerImpl.getRouter();
337 public RpcImplementation getDomRpcInvoker() {
338 return biBrokerImpl.getRouter();
342 public void close() throws Exception {
346 public MountProviderService getBindingMountProviderService() {
347 return baBrokerImpl.getMountManager();
350 public MountProvisionService getDomMountProviderService() {
354 public DataBroker getDataBroker() {