Final bits of NodeIdentifier migration
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequestBuilder.java
index 336902e192fe0ce0eebf687d1fc202b20181fbed..8a04936e195a962b5af174c47e934f0541374678 100644 (file)
@@ -12,25 +12,27 @@ import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.List;
-import javax.annotation.concurrent.NotThreadSafe;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Identifiable;
 
 /**
  * A reusable {@link Builder} for creating {@link ModifyTransactionRequest} message instances. Its internal state is
- * reset when {@link #build()} is invoked, hence it can be used to create a sequence of messages.
+ * reset when {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT
+ * thread-safe.
  *
  * @author Robert Varga
  */
 @Beta
-@NotThreadSafe
 public final class ModifyTransactionRequestBuilder implements Builder<ModifyTransactionRequest>,
         Identifiable<TransactionIdentifier> {
     private final List<TransactionModification> modifications = new ArrayList<>(1);
     private final TransactionIdentifier identifier;
     private final ActorRef replyTo;
-    private PersistenceProtocol protocol = null;
+
+    private PersistenceProtocol protocol;
+    private boolean haveSequence;
+    private long sequence;
 
     public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
         this.identifier = Preconditions.checkNotNull(identifier);
@@ -42,36 +44,51 @@ public final class ModifyTransactionRequestBuilder implements Builder<ModifyTran
         return identifier;
     }
 
-    private void checkFinished() {
-        Preconditions.checkState(protocol != null, "Batch has already been finished");
+    private void checkNotFinished() {
+        Preconditions.checkState(protocol == null, "Batch has already been finished");
     }
 
     public void addModification(final TransactionModification modification) {
-        checkFinished();
+        checkNotFinished();
         modifications.add(Preconditions.checkNotNull(modification));
     }
 
+    public void setSequence(final long sequence) {
+        Preconditions.checkState(!haveSequence, "Sequence has already been set");
+        this.sequence = sequence;
+        haveSequence = true;
+    }
+
     public void setAbort() {
-        checkFinished();
+        checkNotFinished();
         // Transaction is being aborted, no need to transmit operations
         modifications.clear();
         protocol = PersistenceProtocol.ABORT;
     }
 
     public void setCommit(final boolean coordinated) {
-        checkFinished();
+        checkNotFinished();
         protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
     }
 
+    public void setReady() {
+        checkNotFinished();
+        protocol = PersistenceProtocol.READY;
+    }
+
     public int size() {
         return modifications.size();
     }
 
     @Override
     public ModifyTransactionRequest build() {
-        final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, replyTo, modifications, protocol);
+        Preconditions.checkState(haveSequence, "Request sequence has not been set");
+
+        final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
+            protocol);
         modifications.clear();
         protocol = null;
+        haveSequence = false;
         return ret;
     }
 }