2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.concepts;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import java.io.Serial;
17 import java.io.Serializable;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.access.ABIVersion;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.concepts.WritableIdentifier;
24 * An abstract concept of a Message. This class cannot be instantiated directly, use its specializations {@link Request}
25 * and {@link Response}.
28 * Messages have a target and a sequence number. Sequence numbers are expected to be assigned monotonically on a
29 * per-target basis, hence two targets can observe the same sequence number.
32 * This class includes explicit versioning for forward- and backward- compatibility of serialization format. This is
33 * achieved by using the serialization proxy pattern. Subclasses are in complete control of what proxy is used to
34 * serialize a particular object on the wire. This class can serve as an explicit version marker, hence no further
35 * action is necessary in the deserialization path.
38 * For the serialization path an explicit call from the user is required to select the appropriate serialization
39 * version. This is done via {@link #toVersion(ABIVersion)} method, which should return a copy of this object with
40 * the requested ABI version recorded and should return the appropriate serialization proxy.
43 * This workflow allows least disturbance across ABI versions, as all messages not affected by a ABI version bump
44 * will remain working with the same serialization format for the new ABI version.
47 * Note that this class specifies the {@link Immutable} contract, which means that all subclasses must follow this API
50 * @param <T> Target identifier type
51 * @param <C> Message type
53 public abstract class Message<T extends WritableIdentifier, C extends Message<T, C>> implements Immutable,
56 private static final long serialVersionUID = 1L;
58 private final @NonNull ABIVersion version;
59 private final long sequence;
60 private final @NonNull T target;
62 private Message(final ABIVersion version, final T target, final long sequence) {
63 this.target = requireNonNull(target);
64 this.version = requireNonNull(version);
65 this.sequence = sequence;
68 Message(final T target, final long sequence) {
69 this(ABIVersion.current(), target, sequence);
72 Message(final C msg, final ABIVersion version) {
73 this(version, msg.getTarget(), msg.getSequence());
77 * Get the target identifier for this message.
79 * @return Target identifier
81 public final @NonNull T getTarget() {
86 * Get the logical sequence number.
88 * @return logical sequence number
90 public final long getSequence() {
95 public final @NonNull ABIVersion getVersion() {
100 * Return a message which will end up being serialized in the specified {@link ABIVersion}.
102 * @param toVersion Request {@link ABIVersion}
103 * @return A new message which will use ABIVersion as its serialization.
105 @SuppressWarnings("unchecked")
106 public final @NonNull C toVersion(final @NonNull ABIVersion toVersion) {
107 if (this.version == toVersion) {
111 return switch (toVersion) {
112 case BORON, NEON_SR2, SODIUM_SR1, MAGNESIUM -> verifyNotNull(cloneAsVersion(toVersion));
113 case TEST_PAST_VERSION, TEST_FUTURE_VERSION ->
114 throw new IllegalArgumentException("Unhandled ABI version " + toVersion);
115 default -> throw new IllegalArgumentException("Unhandled ABI version " + toVersion);
120 * Create a copy of this message which will serialize to a stream corresponding to the specified method. This
121 * method should be implemented by the concrete final message class and should invoke the equivalent of
122 * {@link #Message(Message, ABIVersion)}.
124 * @param targetVersion target ABI version
125 * @return A message with the specified serialization stream
126 * @throws IllegalArgumentException if this message does not support the target ABI
128 protected abstract @NonNull C cloneAsVersion(@NonNull ABIVersion targetVersion);
131 public final String toString() {
132 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
136 * Add attributes to the output of {@link #toString()}. Subclasses wanting to contribute additional information
137 * should override this method. Any null attributes will be omitted from the output.
139 * @param toStringHelper a {@link ToStringHelper} instance
140 * @return The {@link ToStringHelper} passed in as argument
141 * @throws NullPointerException if toStringHelper is null
143 protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
144 return toStringHelper.add("target", target).add("sequence", Long.toUnsignedString(sequence));
148 * Instantiate a serialization proxy for this object for the target ABI version. Implementations should return
149 * different objects for incompatible {@link ABIVersion}s. This method should never fail, as any compatibility
150 * checks should have been done by {@link #cloneAsVersion(ABIVersion)}.
152 * @param reqVersion Requested ABI version
153 * @return Proxy for this object
155 abstract @NonNull AbstractMessageProxy<T, C> externalizableProxy(@NonNull ABIVersion reqVersion);
157 protected final Object writeReplace() {
158 return externalizableProxy(version);