be85f689eb1d3b6b0b89dacbeb96342534be90f3
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / StandaloneFrontendHistory.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.primitives.UnsignedLong;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongSet;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24
25 /**
26  * Standalone transaction specialization of {@link AbstractFrontendHistory}. There can be multiple open transactions
27  * and they are submitted in any order.
28  *
29  * @author Robert Varga
30  */
31 final class StandaloneFrontendHistory extends AbstractFrontendHistory {
32     private final LocalHistoryIdentifier identifier;
33     private final ShardDataTree tree;
34
35     private StandaloneFrontendHistory(final String persistenceId, final ClientIdentifier clientId,
36             final ShardDataTree tree, final Map<UnsignedLong, Boolean> closedTransactions,
37             final UnsignedLongSet purgedTransactions) {
38         super(persistenceId, tree, closedTransactions, purgedTransactions);
39         identifier = new LocalHistoryIdentifier(clientId, 0);
40         this.tree = requireNonNull(tree);
41     }
42
43     static @NonNull StandaloneFrontendHistory create(final String persistenceId, final ClientIdentifier clientId,
44             final ShardDataTree tree) {
45         return new StandaloneFrontendHistory(persistenceId, clientId, tree, ImmutableMap.of(), UnsignedLongSet.of());
46     }
47
48     static @NonNull StandaloneFrontendHistory recreate(final String persistenceId, final ClientIdentifier clientId,
49             final ShardDataTree tree, final Map<UnsignedLong, Boolean> closedTransactions,
50             final UnsignedLongSet purgedTransactions) {
51         return new StandaloneFrontendHistory(persistenceId, clientId, tree, new HashMap<>(closedTransactions),
52             purgedTransactions.copy());
53     }
54
55     @Override
56     public LocalHistoryIdentifier getIdentifier() {
57         return identifier;
58     }
59
60     @Override
61     FrontendTransaction createOpenSnapshot(final TransactionIdentifier id) {
62         return FrontendReadOnlyTransaction.create(this, tree.newReadOnlyTransaction(id));
63     }
64
65     @Override
66     FrontendTransaction createOpenTransaction(final TransactionIdentifier id) {
67         return FrontendReadWriteTransaction.createOpen(this, tree.newReadWriteTransaction(id));
68     }
69
70     @Override
71     FrontendTransaction createReadyTransaction(final TransactionIdentifier id, final DataTreeModification mod) {
72         return FrontendReadWriteTransaction.createReady(this, id, mod);
73     }
74
75     @Override
76     ShardDataTreeCohort createFailedCohort(final TransactionIdentifier id, final DataTreeModification mod,
77             final Exception failure) {
78         return tree.createFailedCohort(id, mod, failure);
79     }
80
81     @Override
82     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier id, final DataTreeModification mod,
83             final Optional<SortedSet<String>> participatingShardNames) {
84         return tree.createReadyCohort(id, mod, participatingShardNames);
85     }
86 }