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 javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.yangtools.concepts.Builder;
18 import org.opendaylight.yangtools.concepts.Identifiable;
21 * A reusable {@link Builder} for creating {@link ModifyTransactionRequest} message instances. Its internal state is
22 * reset when {@link #build()} is invoked, hence it can be used to create a sequence of messages.
24 * @author Robert Varga
28 public final class ModifyTransactionRequestBuilder implements Builder<ModifyTransactionRequest>,
29 Identifiable<TransactionIdentifier> {
30 private final List<TransactionModification> modifications = new ArrayList<>(1);
31 private final TransactionIdentifier identifier;
32 private final ActorRef replyTo;
34 private PersistenceProtocol protocol;
35 private boolean haveSequence;
36 private long sequence;
38 public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
39 this.identifier = Preconditions.checkNotNull(identifier);
40 this.replyTo = Preconditions.checkNotNull(replyTo);
44 public TransactionIdentifier getIdentifier() {
48 private void checkNotFinished() {
49 Preconditions.checkState(protocol == null, "Batch has already been finished");
52 public void addModification(final TransactionModification modification) {
54 modifications.add(Preconditions.checkNotNull(modification));
57 public void setSequence(final long sequence) {
58 Preconditions.checkState(!haveSequence, "Sequence has already been set");
59 this.sequence = sequence;
63 public void setAbort() {
65 // Transaction is being aborted, no need to transmit operations
66 modifications.clear();
67 protocol = PersistenceProtocol.ABORT;
70 public void setCommit(final boolean coordinated) {
72 protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
75 public void setReady() {
77 protocol = PersistenceProtocol.READY;
81 return modifications.size();
85 public ModifyTransactionRequest build() {
86 Preconditions.checkState(haveSequence, "Request sequence has not been set");
88 final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
90 modifications.clear();