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>. It is
37 * also copy/pasted to org.opendaylight.restconf.nb.rfc8040.test.incubate.InMemoryMdsalModule.
39 * <p>BEWARE: Do *NOT* use this module in component tests or applications mixing
40 * code requiring the old controller and the new mdsal {@link DataBroker} & Co.
41 * APIs together - because this binds a *SEPARATE* {@link InMemoryDOMDataStore},
42 * and doesn't delegate to controller's InMemoryDOMDataStore. This is just fine
43 * for tests where all code under test already uses only the mdsal APIs.
45 * @author Michael Vorburger.ch
47 public class InMemoryMdsalModule extends AbstractModule {
49 private static final int NOTIFICATION_SERVICE_QUEUE_DEPTH = 128;
51 private final AbstractBaseDataBrokerTest dataBrokerTest;
52 private final DOMNotificationRouter domNotificationRouter;
54 public InMemoryMdsalModule() throws Exception {
55 dataBrokerTest = new AbstractConcurrentDataBrokerTest(true) { // NOT AbstractDataBrokerTest
57 dataBrokerTest.setup();
59 domNotificationRouter = DOMNotificationRouter.create(NOTIFICATION_SERVICE_QUEUE_DEPTH);
63 protected void configure() {
68 DataBroker getDataBroker() {
69 return dataBrokerTest.getDataBroker();
73 @Singleton DOMDataBroker getDOMDataBroker() {
74 return dataBrokerTest.getDomBroker();
78 @Singleton DOMNotificationRouter getDOMNotificationRouter() {
79 return dataBrokerTest.getDataBrokerTestCustomizer().getDomNotificationRouter();
83 @Singleton DOMSchemaService getSchemaService() {
84 return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
88 @Singleton SchemaContextProvider getSchemaContextProvider() {
89 DOMSchemaService schemaService = dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
90 if (schemaService instanceof SchemaContextProvider) {
91 return (SchemaContextProvider) schemaService;
93 throw new IllegalStateException(
94 "The schema service isn't a SchemaContextProvider, it's a " + schemaService.getClass());
98 @Singleton DOMMountPointService getDOMMountPoint() {
99 return new DOMMountPointServiceImpl();
103 @Singleton DOMNotificationService getDOMNotificationService() {
104 return domNotificationRouter;
108 @Singleton DOMNotificationPublishService getDOMNotificationPublishService() {
109 return domNotificationRouter;
113 @Singleton DOMNotificationSubscriptionListenerRegistry getDOMNotificationSubscriptionListenerRegistry() {
114 return domNotificationRouter;
118 @Singleton DOMRpcService getDOMRpcService(DOMSchemaService schemaService) {
119 return DOMRpcRouter.newInstance(schemaService).getRpcService();
123 public void close() {
124 // TODO When moving this to mdsal, must close components to shut down Threads etc.
125 // but cannot do this here (in netconf) yet, because we need to change AbstractBaseDataBrokerTest & Co..