Merge "Allow no payload for RPCs with no input"
[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.SchemaContextProvider;
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>.
37  *
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.
43  *
44  * @author Michael Vorburger.ch
45  */
46 public class InMemoryMdsalModule extends AbstractModule {
47
48     private static final int NOTIFICATION_SERVICE_QUEUE_DEPTH = 128;
49
50     private final AbstractBaseDataBrokerTest dataBrokerTest;
51     private final DOMNotificationRouter domNotificationRouter;
52
53     public InMemoryMdsalModule() throws Exception {
54         dataBrokerTest = new AbstractConcurrentDataBrokerTest(true) { // NOT AbstractDataBrokerTest
55         };
56         dataBrokerTest.setup();
57
58         domNotificationRouter = DOMNotificationRouter.create(NOTIFICATION_SERVICE_QUEUE_DEPTH);
59     }
60
61     @Override
62     protected void configure() {
63     }
64
65     @Provides
66     @Singleton
67     DataBroker getDataBroker() {
68         return dataBrokerTest.getDataBroker();
69     }
70
71     @Provides
72     @Singleton DOMDataBroker getDOMDataBroker() {
73         return dataBrokerTest.getDomBroker();
74     }
75
76     @Provides
77     @Singleton DOMNotificationRouter getDOMNotificationRouter() {
78         return dataBrokerTest.getDataBrokerTestCustomizer().getDomNotificationRouter();
79     }
80
81     @Provides
82     @Singleton DOMSchemaService getSchemaService() {
83         return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
84     }
85
86     @Provides
87     @Singleton SchemaContextProvider getSchemaContextProvider() {
88         DOMSchemaService schemaService = dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
89         if (schemaService instanceof SchemaContextProvider) {
90             return (SchemaContextProvider) schemaService;
91         }
92         throw new IllegalStateException(
93                 "The schema service isn't a SchemaContextProvider, it's a " + schemaService.getClass());
94     }
95
96     @Provides
97     @Singleton DOMMountPointService getDOMMountPoint() {
98         return new DOMMountPointServiceImpl();
99     }
100
101     @Provides
102     @Singleton DOMNotificationService getDOMNotificationService() {
103         return domNotificationRouter;
104     }
105
106     @Provides
107     @Singleton DOMNotificationPublishService getDOMNotificationPublishService() {
108         return domNotificationRouter;
109     }
110
111     @Provides
112     @Singleton DOMNotificationSubscriptionListenerRegistry getDOMNotificationSubscriptionListenerRegistry() {
113         return domNotificationRouter;
114     }
115
116     @Provides
117     @Singleton DOMRpcService getDOMRpcService(DOMSchemaService schemaService) {
118         return DOMRpcRouter.newInstance(schemaService).getRpcService();
119     }
120
121     @PreDestroy
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..
125     }
126 }