2 * Copyright (c) 2014 Cisco Systems, 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.dom.spi;
10 import com.google.common.collect.ImmutableList;
11 import java.util.EnumMap;
12 import java.util.List;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
19 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class AbstractDOMDataBroker extends AbstractDOMForwardedTransactionFactory<DOMStore>
27 implements PingPongMergingDOMDataBroker {
28 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMDataBroker.class);
30 private final AtomicLong txNum = new AtomicLong();
31 private final AtomicLong chainNum = new AtomicLong();
32 private final @NonNull List<Extension> supportedExtensions;
34 protected AbstractDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores) {
37 final var builder = ImmutableList.<Extension>builder();
38 if (isSupported(datastores, DOMStoreTreeChangePublisher.class)) {
39 builder.add((DOMDataTreeChangeService) (treeId, listener) -> {
40 final var dsType = treeId.datastore();
41 if (getTxFactories().get(dsType) instanceof DOMStoreTreeChangePublisher publisher) {
42 return publisher.registerTreeChangeListener(treeId.path(), listener);
44 throw new IllegalStateException("Publisher for " + dsType + " data store is not available");
47 if (isSupported(datastores, DOMDataTreeCommitCohortRegistry.class)) {
48 builder.add((DOMDataTreeCommitCohortRegistry) (path, cohort) -> {
49 final var dsType = path.datastore();
50 if (getTxFactories().get(dsType) instanceof DOMDataTreeCommitCohortRegistry registry) {
51 return registry.registerCommitCohort(path, cohort);
53 throw new IllegalStateException("Cohort registry for " + dsType + " data store is not available");
57 supportedExtensions = builder.build();
61 public final List<Extension> supportedExtensions() {
62 return supportedExtensions;
66 public DOMTransactionChain createTransactionChain() {
69 final var delegates = new EnumMap<LogicalDatastoreType, DOMStoreTransactionChain>(LogicalDatastoreType.class);
70 for (var entry : getTxFactories().entrySet()) {
71 delegates.put(entry.getKey(), entry.getValue().createTransactionChain());
74 final long chainId = chainNum.getAndIncrement();
75 LOG.debug("Transactoin chain {} created, backing store chains {}", chainId, delegates);
76 return new DOMDataBrokerTransactionChainImpl(chainId, delegates, this);
80 protected final Object newTransactionIdentifier() {
81 return "DOM-" + txNum.getAndIncrement();
84 private static boolean isSupported(final Map<LogicalDatastoreType, DOMStore> datastores,
85 final Class<?> expDOMStoreInterface) {
86 return datastores.values().stream().allMatch(expDOMStoreInterface::isInstance);