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.datastore.persisted;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.function.Function;
19 import org.apache.commons.lang3.SerializationUtils;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.raft.messages.IdentifiablePayload;
22 import org.opendaylight.yangtools.concepts.Identifier;
25 * Abstract base class for {@link IdentifiablePayload}s which hold a single {@link Identifier}.
27 public abstract class AbstractIdentifiablePayload<T extends Identifier> extends IdentifiablePayload<T> {
29 * An {@link Externalizable} with default implementations we expect our implementations to comply with. On-wire
30 * serialization format is defined by {@link #bytes()}.
32 protected interface SerialForm extends Externalizable {
34 * Return the serial form of this object contents, corresponding to
35 * {@link AbstractIdentifiablePayload#serialized}.
37 * @return Serialized form
42 * Resolve this proxy to an actual {@link AbstractIdentifiablePayload}.
50 * Restore state from specified serialized form.
52 * @param newBytes Serialized form, as returned by {@link #bytes()}
53 * @throws IOException when a deserialization problem occurs
55 void readExternal(byte[] newBytes) throws IOException;
61 * The default implementation is canonical and should never be overridden.
64 default void readExternal(final ObjectInput in) throws IOException {
65 final var bytes = new byte[in.readInt()];
74 * The default implementation is canonical and should never be overridden.
77 default void writeExternal(final ObjectOutput out) throws IOException {
78 final var bytes = bytes();
79 out.writeInt(bytes.length);
85 private static final long serialVersionUID = 1L;
87 private final byte @NonNull [] serialized;
88 private final @NonNull T identifier;
90 AbstractIdentifiablePayload(final @NonNull T identifier, final byte @NonNull[] serialized) {
91 this.identifier = requireNonNull(identifier);
92 this.serialized = requireNonNull(serialized);
96 public final T getIdentifier() {
101 public final int size() {
102 return serialized.length;
105 protected final byte @NonNull [] serialized() {
110 public final int serializedSize() {
111 // TODO: this is not entirely accurate, as the serialization stream has additional overheads:
112 // - 3 bytes for each block of data <256 bytes
113 // - 5 bytes for each block of data >=256 bytes
114 // - each block of data is limited to 1024 bytes as per serialization spec
115 return size() + externalizableProxySize();
119 public final String toString() {
120 return MoreObjects.toStringHelper(this).add("identifier", identifier).add("size", size()).toString();
124 public final Object writeReplace() {
125 return verifyNotNull(externalizableProxy(serialized));
128 protected abstract @NonNull SerialForm externalizableProxy(byte @NonNull[] serialized);
130 protected abstract int externalizableProxySize();
132 protected static final int externalizableProxySize(final Function<byte[], ? extends SerialForm> constructor) {
133 return SerializationUtils.serialize(constructor.apply(new byte[0])).length;