73d2c9f805a530fc2c5de9ea6bcee49bf9bcb481
[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 java.util.Optional;
18 import java.util.SortedSet;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.RequestException;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23
24
25 /**
26  * Chained transaction specialization of {@link AbstractFrontendHistory}. It prevents concurrent open transactions.
27  *
28  * @author Robert Varga
29  */
30 final class LocalFrontendHistory extends AbstractFrontendHistory {
31     private final ShardDataTreeTransactionChain chain;
32
33     private LocalFrontendHistory(final String persistenceId, final ShardDataTree tree,
34             final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
35             final RangeSet<UnsignedLong> purgedTransactions) {
36         super(persistenceId, tree, closedTransactions, purgedTransactions);
37         this.chain = Preconditions.checkNotNull(chain);
38     }
39
40     static LocalFrontendHistory create(final String persistenceId, final ShardDataTree tree,
41             final ShardDataTreeTransactionChain chain) {
42         return new LocalFrontendHistory(persistenceId, tree, chain, ImmutableMap.of(), TreeRangeSet.create());
43     }
44
45     static LocalFrontendHistory recreate(final String persistenceId, final ShardDataTree tree,
46             final ShardDataTreeTransactionChain chain, final Map<UnsignedLong, Boolean> closedTransactions,
47             final RangeSet<UnsignedLong> purgedTransactions) {
48         return new LocalFrontendHistory(persistenceId, tree, chain, new HashMap<>(closedTransactions),
49             TreeRangeSet.create(purgedTransactions));
50     }
51
52     @Override
53     public LocalHistoryIdentifier getIdentifier() {
54         return chain.getIdentifier();
55     }
56
57     @Override
58     FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) throws RequestException {
59         return FrontendReadOnlyTransaction.create(this, chain.newReadOnlyTransaction(id));
60     }
61
62     @Override
63     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) throws RequestException {
64         return FrontendReadWriteTransaction.createOpen(this, chain.newReadWriteTransaction(id));
65     }
66
67     @Override
68     FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod)
69             throws RequestException {
70         return FrontendReadWriteTransaction.createReady(this, id, mod);
71     }
72
73     @Override
74     ShardDataTreeCohort createFailedCohort(final TransactionIdentifier id, final DataTreeModification mod,
75             final Exception failure) {
76         return chain.createFailedCohort(id, mod, failure);
77     }
78
79     @Override
80     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod,
81             final Optional<SortedSet<String>> participatingShardNames) {
82         return chain.createReadyCohort(id, mod, participatingShardNames);
83     }
84 }