2 * Copyright (c) 2017 Brocade Communications 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 java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.raft.persisted.MigratedSerializable;
24 * Represents the persisted snapshot state for the ShardManager.
26 * @author Thomas Pantelis
28 public sealed class ShardManagerSnapshot implements Serializable {
29 interface SerializedForm extends Externalizable {
31 * Return the serial form of this object contents, corresponding to {@link ShardManagerSnapshot#shardList}.
33 * @return List of shards names.
35 List<String> shardNames();
38 * Resolve this proxy to an actual {@link ShardManagerSnapshot}. Implementations can rely on the object to be
39 * set via {@link #resolveTo(ShardManagerSnapshot)}.
46 * Set this proxy to return {@code snapshot} on next {@link #readResolve()}.
48 * @param newSnapshot Snapshot to set
50 void resolveTo(@NonNull ShardManagerSnapshot newSnapshot);
53 default void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
54 final int size = in.readInt();
55 final var shardList = new ArrayList<String>(size);
56 for (int i = 0; i < size; i++) {
57 shardList.add((String) in.readObject());
59 resolveTo(new ShardManagerSnapshot(shardList));
63 default void writeExternal(final ObjectOutput out) throws IOException {
64 final var shardList = shardNames();
65 out.writeInt(shardList.size());
66 for (var shardName : shardList) {
67 out.writeObject(shardName);
72 @Deprecated(since = "7.0.0", forRemoval = true)
73 private static final class Magnesium extends ShardManagerSnapshot implements MigratedSerializable {
75 private static final long serialVersionUID = 1L;
77 Magnesium(final List<String> shardList) {
82 public boolean isMigrated() {
87 @Deprecated(since = "7.0.0", forRemoval = true)
88 private static final class Proxy implements SerializedForm {
90 private static final long serialVersionUID = 1L;
92 private ShardManagerSnapshot snapshot = null;
94 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
95 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
96 @SuppressWarnings("checkstyle:RedundantModifier")
102 public List<String> shardNames() {
103 return snapshot.getShardList();
107 public void resolveTo(final ShardManagerSnapshot newSnapshot) {
108 snapshot = requireNonNull(newSnapshot);
112 public Object readResolve() {
113 return new Magnesium(snapshot.getShardList());
118 private static final long serialVersionUID = 1L;
120 private final List<String> shardList;
122 public ShardManagerSnapshot(final @NonNull List<String> shardList) {
123 this.shardList = ImmutableList.copyOf(shardList);
126 public final List<String> getShardList() {
131 public final Object writeReplace() {
136 public final String toString() {
137 return "ShardManagerSnapshot [ShardList = " + shardList + " ]";