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