BUG-8371: Respond to CreateLocalHistoryRequest after replication
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalFrontendHistory.java
1 /*
2  * Copyright (c) 2016 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.datastore;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.RangeSet;
13 import com.google.common.collect.TreeRangeSet;
14 import com.google.common.primitives.UnsignedLong;
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.RequestException;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21
22
23 /**
24  * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
25  *
26  * @author Robert Varga
27  */
28 final class LocalFrontendHistory extends AbstractFrontendHistory {
29     private final ShardDataTreeTransactionChain chain;
30
31     private LocalFrontendHistory(final String persistenceId, final ShardDataTree tree,
32             final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
33             final RangeSet<UnsignedLong> purgedTransactions) {
34         super(persistenceId, tree, closedTransactions, purgedTransactions);
35         this.chain = Preconditions.checkNotNull(chain);
36     }
37
38     static LocalFrontendHistory create(final String persistenceId, final ShardDataTree tree,
39             final ShardDataTreeTransactionChain chain) {
40         return new LocalFrontendHistory(persistenceId, tree, chain, ImmutableMap.of(), TreeRangeSet.create());
41     }
42
43     static LocalFrontendHistory recreate(final String persistenceId, final ShardDataTree tree,
44             final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
45             final RangeSet<UnsignedLong> purgedTransactions) {
46         return new LocalFrontendHistory(persistenceId, tree, chain, new HashMap<>(closedTransactions),
47             TreeRangeSet.create(purgedTransactions));
48     }
49
50     @Override
51     public LocalHistoryIdentifier getIdentifier() {
52         return chain.getIdentifier();
53     }
54
55     @Override
56     FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
57         return FrontendReadOnlyTransaction.create(this, chain.newReadOnlyTransaction(id));
58     }
59
60     @Override
61     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
62         return FrontendReadWriteTransaction.createOpen(this, chain.newReadWriteTransaction(id));
63     }
64
65     @Override
66     FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
67             throws RequestException {
68         return FrontendReadWriteTransaction.createReady(this, id, mod);
69     }
70
71     @Override
72     ShardDataTreeCohort createFailedCohort(final TransactionIdentifier id, final DataTreeModification mod,
73             final Exception failure) {
74         return chain.createFailedCohort(id, mod, failure);
75     }
76
77     @Override
78     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod) {
79         return chain.createReadyCohort(id, mod);
80     }
81 }