Bump mdsal to 4.0.0
[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 package org.opendaylight.controller.cluster.databroker;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableClassToInstanceMap;
14 import com.google.common.collect.ImmutableClassToInstanceMap.Builder;
15 import java.util.EnumMap;
16 import java.util.Map;
17 import java.util.concurrent.atomic.AtomicLong;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
28 import org.opendaylight.mdsal.dom.spi.PingPongMergingDOMDataBroker;
29 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
30 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
31 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public abstract class AbstractDOMBroker extends AbstractDOMTransactionFactory<DOMStore>
37         implements PingPongMergingDOMDataBroker {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBroker.class);
40
41     private final AtomicLong txNum = new AtomicLong();
42     private final AtomicLong chainNum = new AtomicLong();
43     private final ClassToInstanceMap<DOMDataBrokerExtension> extensions;
44
45     private volatile AutoCloseable closeable;
46
47     protected AbstractDOMBroker(final Map<LogicalDatastoreType, DOMStore> datastores) {
48         super(datastores);
49
50         Builder<DOMDataBrokerExtension> extBuilder = ImmutableClassToInstanceMap.builder();
51         if (isSupported(datastores, DOMStoreTreeChangePublisher.class)) {
52             extBuilder.put(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
53                 @Override
54                 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
55                         final DOMDataTreeIdentifier treeId, final L listener) {
56                     DOMStore store = getDOMStore(treeId.getDatastoreType());
57                     return ((DOMStoreTreeChangePublisher) store).registerTreeChangeListener(
58                             treeId.getRootIdentifier(), listener);
59                 }
60             });
61         }
62
63         if (isSupported(datastores, DOMDataTreeCommitCohortRegistry.class)) {
64             extBuilder.put(DOMDataTreeCommitCohortRegistry.class, new DOMDataTreeCommitCohortRegistry() {
65                 @Override
66                 public <T extends DOMDataTreeCommitCohort> DOMDataTreeCommitCohortRegistration<T> registerCommitCohort(
67                         final DOMDataTreeIdentifier path, final T cohort) {
68                     DOMStore store = getDOMStore(path.getDatastoreType());
69                     return ((DOMDataTreeCommitCohortRegistry) store).registerCommitCohort(path, cohort);
70                 }
71             });
72         }
73
74         extensions = extBuilder.build();
75     }
76
77     private static boolean isSupported(final Map<LogicalDatastoreType, DOMStore> datastores,
78             final Class<?> expDOMStoreInterface) {
79         return datastores.values().stream().allMatch(expDOMStoreInterface::isInstance);
80     }
81
82     public void setCloseable(final AutoCloseable closeable) {
83         this.closeable = closeable;
84     }
85
86     @Override
87     @SuppressWarnings("checkstyle:IllegalCatch")
88     public void close() {
89         super.close();
90
91         if (closeable != null) {
92             try {
93                 closeable.close();
94             } catch (Exception e) {
95                 LOG.debug("Error closing instance", e);
96             }
97         }
98     }
99
100     @Override
101     protected Object newTransactionIdentifier() {
102         return "DOM-" + txNum.getAndIncrement();
103     }
104
105     @Override
106     public ClassToInstanceMap<DOMDataBrokerExtension> getExtensions() {
107         return extensions;
108     }
109
110     @Override
111     public DOMTransactionChain createTransactionChain(final DOMTransactionChainListener listener) {
112         checkNotClosed();
113
114         final Map<LogicalDatastoreType, DOMStoreTransactionChain> backingChains =
115                 new EnumMap<>(LogicalDatastoreType.class);
116         for (Map.Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
117             backingChains.put(entry.getKey(), entry.getValue().createTransactionChain());
118         }
119
120         final long chainId = chainNum.getAndIncrement();
121         LOG.debug("Transaction chain {} created with listener {}, backing store chains {}", chainId, listener,
122                 backingChains);
123         return new DOMBrokerTransactionChain(chainId, backingChains, this, listener);
124     }
125
126     private DOMStore getDOMStore(final LogicalDatastoreType type) {
127         DOMStore store = getTxFactories().get(type);
128         checkState(store != null, "Requested logical data store is not available.");
129         return store;
130     }
131 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.