2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter.test;
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;
33 public abstract class AbstractDataBrokerTestCustomizer {
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;
41 public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
42 return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
43 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
44 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
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);
56 public DOMStore createConfigurationDatastore() {
57 final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
58 this.schemaService.registerSchemaContextListener(store);
62 public DOMStore createOperationalDatastore() {
63 final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
64 this.schemaService.registerSchemaContextListener(store);
68 public DOMDataBroker createDOMDataBroker() {
69 return new SerializedDOMDataBroker(getDatastores(), getCommitCoordinatorExecutor());
72 public NotificationService createNotificationService() {
73 return new BindingDOMNotificationServiceAdapter(this.domNotificationRouter,
74 this.bindingToNormalized.getCodecRegistry());
77 public NotificationPublishService createNotificationPublishService() {
78 return new BindingDOMNotificationPublishServiceAdapter(this.domNotificationRouter, this.bindingToNormalized);
81 public abstract ListeningExecutorService getCommitCoordinatorExecutor();
83 public ListeningExecutorService getDataTreeChangeListenerExecutor() {
84 return MoreExecutors.newDirectExecutorService();
87 public DataBroker createDataBroker() {
88 return new BindingDOMDataBrokerAdapter(getDOMDataBroker(), this.bindingToNormalized);
91 public BindingToNormalizedNodeCodec getBindingToNormalized() {
92 return this.bindingToNormalized;
95 public DOMSchemaService getSchemaService() {
96 return this.schemaService;
99 public DOMDataBroker getDOMDataBroker() {
100 if (this.domDataBroker == null) {
101 this.domDataBroker = createDOMDataBroker();
103 return this.domDataBroker;
106 private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
107 if (this.datastores == null) {
108 this.datastores = createDatastores();
110 return this.datastores;
113 public void updateSchema(final SchemaContext ctx) {
114 this.schemaService.changeSchema(ctx);
117 public DOMNotificationRouter getDomNotificationRouter() {
118 return this.domNotificationRouter;