97381cc267722dbd8afecd80865054d4de32dd3f
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / CommitLocalTransactionRequest.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.access.commands;
9
10 import akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17
18 /**
19  * Request to commit a local transaction. Since local transactions do not introduce state on the backend until they
20  * are ready, this message carries a complete set of modifications.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 public final class CommitLocalTransactionRequest extends AbstractLocalTransactionRequest<CommitLocalTransactionRequest> {
26     private static final long serialVersionUID = 1L;
27     private final DataTreeModification mod;
28     private final boolean coordinated;
29
30     public CommitLocalTransactionRequest(final @Nonnull TransactionIdentifier identifier, final long sequence,
31             final @Nonnull ActorRef replyTo, final @Nonnull DataTreeModification mod, final boolean coordinated) {
32         this(identifier, sequence, 0, replyTo, mod, coordinated);
33     }
34
35     CommitLocalTransactionRequest(final @Nonnull TransactionIdentifier identifier, final long sequence,
36             final long retry, final @Nonnull ActorRef replyTo, final @Nonnull DataTreeModification mod,
37             final boolean coordinated) {
38         super(identifier, sequence, retry, replyTo);
39         this.mod = Preconditions.checkNotNull(mod);
40         this.coordinated = coordinated;
41     }
42
43
44     private CommitLocalTransactionRequest(final CommitLocalTransactionRequest request, final long retry) {
45         super(request, retry);
46         this.mod = request.mod;
47         this.coordinated = request.coordinated;
48     }
49
50     public DataTreeModification getModification() {
51         return mod;
52     }
53
54     /**
55      * Indicate if this is a coordinated, multi-backend request. If this method returns true, the backend must
56      * act as a cohort participating in the three-phase commit (3PC) protocol to commit this transaction.  If this
57      * method returns false, the backend should proceed to commit the transaction and respond once the commit completes
58      * or fails to complete.
59      *
60      * @return Indication of coordinated commit.
61      */
62     public boolean isCoordinated() {
63         return coordinated;
64     }
65
66     @Override
67     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
68         return super.addToStringAttributes(toStringHelper).add("coordinated", coordinated);
69     }
70
71     @Override
72     protected CommitLocalTransactionRequest cloneAsRetry(final long retry) {
73         return new CommitLocalTransactionRequest(this, retry);
74     }
75 }