2e0320bbcd7785d3cdb5546d3098280cce9180f0
[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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableList;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNull;
22
23 /**
24  * Represents the persisted snapshot state for the ShardManager.
25  *
26  * @author Thomas Pantelis
27  */
28 public final 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     private static final class Proxy implements SerializedForm {
73         private static final long serialVersionUID = 1L;
74
75         private ShardManagerSnapshot snapshot;
76
77         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
78         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
79         @SuppressWarnings("checkstyle:RedundantModifier")
80         public Proxy() {
81             // For Externalizable
82         }
83
84         Proxy(final ShardManagerSnapshot snapshot) {
85             this.snapshot = snapshot;
86         }
87
88         @Override
89         public List<String> shardNames() {
90             return snapshot.getShardList();
91         }
92
93         @Override
94         public void resolveTo(final ShardManagerSnapshot newSnapshot) {
95             snapshot = requireNonNull(newSnapshot);
96         }
97
98         @Override
99         public Object readResolve() {
100             return verifyNotNull(snapshot);
101         }
102     }
103
104     private static final long serialVersionUID = 1L;
105
106     private final List<String> shardList;
107
108     public ShardManagerSnapshot(final @NonNull List<String> shardList) {
109         this.shardList = ImmutableList.copyOf(shardList);
110     }
111
112     public List<String> getShardList() {
113         return shardList;
114     }
115
116     private Object writeReplace() {
117         return new Proxy(this);
118     }
119
120     @Override
121     public String toString() {
122         return "ShardManagerSnapshot [ShardList = " + shardList + " ]";
123     }
124 }