BUG-5280: introduce Request/Response and related concepts
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / AbstractMessageProxy.java
diff --git a/opendaylight/md-sal/cds-access-api/src/main/java/org/opendaylight/controller/cluster/access/concepts/AbstractMessageProxy.java b/opendaylight/md-sal/cds-access-api/src/main/java/org/opendaylight/controller/cluster/access/concepts/AbstractMessageProxy.java
new file mode 100644 (file)
index 0000000..04e7037
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.access.concepts;
+
+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 javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.WritableIdentifier;
+import org.opendaylight.yangtools.concepts.WritableObjects;
+
+/**
+ * Abstract Externalizable proxy for use with {@link Message} subclasses.
+ *
+ * @author Robert Varga
+ *
+ * @param <T> Target identifier type
+ * @param <C> Message class
+ */
+abstract class AbstractMessageProxy<T extends WritableIdentifier, C extends Message<T, C>> implements Externalizable {
+    private static final long serialVersionUID = 1L;
+    private T target;
+    private long sequence;
+    private long retry;
+
+    protected AbstractMessageProxy() {
+        // For Externalizable
+    }
+
+    AbstractMessageProxy(final @Nonnull C message) {
+        this.target = message.getTarget();
+        this.sequence = message.getSequence();
+        this.retry = message.getRetry();
+    }
+
+    @Override
+    public void writeExternal(final ObjectOutput out) throws IOException {
+        target.writeTo(out);
+        WritableObjects.writeLongs(out, sequence, retry);
+    }
+
+    @Override
+    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+        target = Verify.verifyNotNull(readTarget(in));
+
+        final byte header = WritableObjects.readLongHeader(in);
+        sequence = WritableObjects.readFirstLong(in, header);
+        retry = WritableObjects.readSecondLong(in, header);
+    }
+
+    protected final Object readResolve() {
+        return Verify.verifyNotNull(createMessage(target, sequence, retry));
+    }
+
+    protected abstract @Nonnull T readTarget(@Nonnull DataInput in) throws IOException;
+    abstract @Nonnull C createMessage(@Nonnull T target, long sequence, long retry);
+}
\ No newline at end of file