Switch to use PayloadVersion.CHLORINE_SR2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardManagerSnapshot.java
1 /*
2  * Copyright (c) 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
24  * Represents the persisted snapshot state for the ShardManager.
25  *
26  * @author Thomas Pantelis
27  */
28 public sealed class ShardManagerSnapshot implements Serializable {
29     interface SerializedForm extends Externalizable {
30         /**
31          * Return the serial form of this object contents, corresponding to {@link ShardManagerSnapshot#shardList}.
32          *
33          * @return List of shards names.
34          */
35         List<String> shardNames();
36
37         /**
38          * Resolve this proxy to an actual {@link ShardManagerSnapshot}. Implementations can rely on the object to be
39          * set via {@link #resolveTo(ShardManagerSnapshot)}.
40          *
41          * @return A snapshot
42          */
43         Object readResolve();
44
45         /**
46          * Set this proxy to return {@code snapshot} on next {@link #readResolve()}.
47          *
48          * @param newSnapshot Snapshot to set
49          */
50         void resolveTo(@NonNull ShardManagerSnapshot newSnapshot);
51
52         @Override
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());
58             }
59             resolveTo(new ShardManagerSnapshot(shardList));
60         }
61
62         @Override
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);
68             }
69         }
70     }
71
72     @Deprecated(since = "7.0.0", forRemoval = true)
73     private static final class Magnesium extends ShardManagerSnapshot implements MigratedSerializable {
74         @java.io.Serial
75         private static final long serialVersionUID = 1L;
76
77         Magnesium(final List<String> shardList) {
78             super(shardList);
79         }
80
81         @Override
82         public boolean isMigrated() {
83             return true;
84         }
85     }
86
87     @Deprecated(since = "7.0.0", forRemoval = true)
88     private static final class Proxy implements SerializedForm {
89         @java.io.Serial
90         private static final long serialVersionUID = 1L;
91
92         private ShardManagerSnapshot snapshot = null;
93
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")
97         public Proxy() {
98             // For Externalizable
99         }
100
101         @Override
102         public List<String> shardNames() {
103             return snapshot.getShardList();
104         }
105
106         @Override
107         public void resolveTo(final ShardManagerSnapshot newSnapshot) {
108             snapshot = requireNonNull(newSnapshot);
109         }
110
111         @Override
112         public Object readResolve() {
113             return new Magnesium(snapshot.getShardList());
114         }
115     }
116
117     @java.io.Serial
118     private static final long serialVersionUID = 1L;
119
120     private final List<String> shardList;
121
122     public ShardManagerSnapshot(final @NonNull List<String> shardList) {
123         this.shardList = ImmutableList.copyOf(shardList);
124     }
125
126     public final List<String> getShardList() {
127         return shardList;
128     }
129
130     @java.io.Serial
131     public final Object writeReplace() {
132         return new SM(this);
133     }
134
135     @Override
136     public final String toString() {
137         return "ShardManagerSnapshot [ShardList = " + shardList + " ]";
138     }
139 }