BUG-5280: fix invalid local transaction replay
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Optional;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20
21 /**
22  * Request to commit a local transaction. Since local transactions do not introduce state on the backend until they
23  * are ready, this message carries a complete set of modifications.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class CommitLocalTransactionRequest
29         extends AbstractLocalTransactionRequest<CommitLocalTransactionRequest> {
30     private static final long serialVersionUID = 1L;
31
32     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
33             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
34             + "aren't serialized. FindBugs does not recognize this.")
35     private final DataTreeModification mod;
36     private final Exception delayedFailure;
37     private final boolean coordinated;
38
39     public CommitLocalTransactionRequest(@Nonnull final TransactionIdentifier identifier, final long sequence,
40             @Nonnull final ActorRef replyTo, @Nonnull final DataTreeModification mod,
41             @Nullable final Exception delayedFailure, final boolean coordinated) {
42         super(identifier, sequence, replyTo);
43         this.mod = Preconditions.checkNotNull(mod);
44         this.delayedFailure = delayedFailure;
45         this.coordinated = coordinated;
46     }
47
48     /**
49      * Return the delayed error detected on the frontend. If this error is present, it will be reported as the result
50      * of the first step of the commit process.
51      *
52      * @return Delayed failure, if present.
53      */
54     public Optional<Exception> getDelayedFailure() {
55         return Optional.ofNullable(delayedFailure);
56     }
57
58     public DataTreeModification getModification() {
59         return mod;
60     }
61
62     /**
63      * Indicate if this is a coordinated, multi-backend request. If this method returns true, the backend must
64      * act as a cohort participating in the three-phase commit (3PC) protocol to commit this transaction.  If this
65      * method returns false, the backend should proceed to commit the transaction and respond once the commit completes
66      * or fails to complete.
67      *
68      * @return Indication of coordinated commit.
69      */
70     public boolean isCoordinated() {
71         return coordinated;
72     }
73
74     @Override
75     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
76         return super.addToStringAttributes(toStringHelper).add("coordinated", coordinated)
77                 .add("delayedError", delayedFailure);
78     }
79 }