4d69c99603ff1701551b95aa5dd172789f50e0a2
[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 = new DOMNotificationRouter(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.notificationService());
62     }
63
64     public NotificationPublishService createNotificationPublishService() {
65         return new BindingDOMNotificationPublishServiceAdapter(schemaService,
66             domNotificationRouter.notificationPublishService());
67     }
68
69     public abstract ListeningExecutorService getCommitCoordinatorExecutor();
70
71     public ListeningExecutorService getDataTreeChangeListenerExecutor() {
72         return MoreExecutors.newDirectExecutorService();
73     }
74
75     public DataBroker createDataBroker() {
76         return new BindingDOMDataBrokerAdapter(schemaService, getDOMDataBroker());
77     }
78
79     public AdapterContext getAdapterContext() {
80         return schemaService;
81     }
82
83     public DOMSchemaService getSchemaService() {
84         return schemaService;
85     }
86
87     public DOMDataBroker getDOMDataBroker() {
88         if (domDataBroker == null) {
89             domDataBroker = createDOMDataBroker();
90         }
91         return domDataBroker;
92     }
93
94     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
95         if (datastores == null) {
96             datastores = createDatastores();
97         }
98         return datastores;
99     }
100
101     public void updateSchema(final BindingRuntimeContext ctx) {
102         schemaService.changeSchema(ctx);
103     }
104
105     public DOMNotificationRouter getDomNotificationRouter() {
106         return domNotificationRouter;
107     }
108 }