68191fbbc2827d182cf4fc53c5bf77cfb8c64f42
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBroker.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.controller.cluster.databroker;
10
11 import static com.google.common.base.Preconditions.checkState;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import java.util.EnumMap;
16 import java.util.Map;
17 import java.util.concurrent.atomic.AtomicLong;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeCommitCohortRegistry;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 abstract class AbstractDOMBroker extends AbstractDOMTransactionFactory<DOMStore>
39         implements DOMDataBroker {
40
41     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBroker.class);
42
43     private final AtomicLong txNum = new AtomicLong();
44     private final AtomicLong chainNum = new AtomicLong();
45     private final Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> extensions;
46     private volatile AutoCloseable closeable;
47
48     protected AbstractDOMBroker(final Map<LogicalDatastoreType, DOMStore> datastores) {
49         super(datastores);
50
51         Builder<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> extBuilder = ImmutableMap.builder();
52         if (isSupported(datastores, DOMStoreTreeChangePublisher.class)) {
53             extBuilder.put(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
54                 @Override
55                 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
56                         final DOMDataTreeIdentifier treeId, final L listener) {
57                     DOMStore store = getTxFactories().get(treeId.getDatastoreType());
58                     checkState(store != null, "Requested logical data store is not available.");
59
60                     return ((DOMStoreTreeChangePublisher) store).registerTreeChangeListener(
61                             treeId.getRootIdentifier(), listener);
62                 }
63             });
64         }
65
66         if (isSupported(datastores, org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry.class)) {
67             extBuilder.put(DOMDataTreeCommitCohortRegistry.class, new DOMDataTreeCommitCohortRegistry() {
68                 @Override
69                 public <T extends DOMDataTreeCommitCohort> DOMDataTreeCommitCohortRegistration<T> registerCommitCohort(
70                         org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier path, T cohort) {
71                     DOMStore store = getTxFactories().get(toLegacy(path.getDatastoreType()));
72                     checkState(store != null, "Requested logical data store is not available.");
73
74                     return ((org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry) store)
75                             .registerCommitCohort(path, cohort);
76                 }
77             });
78         }
79
80         extensions = extBuilder.build();
81     }
82
83     private static LogicalDatastoreType toLegacy(org.opendaylight.mdsal.common.api.LogicalDatastoreType datastoreType) {
84         switch (datastoreType) {
85             case CONFIGURATION:
86                 return LogicalDatastoreType.CONFIGURATION;
87             case OPERATIONAL:
88                 return LogicalDatastoreType.OPERATIONAL;
89             default:
90                 throw new IllegalArgumentException("Unsupported data store type: " + datastoreType);
91         }
92     }
93
94     private static boolean isSupported(Map<LogicalDatastoreType, DOMStore> datastores,
95             Class<?> expDOMStoreInterface) {
96         for (DOMStore ds : datastores.values()) {
97             if (!expDOMStoreInterface.isAssignableFrom(ds.getClass())) {
98                 return false;
99             }
100         }
101
102         return true;
103     }
104
105     public void setCloseable(final AutoCloseable closeable) {
106         this.closeable = closeable;
107     }
108
109     @Override
110     @SuppressWarnings("checkstyle:IllegalCatch")
111     public void close() {
112         super.close();
113
114         if (closeable != null) {
115             try {
116                 closeable.close();
117             } catch (Exception e) {
118                 LOG.debug("Error closing instance", e);
119             }
120         }
121     }
122
123     @Override
124     protected Object newTransactionIdentifier() {
125         return "DOM-" + txNum.getAndIncrement();
126     }
127
128     @Override
129     public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
130             final YangInstanceIdentifier path, final DOMDataChangeListener listener,
131             final DataChangeScope triggeringScope) {
132         DOMStore potentialStore = getTxFactories().get(store);
133         checkState(potentialStore != null, "Requested logical data store is not available.");
134         return potentialStore.registerChangeListener(path, listener, triggeringScope);
135     }
136
137     @Override
138     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
139         return extensions;
140     }
141
142     @Override
143     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
144         checkNotClosed();
145
146         final Map<LogicalDatastoreType, DOMStoreTransactionChain> backingChains =
147                 new EnumMap<>(LogicalDatastoreType.class);
148         for (Map.Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
149             backingChains.put(entry.getKey(), entry.getValue().createTransactionChain());
150         }
151
152         final long chainId = chainNum.getAndIncrement();
153         LOG.debug("Transaction chain {} created with listener {}, backing store chains {}", chainId, listener,
154                 backingChains);
155         return new DOMBrokerTransactionChain(chainId, backingChains, this, listener);
156     }
157 }