88c6a8e24eb3455b3a541a27441faf24ef110727
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequestBuilder.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 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;
19
20 /**
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.
23  *
24  * @author Robert Varga
25  */
26 @Beta
27 @NotThreadSafe
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;
33     private PersistenceProtocol protocol = null;
34     private long sequence;
35
36     public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
37         this.identifier = Preconditions.checkNotNull(identifier);
38         this.replyTo = Preconditions.checkNotNull(replyTo);
39     }
40
41     @Override
42     public TransactionIdentifier getIdentifier() {
43         return identifier;
44     }
45
46     private void checkFinished() {
47         Preconditions.checkState(protocol != null, "Batch has already been finished");
48     }
49
50     public void setSequence(final long sequence) {
51         checkFinished();
52         Preconditions.checkState(modifications.isEmpty(), "Sequence must be set first");
53         this.sequence = sequence;
54     }
55
56     public void addModification(final TransactionModification modification) {
57         checkFinished();
58         modifications.add(Preconditions.checkNotNull(modification));
59     }
60
61     public void setAbort() {
62         checkFinished();
63         // Transaction is being aborted, no need to transmit operations
64         modifications.clear();
65         protocol = PersistenceProtocol.ABORT;
66     }
67
68     public void setCommit(final boolean coordinated) {
69         checkFinished();
70         protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
71     }
72
73     public int size() {
74         return modifications.size();
75     }
76
77     @Override
78     public ModifyTransactionRequest build() {
79         final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, 0, replyTo,
80             modifications, protocol);
81         modifications.clear();
82         protocol = null;
83         sequence = 0;
84         return ret;
85     }
86
87 }