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 java.util.ArrayList;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.concepts.Identifiable;
21 * A reusable builder for creating {@link ModifyTransactionRequest} message instances. Its internal state is reset when
22 * {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT thread-safe.
24 public final class ModifyTransactionRequestBuilder implements Identifiable<TransactionIdentifier> {
25 private final List<TransactionModification> modifications = new ArrayList<>(1);
26 private final @NonNull TransactionIdentifier identifier;
27 private final ActorRef replyTo;
29 private PersistenceProtocol protocol;
30 private boolean haveSequence;
31 private long sequence;
33 public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
34 this.identifier = requireNonNull(identifier);
35 this.replyTo = requireNonNull(replyTo);
39 public TransactionIdentifier getIdentifier() {
43 private void checkNotFinished() {
44 checkState(protocol == null, "Batch has already been finished");
47 public void addModification(final TransactionModification modification) {
49 modifications.add(requireNonNull(modification));
52 public void setSequence(final long sequence) {
53 checkState(!haveSequence, "Sequence has already been set");
54 this.sequence = sequence;
58 public void setAbort() {
60 // Transaction is being aborted, no need to transmit operations
61 modifications.clear();
62 protocol = PersistenceProtocol.ABORT;
65 public void setCommit(final boolean coordinated) {
67 protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
70 public void setReady() {
72 protocol = PersistenceProtocol.READY;
76 return modifications.size();
79 public @NonNull ModifyTransactionRequest build() {
80 checkState(haveSequence, "Request sequence has not been set");
82 final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
84 modifications.clear();