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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
24 * Stores a snapshot of the internal state of a data store.
26 * @author Thomas Pantelis
28 public class DatastoreSnapshot implements Serializable {
29 private static final long serialVersionUID = 1L;
31 private static final class Proxy implements Externalizable {
32 private static final long serialVersionUID = 1L;
34 private DatastoreSnapshot datastoreSnapshot;
36 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
37 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
38 @SuppressWarnings("checkstyle:RedundantModifier")
43 Proxy(final DatastoreSnapshot datastoreSnapshot) {
44 this.datastoreSnapshot = datastoreSnapshot;
48 public void writeExternal(ObjectOutput out) throws IOException {
49 out.writeObject(datastoreSnapshot.type);
50 out.writeObject(datastoreSnapshot.shardManagerSnapshot);
52 out.writeInt(datastoreSnapshot.shardSnapshots.size());
53 for (ShardSnapshot shardSnapshot: datastoreSnapshot.shardSnapshots) {
54 out.writeObject(shardSnapshot);
59 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
60 String type = (String)in.readObject();
61 ShardManagerSnapshot shardManagerSnapshot = (ShardManagerSnapshot) in.readObject();
63 int size = in.readInt();
64 List<ShardSnapshot> shardSnapshots = new ArrayList<>(size);
65 for (int i = 0; i < size; i++) {
66 shardSnapshots.add((ShardSnapshot) in.readObject());
69 datastoreSnapshot = new DatastoreSnapshot(type, shardManagerSnapshot, shardSnapshots);
72 private Object readResolve() {
73 return datastoreSnapshot;
77 private final String type;
78 private final ShardManagerSnapshot shardManagerSnapshot;
79 private final List<ShardSnapshot> shardSnapshots;
81 public DatastoreSnapshot(@Nonnull String type, @Nullable ShardManagerSnapshot shardManagerSnapshot,
82 @Nonnull List<ShardSnapshot> shardSnapshots) {
83 this.type = Preconditions.checkNotNull(type);
84 this.shardManagerSnapshot = shardManagerSnapshot;
85 this.shardSnapshots = ImmutableList.copyOf(Preconditions.checkNotNull(shardSnapshots));
89 public String getType() {
94 public ShardManagerSnapshot getShardManagerSnapshot() {
95 return shardManagerSnapshot;
99 public List<ShardSnapshot> getShardSnapshots() {
100 return shardSnapshots;
103 private Object writeReplace() {
104 return new Proxy(this);
107 public static class ShardSnapshot implements Serializable {
108 private static final long serialVersionUID = 1L;
110 private static final class Proxy implements Externalizable {
111 private static final long serialVersionUID = 1L;
113 private ShardSnapshot shardSnapshot;
115 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
116 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
117 @SuppressWarnings("checkstyle:RedundantModifier")
119 // For Externalizable
122 Proxy(final ShardSnapshot shardSnapshot) {
123 this.shardSnapshot = shardSnapshot;
127 public void writeExternal(ObjectOutput out) throws IOException {
128 out.writeObject(shardSnapshot.name);
129 out.writeObject(shardSnapshot.snapshot);
133 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
134 shardSnapshot = new ShardSnapshot((String)in.readObject(), (Snapshot) in.readObject());
137 private Object readResolve() {
138 return shardSnapshot;
142 private final String name;
143 private final Snapshot snapshot;
145 public ShardSnapshot(@Nonnull String name, @Nonnull Snapshot snapshot) {
146 this.name = Preconditions.checkNotNull(name);
147 this.snapshot = Preconditions.checkNotNull(snapshot);
151 public String getName() {
156 public Snapshot getSnapshot() {
160 private Object writeReplace() {
161 return new Proxy(this);