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;
68 public class BindingTestContext implements AutoCloseable {
70 public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
71 .builder().toInstance();
73 private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
75 private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
77 private DomForwardedBindingBrokerImpl baBrokerImpl;
78 private DataBrokerImpl baDataImpl;
79 private NotificationBrokerImpl baNotifyImpl;
80 private BindingIndependentConnector baConnectImpl;
82 private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
83 @SuppressWarnings("deprecation")
84 private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker;
85 private BrokerImpl biBrokerImpl;
87 private final ListeningExecutorService executor;
88 private final ClassPool classPool;
90 private final boolean startWithSchema;
92 private MountPointManagerImpl biMountImpl;
96 private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
98 private BackwardsCompatibleDataBroker biCompatibleBroker;
100 @SuppressWarnings("deprecation")
101 private DataProviderService baData;
103 private DOMDataBroker newDOMDataBroker;
105 private final MockSchemaService mockSchemaService = new MockSchemaService();
107 private DataBroker dataBroker;
111 public DOMDataBroker getDomAsyncDataBroker() {
112 return newDOMDataBroker;
115 protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
116 this.executor = executor;
117 this.classPool = classPool;
118 this.startWithSchema = startWithSchema;
121 public void startDomDataBroker() {
122 checkState(executor != null, "Executor needs to be set");
123 biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
124 biDataImpl.setExecutor(executor);
125 biDataLegacyBroker = biDataImpl;
128 public void startNewDataBroker() {
129 checkState(executor != null, "Executor needs to be set");
130 checkState(newDOMDataBroker != null, "DOM Data Broker must be set");
131 dataBroker = new ForwardedBindingDataBroker(newDOMDataBroker, mappingServiceImpl, mockSchemaService);
134 public void startNewDomDataBroker() {
135 checkState(executor != null, "Executor needs to be set");
136 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", executor);
137 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", executor);
138 newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
139 .put(LogicalDatastoreType.OPERATIONAL, operStore)
140 .put(LogicalDatastoreType.CONFIGURATION, configStore)
143 newDOMDataBroker = new DOMDataBrokerImpl(newDatastores, executor);
145 biCompatibleBroker = new BackwardsCompatibleDataBroker(newDOMDataBroker,mockSchemaService);
147 mockSchemaService.registerSchemaServiceListener(configStore);
148 mockSchemaService.registerSchemaServiceListener(operStore);
149 biDataLegacyBroker = biCompatibleBroker;
152 public void startBindingDataBroker() {
153 checkState(executor != null, "Executor needs to be set");
154 baDataImpl = new DataBrokerImpl();
155 baDataImpl.setExecutor(executor);
159 public void startBindingBroker() {
160 checkState(executor != null, "Executor needs to be set");
161 checkState(baData != null, "Binding Data Broker must be started");
162 checkState(baNotifyImpl != null, "Notification Service must be started");
163 baBrokerImpl = new DomForwardedBindingBrokerImpl("test");
165 baBrokerImpl.getMountManager().setDataCommitExecutor(executor);
166 baBrokerImpl.getMountManager().setNotificationExecutor(executor);
167 baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test"));
168 baBrokerImpl.setLegacyDataBroker(baData);
169 baBrokerImpl.setNotificationBroker(baNotifyImpl);
170 baBrokerImpl.start();
173 public void startForwarding() {
174 checkState(baData != null, "Binding Data Broker needs to be started");
175 checkState(biDataLegacyBroker != null, "DOM Data Broker needs to be started.");
176 checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
178 baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
179 baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
180 baBrokerImpl.setConnector(baConnectImpl);
181 baBrokerImpl.setDomProviderContext(createMockContext());
182 baBrokerImpl.startForwarding();
185 private ProviderSession createMockContext() {
187 @SuppressWarnings("deprecation")
188 final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
189 .<BrokerService> builder()
191 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) //
192 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
193 .put(MountProvisionService.class, biMountImpl) //
196 return new ProviderSession() {
199 public Future<RpcResult<CompositeNode>> rpc(final QName rpc, final CompositeNode input) {
200 throw new UnsupportedOperationException();
204 public <T extends BrokerService> T getService(final Class<T> service) {
205 return domBrokerServices.getInstance(service);
209 public boolean isClosed() {
214 public Set<QName> getSupportedRpcs() {
219 public void close() {
223 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
224 final RpcRegistrationListener listener) {
229 public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation)
230 throws IllegalArgumentException {
235 public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
240 public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
246 public void startBindingToDomMappingService() {
247 checkState(classPool != null, "ClassPool needs to be present");
248 mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(classPool);
249 mockSchemaService.registerSchemaServiceListener(mappingServiceImpl);
252 private void updateYangSchema(final ImmutableSet<YangModuleInfo> moduleInfos) {
253 mockSchemaService.changeSchema(getContext(moduleInfos));
256 private SchemaContext getContext(final ImmutableSet<YangModuleInfo> moduleInfos) {
257 ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
258 ctx.addModuleInfos(moduleInfos);
259 return ctx.tryToCreateSchemaContext().get();
262 public void start() {
263 startNewDomDataBroker();
266 startDomMountPoint();
267 startBindingToDomMappingService();
268 startNewDataBroker();
269 startNewBindingDataBroker();
270 startBindingNotificationBroker();
271 startBindingBroker();
274 if (startWithSchema) {
275 loadYangSchemaFromClasspath();
279 public void startNewBindingDataBroker() {
280 ForwardedBackwardsCompatibleDataBroker forwarded = new ForwardedBackwardsCompatibleDataBroker(newDOMDataBroker, mappingServiceImpl,mockSchemaService, executor);
284 private void startDomMountPoint() {
285 biMountImpl = new MountPointManagerImpl();
286 biMountImpl.setDataBroker(getDomDataBroker());
289 private void startDomBroker() {
290 checkState(executor != null);
292 SchemaAwareRpcBroker router = new SchemaAwareRpcBroker("/", mockSchemaService);
293 ClassToInstanceMap<BrokerService> services = MutableClassToInstanceMap.create();
294 biBrokerImpl = new BrokerImpl(router,services);
298 public void startBindingNotificationBroker() {
299 checkState(executor != null);
300 baNotifyImpl = new NotificationBrokerImpl(executor);
304 public void loadYangSchemaFromClasspath() {
305 ImmutableSet<YangModuleInfo> moduleInfos = BindingReflections.loadModuleInfos();
306 updateYangSchema(moduleInfos);
309 @SuppressWarnings("deprecation")
310 public DataProviderService getBindingDataBroker() {
314 @SuppressWarnings("deprecation")
315 public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
316 return biDataLegacyBroker;
319 public BindingIndependentMappingService getBindingToDomMappingService() {
320 return mappingServiceImpl;
323 public RpcProviderRegistry getBindingRpcRegistry() {
324 return baBrokerImpl.getRoot();
327 public RpcProvisionRegistry getDomRpcRegistry() {
328 if (biBrokerImpl == null) {
331 return biBrokerImpl.getRouter();
334 public RpcImplementation getDomRpcInvoker() {
335 return biBrokerImpl.getRouter();
339 public void close() throws Exception {
343 public MountProviderService getBindingMountProviderService() {
344 return baBrokerImpl.getMountManager();
347 public MountProvisionService getDomMountProviderService() {
351 public DataBroker getDataBroker() {