Refactor AbstractDataBrokerTestCustomizer
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / AbstractDataBrokerTestCustomizer.java
1 /*
2  * Copyright (c) 2017 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.dom.adapter.test;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
15 import org.opendaylight.mdsal.binding.api.NotificationService;
16 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
17 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataBrokerAdapter;
18 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMNotificationPublishServiceAdapter;
19 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMNotificationServiceAdapter;
20 import org.opendaylight.mdsal.binding.dom.adapter.test.util.MockSchemaService;
21 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.mdsal.dom.broker.DOMNotificationRouter;
26 import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
28 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
29
30 public abstract class AbstractDataBrokerTestCustomizer {
31     private final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(16);
32     private final MockSchemaService schemaService = new MockSchemaService();
33
34     private DOMDataBroker domDataBroker;
35     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
36
37     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
38         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
39                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
40                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
41                 .build();
42     }
43
44     public DOMStore createConfigurationDatastore() {
45         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
46         schemaService.registerSchemaContextListener(store);
47         return store;
48     }
49
50     public DOMStore createOperationalDatastore() {
51         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
52         schemaService.registerSchemaContextListener(store);
53         return store;
54     }
55
56     public DOMDataBroker createDOMDataBroker() {
57         return new SerializedDOMDataBroker(getDatastores(), getCommitCoordinatorExecutor());
58     }
59
60     public NotificationService createNotificationService() {
61         return new BindingDOMNotificationServiceAdapter(schemaService, domNotificationRouter);
62     }
63
64     public NotificationPublishService createNotificationPublishService() {
65         return new BindingDOMNotificationPublishServiceAdapter(schemaService, domNotificationRouter);
66     }
67
68     public abstract ListeningExecutorService getCommitCoordinatorExecutor();
69
70     public ListeningExecutorService getDataTreeChangeListenerExecutor() {
71         return MoreExecutors.newDirectExecutorService();
72     }
73
74     public DataBroker createDataBroker() {
75         return new BindingDOMDataBrokerAdapter(schemaService, getDOMDataBroker());
76     }
77
78     public AdapterContext getAdapterContext() {
79         return schemaService;
80     }
81
82     public DOMSchemaService getSchemaService() {
83         return schemaService;
84     }
85
86     public DOMDataBroker getDOMDataBroker() {
87         if (domDataBroker == null) {
88             domDataBroker = createDOMDataBroker();
89         }
90         return domDataBroker;
91     }
92
93     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
94         if (datastores == null) {
95             datastores = createDatastores();
96         }
97         return datastores;
98     }
99
100     public void updateSchema(final BindingRuntimeContext ctx) {
101         schemaService.changeSchema(ctx);
102     }
103
104     public DOMNotificationRouter getDomNotificationRouter() {
105         return domNotificationRouter;
106     }
107 }