Cleanup DataBrokerTestModule error reporting
[mdsal.git] / binding / mdsal-binding-test-utils / src / main / java / org / opendaylight / mdsal / binding / testutils / DataBrokerTestModule.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.testutils;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Throwables;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
14 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
17 import org.opendaylight.mdsal.dom.broker.DOMNotificationRouter;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
19
20 @Beta
21 public class DataBrokerTestModule {
22     private final boolean useMTDataTreeChangeListenerExecutor;
23
24     private AbstractConcurrentDataBrokerTest dataBrokerTest;
25
26     public DataBrokerTestModule(final boolean useMTDataTreeChangeListenerExecutor) {
27         this.useMTDataTreeChangeListenerExecutor = useMTDataTreeChangeListenerExecutor;
28     }
29
30     public static DataBroker dataBroker() {
31         return new DataBrokerTestModule(false).getDataBroker();
32     }
33
34     // Suppress IllegalCatch because of AbstractDataBrokerTest (change later)
35     @SuppressWarnings("checkstyle:IllegalCatch")
36     public DataBroker getDataBroker() {
37         try {
38             // This is a little bit "upside down" - in the future,
39             // we should probably put what is in AbstractDataBrokerTest
40             // into this DataBrokerTestModule, and make AbstractDataBrokerTest
41             // use it, instead of the way around it currently is (the opposite);
42             // this is just for historical reasons... and works for now.
43             dataBrokerTest = new AbstractConcurrentDataBrokerTest(useMTDataTreeChangeListenerExecutor) { };
44             dataBrokerTest.setup();
45             return dataBrokerTest.getDataBroker();
46         } catch (Exception e) {
47             Throwables.throwIfUnchecked(e);
48             throw new IllegalStateException(e);
49         }
50     }
51
52     public DOMDataBroker getDOMDataBroker() {
53         return dataBrokerTest.getDomBroker();
54     }
55
56     public CurrentAdapterSerializer getBindingToNormalizedNodeCodec() {
57         return dataBrokerTest.getDataBrokerTestCustomizer().getAdapterContext().currentSerializer();
58     }
59
60     public DOMNotificationRouter getDOMNotificationRouter() {
61         return dataBrokerTest.getDataBrokerTestCustomizer().getDomNotificationRouter();
62     }
63
64     public DOMSchemaService getSchemaService() {
65         return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService();
66     }
67
68     public EffectiveModelContextProvider getSchemaContextProvider() {
69         return dataBrokerTest.getDataBrokerTestCustomizer().getSchemaService()::getGlobalContext;
70     }
71 }