2 * Copyright (c) 2016 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.controller.cluster.databroker;
10 import com.google.common.base.Preconditions;
12 import java.util.WeakHashMap;
13 import javax.annotation.concurrent.GuardedBy;
14 import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
15 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
16 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
17 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * An implementation of {@link DOMStoreTransactionChain} backed by a {@link ClientLocalHistory}.
29 * @author Robert Varga
31 final class ClientBackedTransactionChain implements DOMStoreTransactionChain {
32 private static final Logger LOG = LoggerFactory.getLogger(ClientBackedTransactionChain.class);
35 private final Map<AbstractClientHandle<?>, Boolean> openSnapshots = new WeakHashMap<>();
37 private final ClientLocalHistory history;
38 private final boolean debugAllocation;
40 ClientBackedTransactionChain(final ClientLocalHistory history, final boolean debugAllocation) {
41 this.history = Preconditions.checkNotNull(history);
42 this.debugAllocation = debugAllocation;
46 public DOMStoreReadTransaction newReadOnlyTransaction() {
47 return new ClientBackedReadTransaction(createSnapshot(), this, allocationContext());
51 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
52 return new ClientBackedReadWriteTransaction(createTransaction(), allocationContext());
56 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
57 return new ClientBackedWriteTransaction(createTransaction(), allocationContext());
61 public synchronized void close() {
62 for (AbstractClientHandle<?> snap : openSnapshots.keySet()) {
63 LOG.warn("Aborting unclosed transaction {}", snap.getIdentifier());
66 openSnapshots.clear();
71 synchronized void snapshotClosed(final ClientSnapshot clientTransaction) {
72 openSnapshots.remove(clientTransaction);
75 private ClientSnapshot createSnapshot() {
77 return recordSnapshot(history.takeSnapshot());
78 } catch (org.opendaylight.mdsal.common.api.TransactionChainClosedException e) {
79 throw new TransactionChainClosedException("Transaction chain has been closed", e);
83 private ClientTransaction createTransaction() {
85 return recordSnapshot(history.createTransaction());
86 } catch (org.opendaylight.mdsal.common.api.TransactionChainClosedException e) {
87 throw new TransactionChainClosedException("Transaction chain has been closed", e);
91 private Throwable allocationContext() {
92 return debugAllocation ? new Throwable("allocated at") : null;
95 private synchronized <T extends AbstractClientHandle<?>> T recordSnapshot(final T snapshot) {
96 openSnapshots.put(snapshot, Boolean.TRUE);