af5611921d37bd7a1c28d348536e72a17fb1145d
[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.MockAdapterContext;
21 import org.opendaylight.mdsal.binding.dom.adapter.test.util.MockSchemaService;
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 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30
31 public abstract class AbstractDataBrokerTestCustomizer {
32
33     private DOMDataBroker domDataBroker;
34     private final DOMNotificationRouter domNotificationRouter;
35     private final MockSchemaService schemaService;
36     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
37     private final MockAdapterContext adapterContext;
38
39     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
40         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
41                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
42                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
43                 .build();
44     }
45
46     public AbstractDataBrokerTestCustomizer() {
47         schemaService = new MockSchemaService();
48         adapterContext = new MockAdapterContext();
49         schemaService.registerSchemaContextListener(adapterContext);
50         domNotificationRouter = DOMNotificationRouter.create(16);
51     }
52
53     public DOMStore createConfigurationDatastore() {
54         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
55         schemaService.registerSchemaContextListener(store);
56         return store;
57     }
58
59     public DOMStore createOperationalDatastore() {
60         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
61         schemaService.registerSchemaContextListener(store);
62         return store;
63     }
64
65     public DOMDataBroker createDOMDataBroker() {
66         return new SerializedDOMDataBroker(getDatastores(), getCommitCoordinatorExecutor());
67     }
68
69     public NotificationService createNotificationService() {
70         return new BindingDOMNotificationServiceAdapter(adapterContext, domNotificationRouter);
71     }
72
73     public NotificationPublishService createNotificationPublishService() {
74         return new BindingDOMNotificationPublishServiceAdapter(adapterContext, domNotificationRouter);
75     }
76
77     public abstract ListeningExecutorService getCommitCoordinatorExecutor();
78
79     public ListeningExecutorService getDataTreeChangeListenerExecutor() {
80         return MoreExecutors.newDirectExecutorService();
81     }
82
83     public DataBroker createDataBroker() {
84         return new BindingDOMDataBrokerAdapter(adapterContext, getDOMDataBroker());
85     }
86
87     public AdapterContext getAdapterContext() {
88         return adapterContext;
89     }
90
91     public DOMSchemaService getSchemaService() {
92         return schemaService;
93     }
94
95     public DOMDataBroker getDOMDataBroker() {
96         if (domDataBroker == null) {
97             domDataBroker = createDOMDataBroker();
98         }
99         return domDataBroker;
100     }
101
102     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
103         if (datastores == null) {
104             datastores = createDatastores();
105         }
106         return datastores;
107     }
108
109     public void updateSchema(final EffectiveModelContext ctx) {
110         schemaService.changeSchema(ctx);
111     }
112
113     public DOMNotificationRouter getDomNotificationRouter() {
114         return domNotificationRouter;
115     }
116 }