2 * Copyright (c) 2015 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.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
25 * Stores a snapshot of the internal state of a data store.
27 * @author Thomas Pantelis
29 public class DatastoreSnapshot implements Serializable {
30 private static final long serialVersionUID = 1L;
32 private static final class Proxy implements Externalizable {
33 private static final long serialVersionUID = 1L;
35 private DatastoreSnapshot datastoreSnapshot;
37 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
38 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
39 @SuppressWarnings("checkstyle:RedundantModifier")
44 Proxy(final DatastoreSnapshot datastoreSnapshot) {
45 this.datastoreSnapshot = datastoreSnapshot;
49 public void writeExternal(ObjectOutput out) throws IOException {
50 out.writeObject(datastoreSnapshot.type);
51 out.writeObject(datastoreSnapshot.shardManagerSnapshot);
53 out.writeInt(datastoreSnapshot.shardSnapshots.size());
54 for (ShardSnapshot shardSnapshot: datastoreSnapshot.shardSnapshots) {
55 out.writeObject(shardSnapshot);
60 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
61 String localType = (String)in.readObject();
62 ShardManagerSnapshot localShardManagerSnapshot = (ShardManagerSnapshot) in.readObject();
64 int size = in.readInt();
65 List<ShardSnapshot> localShardSnapshots = new ArrayList<>(size);
66 for (int i = 0; i < size; i++) {
67 localShardSnapshots.add((ShardSnapshot) in.readObject());
70 datastoreSnapshot = new DatastoreSnapshot(localType, localShardManagerSnapshot, localShardSnapshots);
73 private Object readResolve() {
74 return datastoreSnapshot;
78 private final String type;
79 private final ShardManagerSnapshot shardManagerSnapshot;
80 private final List<ShardSnapshot> shardSnapshots;
82 public DatastoreSnapshot(@NonNull String type, @Nullable ShardManagerSnapshot shardManagerSnapshot,
83 @NonNull List<ShardSnapshot> shardSnapshots) {
84 this.type = requireNonNull(type);
85 this.shardManagerSnapshot = shardManagerSnapshot;
86 this.shardSnapshots = ImmutableList.copyOf(shardSnapshots);
89 public @NonNull String getType() {
93 public @Nullable ShardManagerSnapshot getShardManagerSnapshot() {
94 return shardManagerSnapshot;
97 public @NonNull List<ShardSnapshot> getShardSnapshots() {
98 return shardSnapshots;
101 private Object writeReplace() {
102 return new Proxy(this);
105 public static class ShardSnapshot implements Serializable {
106 private static final long serialVersionUID = 1L;
108 private static final class Proxy implements Externalizable {
109 private static final long serialVersionUID = 1L;
111 private ShardSnapshot shardSnapshot;
113 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
114 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
115 @SuppressWarnings("checkstyle:RedundantModifier")
117 // For Externalizable
120 Proxy(final ShardSnapshot shardSnapshot) {
121 this.shardSnapshot = shardSnapshot;
125 public void writeExternal(ObjectOutput out) throws IOException {
126 out.writeObject(shardSnapshot.name);
127 out.writeObject(shardSnapshot.snapshot);
131 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
132 shardSnapshot = new ShardSnapshot((String)in.readObject(), (Snapshot) in.readObject());
135 private Object readResolve() {
136 return shardSnapshot;
140 private final String name;
141 private final Snapshot snapshot;
143 public ShardSnapshot(@NonNull String name, @NonNull Snapshot snapshot) {
144 this.name = requireNonNull(name);
145 this.snapshot = requireNonNull(snapshot);
148 public @NonNull String getName() {
152 public @NonNull Snapshot getSnapshot() {
156 private Object writeReplace() {
157 return new Proxy(this);