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 akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16 import org.opendaylight.yangtools.concepts.Builder;
17 import org.opendaylight.yangtools.concepts.Identifiable;
20 * A reusable {@link Builder} for creating {@link ModifyTransactionRequest} message instances. Its internal state is
21 * reset when {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT
24 * @author Robert Varga
27 public final class ModifyTransactionRequestBuilder implements Builder<ModifyTransactionRequest>,
28 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 = Preconditions.checkNotNull(identifier);
39 this.replyTo = Preconditions.checkNotNull(replyTo);
43 public TransactionIdentifier getIdentifier() {
47 private void checkNotFinished() {
48 Preconditions.checkState(protocol == null, "Batch has already been finished");
51 public void addModification(final TransactionModification modification) {
53 modifications.add(Preconditions.checkNotNull(modification));
56 public void setSequence(final long sequence) {
57 Preconditions.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();
84 public ModifyTransactionRequest build() {
85 Preconditions.checkState(haveSequence, "Request sequence has not been set");
87 final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
89 modifications.clear();