581768c0ed73352d5c1b80fc5e32033dfe266cf2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeCohort.java
1 /*
2  * Copyright (c) 2015 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.annotations.VisibleForTesting;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.primitives.UnsignedLong;
14 import com.google.common.util.concurrent.FutureCallback;
15 import java.util.Optional;
16 import java.util.SortedSet;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22
23 @VisibleForTesting
24 public abstract class ShardDataTreeCohort implements Identifiable<TransactionIdentifier> {
25     public enum State {
26         READY,
27         CAN_COMMIT_PENDING,
28         CAN_COMMIT_COMPLETE,
29         PRE_COMMIT_PENDING,
30         PRE_COMMIT_COMPLETE,
31         COMMIT_PENDING,
32
33         ABORTED,
34         COMMITTED,
35         FAILED,
36     }
37
38     ShardDataTreeCohort() {
39         // Prevent foreign instantiation
40     }
41
42     // FIXME: This leaks internal state generated in preCommit,
43     // should be result of canCommit
44     abstract DataTreeCandidateTip getCandidate();
45
46     abstract DataTreeModification getDataTreeModification();
47
48     abstract Optional<SortedSet<String>> getParticipatingShardNames();
49
50     // FIXME: Should return rebased DataTreeCandidateTip
51     @VisibleForTesting
52     public abstract void canCommit(FutureCallback<Void> callback);
53
54     @VisibleForTesting
55     public abstract void preCommit(FutureCallback<DataTreeCandidate> callback);
56
57     @VisibleForTesting
58     public abstract void abort(FutureCallback<Void> callback);
59
60     @VisibleForTesting
61     public abstract void commit(FutureCallback<UnsignedLong> callback);
62
63     public abstract boolean isFailed();
64
65     public abstract State getState();
66
67     @Override
68     public final String toString() {
69         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
70     }
71
72     ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
73         return toStringHelper.add("id", getIdentifier()).add("state", getState());
74     }
75 }