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