Add new cds-access-api proxies
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.java
index f8f890e2c1df0e75969b2c85fb82b8fd2d93f2aa..0e70edc76fb10d7c63e34f032722c4b5afbb2dfb 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.access.concepts;
 
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
@@ -16,6 +17,7 @@ import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.WritableIdentifier;
 import org.opendaylight.yangtools.concepts.WritableObjects;
 
@@ -24,24 +26,49 @@ import org.opendaylight.yangtools.concepts.WritableObjects;
  * - a {@link ClientIdentifier}, which uniquely identifies a single instantiation of a particular frontend
  * - an unsigned long, which uniquely identifies the history on the backend
  * - an unsigned long cookie, assigned by the client and meaningless on the backend, which just reflects it back
- *
- * @author Robert Varga
  */
 public final class LocalHistoryIdentifier implements WritableIdentifier {
-    /*
-     * Implementation note: cookie is currently required only for module-based sharding, which is implemented as part
-     *                      of normal DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence
-     *                      we may end up not needing cookie at all.
+    /**
+     * Serialized form of {@link LocalHistoryIdentifier}.
      *
-     *                      We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we
-     *                      end up not needing the cookie at all, we can switch to writeLong() and use zero flags for
-     *                      compatibility.
+     * @implNote
+     *     cookie is currently required only for module-based sharding, which is implemented as part of normal
+     *     DataBroker interfaces. For DOMDataTreeProducer cookie will always be zero, hence we may end up not needing
+     *     cookie at all.
+     *     We use WritableObjects.writeLongs() to output historyId and cookie (in that order). If we end up not needing
+     *     the cookie at all, we can switch to writeLong() and use zero flags for compatibility.
      */
-    private static final class Proxy implements Externalizable {
+    interface SerialForm extends Externalizable {
+        @NonNull LocalHistoryIdentifier identifier();
+
+        void setIdentifier(@NonNull LocalHistoryIdentifier identifier);
+
+        @java.io.Serial
+        Object readResolve();
+
+        @Override
+        default void writeExternal(final ObjectOutput out) throws IOException {
+            final var id = identifier();
+            id.getClientId().writeTo(out);
+            WritableObjects.writeLongs(out, id.getHistoryId(), id.getCookie());
+        }
+
+        @Override
+        default void readExternal(final ObjectInput in) throws IOException {
+            final var clientId = ClientIdentifier.readFrom(in);
+
+            final byte header = WritableObjects.readLongHeader(in);
+            final var historyId = WritableObjects.readFirstLong(in, header);
+            final var cookie = WritableObjects.readSecondLong(in, header);
+            setIdentifier(new LocalHistoryIdentifier(clientId, historyId, cookie));
+        }
+    }
+
+    private static final class Proxy implements SerialForm {
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
-        private ClientIdentifier clientId;
-        private long historyId;
-        private long cookie;
+
+        private LocalHistoryIdentifier identifier;
 
         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
         // be able to create instances via reflection.
@@ -50,34 +77,30 @@ public final class LocalHistoryIdentifier implements WritableIdentifier {
             // For Externalizable
         }
 
-        Proxy(final ClientIdentifier frontendId, final long historyId, final long cookie) {
-            this.clientId = requireNonNull(frontendId);
-            this.historyId = historyId;
-            this.cookie = cookie;
+        Proxy(final LocalHistoryIdentifier identifier) {
+            this.identifier = requireNonNull(identifier);
         }
 
         @Override
-        public void writeExternal(final ObjectOutput out) throws IOException {
-            clientId.writeTo(out);
-            WritableObjects.writeLongs(out, historyId, cookie);
+        public LocalHistoryIdentifier identifier() {
+            return verifyNotNull(identifier);
         }
 
         @Override
-        public void readExternal(final ObjectInput in) throws IOException {
-            clientId = ClientIdentifier.readFrom(in);
-
-            final byte header = WritableObjects.readLongHeader(in);
-            historyId = WritableObjects.readFirstLong(in, header);
-            cookie = WritableObjects.readSecondLong(in, header);
+        public void setIdentifier(final LocalHistoryIdentifier identifier) {
+            this.identifier = requireNonNull(identifier);
         }
 
-        private Object readResolve() {
-            return new LocalHistoryIdentifier(clientId, historyId, cookie);
+        @Override
+        public Object readResolve() {
+            return identifier();
         }
     }
 
+    @java.io.Serial
     private static final long serialVersionUID = 1L;
-    private final ClientIdentifier clientId;
+
+    private final @NonNull ClientIdentifier clientId;
     private final long historyId;
     private final long cookie;
 
@@ -86,12 +109,12 @@ public final class LocalHistoryIdentifier implements WritableIdentifier {
     }
 
     public LocalHistoryIdentifier(final ClientIdentifier frontendId, final long historyId, final long cookie) {
-        this.clientId = requireNonNull(frontendId);
+        clientId = requireNonNull(frontendId);
         this.historyId = historyId;
         this.cookie = cookie;
     }
 
-    public static LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
+    public static @NonNull LocalHistoryIdentifier readFrom(final DataInput in) throws IOException {
         final ClientIdentifier clientId = ClientIdentifier.readFrom(in);
 
         final byte header = WritableObjects.readLongHeader(in);
@@ -105,7 +128,7 @@ public final class LocalHistoryIdentifier implements WritableIdentifier {
         WritableObjects.writeLongs(out, historyId, cookie);
     }
 
-    public ClientIdentifier getClientId() {
+    public @NonNull ClientIdentifier getClientId() {
         return clientId;
     }
 
@@ -130,11 +153,10 @@ public final class LocalHistoryIdentifier implements WritableIdentifier {
         if (this == obj) {
             return true;
         }
-        if (!(obj instanceof LocalHistoryIdentifier)) {
+        if (!(obj instanceof LocalHistoryIdentifier other)) {
             return false;
         }
 
-        final LocalHistoryIdentifier other = (LocalHistoryIdentifier) obj;
         return historyId == other.historyId && cookie == other.cookie && clientId.equals(other.clientId);
     }
 
@@ -145,7 +167,8 @@ public final class LocalHistoryIdentifier implements WritableIdentifier {
                 .add("cookie", Long.toUnsignedString(cookie, 16)).toString();
     }
 
+    @java.io.Serial
     private Object writeReplace() {
-        return new Proxy(clientId, historyId, cookie);
+        return new Proxy(this);
     }
 }