cabdc6ce77db2c6ee815b4608f24b851b3560ed7
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / incubate / InMemoryMdsalModule.java
1 /*
2  * Copyright (c) 2019 Red Hat, 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.restconf.impl.test.incubate;
9
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.EffectiveModelContextProvider;
29
30 /**
31  * Guice Module which binds the mdsal (not controller) {@link DataBroker} & Co.
32  * in-memory implementation suitable for tests.
33  *
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.
38  *
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.
44  *
45  * @author Michael Vorburger.ch
46  */
47 public class InMemoryMdsalModule extends AbstractModule {
48
49     private static final int NOTIFICATION_SERVICE_QUEUE_DEPTH = 128;
50
51     private final AbstractBaseDataBrokerTest dataBrokerTest;
52     private final DOMNotificationRouter domNotificationRouter;
53
54     public InMemoryMdsalModule() throws Exception {
55         dataBrokerTest = new AbstractConcurrentDataBrokerTest(true) { // NOT AbstractDataBrokerTest
56         };
57         dataBrokerTest.setup();
58
59         domNotificationRouter = DOMNotificationRouter.create(NOTIFICATION_SERVICE_QUEUE_DEPTH);
60     }
61
62     @Override
63     protected void configure() {
64     }
65
66     @Provides
67     @Singleton
68     DataBroker getDataBroker() {
69         return dataBrokerTest.getDataBroker();
70     }
71
72     @Provides
73     @Singleton DOMDataBroker getDOMDataBroker() {
74         return dataBrokerTest.getDomBroker();
75     }
76
77     @Provides
78     @Singleton DOMNotificationRouter getDOMNotificationRouter() {
79         return dataBrokerTest.getDataBrokerTestCustomizer().getDomNotificationRouter();
80     }
81
82     @Provides
83     @Singleton DOMSchemaService getSchemaService() {
84         return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
85     }
86
87     @Provides
88     @Singleton EffectiveModelContextProvider getSchemaContextProvider() {
89         DOMSchemaService schemaService = dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
90         if (schemaService instanceof EffectiveModelContextProvider) {
91             return (EffectiveModelContextProvider) schemaService;
92         }
93         throw new IllegalStateException(
94                 "The schema service isn't a SchemaContextProvider, it's a " + schemaService.getClass());
95     }
96
97     @Provides
98     @Singleton DOMMountPointService getDOMMountPoint() {
99         return new DOMMountPointServiceImpl();
100     }
101
102     @Provides
103     @Singleton DOMNotificationService getDOMNotificationService() {
104         return domNotificationRouter;
105     }
106
107     @Provides
108     @Singleton DOMNotificationPublishService getDOMNotificationPublishService() {
109         return domNotificationRouter;
110     }
111
112     @Provides
113     @Singleton DOMNotificationSubscriptionListenerRegistry getDOMNotificationSubscriptionListenerRegistry() {
114         return domNotificationRouter;
115     }
116
117     @Provides
118     @Singleton DOMRpcService getDOMRpcService(DOMSchemaService schemaService) {
119         return DOMRpcRouter.newInstance(schemaService).getRpcService();
120     }
121
122     @PreDestroy
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..
126     }
127 }