2 * Copyright (c) 2019 Red Hat, 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.restconf.impl.test.incubate;
10 import com.google.inject.AbstractModule;
11 import com.google.inject.Provides;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Singleton;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractBaseDataBrokerTest;
16 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
17 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
24 import org.opendaylight.mdsal.dom.broker.DOMNotificationRouter;
25 import org.opendaylight.mdsal.dom.broker.DOMRpcRouter;
26 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
27 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
31 * Guice Module which binds the mdsal (not controller) {@link DataBroker} & Co.
32 * in-memory implementation suitable for tests.
34 * <p>This class is here only temporarily and it can and should be removed and
35 * replaced when the equivalent will be offered by the mdsal project itself; see
36 * <a href="https://jira.opendaylight.org/browse/MDSAL-418">MDSAL-418</a>.
38 * <p>BEWARE: Do *NOT* use this module in component tests or applications mixing
39 * code requiring the old controller and the new mdsal {@link DataBroker} & Co.
40 * APIs together - because this binds a *SEPARATE* {@link InMemoryDOMDataStore},
41 * and doesn't delegate to controller's InMemoryDOMDataStore. This is just fine
42 * for tests where all code under test already uses only the mdsal APIs.
44 * @author Michael Vorburger.ch
46 public class InMemoryMdsalModule extends AbstractModule {
48 private static final int NOTIFICATION_SERVICE_QUEUE_DEPTH = 128;
50 private final AbstractBaseDataBrokerTest dataBrokerTest;
51 private final DOMNotificationRouter domNotificationRouter;
53 public InMemoryMdsalModule() throws Exception {
54 dataBrokerTest = new AbstractConcurrentDataBrokerTest(true) { // NOT AbstractDataBrokerTest
56 dataBrokerTest.setup();
58 domNotificationRouter = DOMNotificationRouter.create(NOTIFICATION_SERVICE_QUEUE_DEPTH);
62 protected void configure() {
67 DataBroker getDataBroker() {
68 return dataBrokerTest.getDataBroker();
72 @Singleton DOMDataBroker getDOMDataBroker() {
73 return dataBrokerTest.getDomBroker();
77 @Singleton DOMNotificationRouter getDOMNotificationRouter() {
78 return dataBrokerTest.getDataBrokerTestCustomizer().getDomNotificationRouter();
82 @Singleton DOMSchemaService getSchemaService() {
83 return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
87 @Singleton SchemaContextProvider getSchemaContextProvider() {
88 DOMSchemaService schemaService = dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
89 if (schemaService instanceof SchemaContextProvider) {
90 return (SchemaContextProvider) schemaService;
92 throw new IllegalStateException(
93 "The schema service isn't a SchemaContextProvider, it's a " + schemaService.getClass());
97 @Singleton DOMMountPointService getDOMMountPoint() {
98 return new DOMMountPointServiceImpl();
102 @Singleton DOMNotificationService getDOMNotificationService() {
103 return domNotificationRouter;
107 @Singleton DOMNotificationPublishService getDOMNotificationPublishService() {
108 return domNotificationRouter;
112 @Singleton DOMNotificationSubscriptionListenerRegistry getDOMNotificationSubscriptionListenerRegistry() {
113 return domNotificationRouter;
117 @Singleton DOMRpcService getDOMRpcService(DOMSchemaService schemaService) {
118 return DOMRpcRouter.newInstance(schemaService).getRpcService();
122 public void close() {
123 // TODO When moving this to mdsal, must close components to shut down Threads etc.
124 // but cannot do this here (in netconf) yet, because we need to change AbstractBaseDataBrokerTest & Co..