2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.commands;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ActorRef;
14 import com.google.common.annotations.Beta;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.yangtools.concepts.Identifiable;
22 * A reusable builder for creating {@link ModifyTransactionRequest} message instances. Its internal state is reset when
23 * {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT thread-safe.
25 * @author Robert Varga
28 public final class ModifyTransactionRequestBuilder implements Identifiable<TransactionIdentifier> {
29 private final List<TransactionModification> modifications = new ArrayList<>(1);
30 private final TransactionIdentifier identifier;
31 private final ActorRef replyTo;
33 private PersistenceProtocol protocol;
34 private boolean haveSequence;
35 private long sequence;
37 public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
38 this.identifier = requireNonNull(identifier);
39 this.replyTo = requireNonNull(replyTo);
43 public TransactionIdentifier getIdentifier() {
47 private void checkNotFinished() {
48 checkState(protocol == null, "Batch has already been finished");
51 public void addModification(final TransactionModification modification) {
53 modifications.add(requireNonNull(modification));
56 public void setSequence(final long sequence) {
57 checkState(!haveSequence, "Sequence has already been set");
58 this.sequence = sequence;
62 public void setAbort() {
64 // Transaction is being aborted, no need to transmit operations
65 modifications.clear();
66 protocol = PersistenceProtocol.ABORT;
69 public void setCommit(final boolean coordinated) {
71 protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
74 public void setReady() {
76 protocol = PersistenceProtocol.READY;
80 return modifications.size();
83 public @NonNull ModifyTransactionRequest build() {
84 checkState(haveSequence, "Request sequence has not been set");
86 final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
88 modifications.clear();