Add ABIVersion.CHLORINE_SR2
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / Message.java
index 8788c0df989e248d1eddad1be62b16a46237644a..54cf40a8b00b625c64fd31ee4d0e616a7157baad 100644 (file)
@@ -7,17 +7,23 @@
  */
 package org.opendaylight.controller.cluster.access.concepts;
 
-import com.google.common.annotations.Beta;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
+import java.io.DataInput;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.io.Serializable;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.cluster.access.ABIVersion;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.WritableIdentifier;
+import org.opendaylight.yangtools.concepts.WritableObjects;
 
 /**
  * An abstract concept of a Message. This class cannot be instantiated directly, use its specializations {@link Request}
@@ -46,23 +52,58 @@ import org.opendaylight.yangtools.concepts.WritableIdentifier;
  * Note that this class specifies the {@link Immutable} contract, which means that all subclasses must follow this API
  * contract.
  *
- * @author Robert Varga
- *
  * @param <T> Target identifier type
  * @param <C> Message type
  */
-@Beta
-public abstract class Message<T extends WritableIdentifier, C extends Message<T, C>> implements Immutable,
-        Serializable {
+public abstract class Message<T extends WritableIdentifier, C extends Message<T, C>>
+        implements Immutable, Serializable {
+    /**
+     * Externalizable proxy for use with {@link Message} subclasses.
+     *
+     * @param <T> Target identifier type
+     * @param <C> Message class
+     */
+    protected interface SerialForm<T extends WritableIdentifier, C extends Message<T, C>> extends Externalizable {
+
+        @NonNull C message();
+
+        void setMessage(@NonNull C message);
+
+        @Override
+        default void writeExternal(final ObjectOutput out) throws IOException {
+            final var message = message();
+            message.getTarget().writeTo(out);
+            WritableObjects.writeLong(out, message.getSequence());
+            writeExternal(out, message);
+        }
+
+        void writeExternal(@NonNull ObjectOutput out, @NonNull C msg) throws IOException;
+
+        @Override
+        default void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+            final var target = verifyNotNull(readTarget(in));
+            final var sequence = WritableObjects.readLong(in);
+            setMessage(verifyNotNull(readExternal(in, target, sequence)));
+        }
+
+        @NonNull C readExternal(@NonNull ObjectInput in, @NonNull T target, long sequence)
+            throws IOException, ClassNotFoundException;
+
+        Object readResolve();
+
+        @NonNull T readTarget(@NonNull DataInput in) throws IOException;
+    }
+
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
 
-    private final ABIVersion version;
+    private final @NonNull ABIVersion version;
     private final long sequence;
-    private final T target;
+    private final @NonNull T target;
 
     private Message(final ABIVersion version, final T target, final long sequence) {
-        this.target = Preconditions.checkNotNull(target);
-        this.version = Preconditions.checkNotNull(version);
+        this.target = requireNonNull(target);
+        this.version = requireNonNull(version);
         this.sequence = sequence;
     }
 
@@ -79,8 +120,7 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
      *
      * @return Target identifier
      */
-    @Nonnull
-    public final T getTarget() {
+    public final @NonNull T getTarget() {
         return target;
     }
 
@@ -94,8 +134,7 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
     }
 
     @VisibleForTesting
-    @Nonnull
-    public final ABIVersion getVersion() {
+    public final @NonNull ABIVersion getVersion() {
         return version;
     }
 
@@ -106,23 +145,15 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
      * @return A new message which will use ABIVersion as its serialization.
      */
     @SuppressWarnings("unchecked")
-    @Nonnull
-    public final C toVersion(@Nonnull final ABIVersion toVersion) {
+    public final @NonNull C toVersion(final @NonNull ABIVersion toVersion) {
         if (this.version == toVersion) {
             return (C)this;
         }
 
-        switch (toVersion) {
-            case BORON:
-                return Verify.verifyNotNull(cloneAsVersion(toVersion));
-            case TEST_PAST_VERSION:
-            case TEST_FUTURE_VERSION:
-            default:
-                // Fall-through to throw
-                break;
-        }
-
-        throw new IllegalArgumentException("Unhandled ABI version " + toVersion);
+        return switch (toVersion) {
+            case BORON, NEON_SR2, SODIUM_SR1, MAGNESIUM, CHLORINE_SR2 -> verifyNotNull(cloneAsVersion(toVersion));
+            default -> throw new IllegalArgumentException("Unhandled ABI version " + toVersion);
+        };
     }
 
     /**
@@ -134,8 +165,7 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
      * @return A message with the specified serialization stream
      * @throws IllegalArgumentException if this message does not support the target ABI
      */
-    @Nonnull
-    protected abstract C cloneAsVersion(@Nonnull ABIVersion targetVersion);
+    protected abstract @NonNull C cloneAsVersion(@NonNull ABIVersion targetVersion);
 
     @Override
     public final String toString() {
@@ -150,8 +180,7 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
      * @return The {@link ToStringHelper} passed in as argument
      * @throws NullPointerException if toStringHelper is null
      */
-    @Nonnull
-    protected ToStringHelper addToStringAttributes(@Nonnull final ToStringHelper toStringHelper) {
+    protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
         return toStringHelper.add("target", target).add("sequence", Long.toUnsignedString(sequence));
     }
 
@@ -163,9 +192,9 @@ public abstract class Message<T extends WritableIdentifier, C extends Message<T,
      * @param reqVersion Requested ABI version
      * @return Proxy for this object
      */
-    @Nonnull
-    abstract AbstractMessageProxy<T, C> externalizableProxy(@Nonnull ABIVersion reqVersion);
+    protected abstract @NonNull SerialForm<T, C> externalizableProxy(@NonNull ABIVersion reqVersion);
 
+    @java.io.Serial
     protected final Object writeReplace() {
         return externalizableProxy(version);
     }