Bump odlparent to 5.0.0
[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 org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
16 import org.opendaylight.yangtools.concepts.Builder;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18
19 /**
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
22  * thread-safe.
23  *
24  * @author Robert Varga
25  */
26 @Beta
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;
32
33     private PersistenceProtocol protocol;
34     private boolean haveSequence;
35     private long sequence;
36
37     public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
38         this.identifier = Preconditions.checkNotNull(identifier);
39         this.replyTo = Preconditions.checkNotNull(replyTo);
40     }
41
42     @Override
43     public TransactionIdentifier getIdentifier() {
44         return identifier;
45     }
46
47     private void checkNotFinished() {
48         Preconditions.checkState(protocol == null, "Batch has already been finished");
49     }
50
51     public void addModification(final TransactionModification modification) {
52         checkNotFinished();
53         modifications.add(Preconditions.checkNotNull(modification));
54     }
55
56     public void setSequence(final long sequence) {
57         Preconditions.checkState(!haveSequence, "Sequence has already been set");
58         this.sequence = sequence;
59         haveSequence = true;
60     }
61
62     public void setAbort() {
63         checkNotFinished();
64         // Transaction is being aborted, no need to transmit operations
65         modifications.clear();
66         protocol = PersistenceProtocol.ABORT;
67     }
68
69     public void setCommit(final boolean coordinated) {
70         checkNotFinished();
71         protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
72     }
73
74     public void setReady() {
75         checkNotFinished();
76         protocol = PersistenceProtocol.READY;
77     }
78
79     public int size() {
80         return modifications.size();
81     }
82
83     @Override
84     public ModifyTransactionRequest build() {
85         Preconditions.checkState(haveSequence, "Request sequence has not been set");
86
87         final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
88             protocol);
89         modifications.clear();
90         protocol = null;
91         haveSequence = false;
92         return ret;
93     }
94 }