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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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 javax.annotation.Nonnull;
21 import javax.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 type = (String)in.readObject();
62 byte[] shardManagerSnapshot = (byte[]) in.readObject();
64 int size = in.readInt();
65 List<ShardSnapshot> shardSnapshots = new ArrayList<>(size);
66 for (int i = 0; i < size; i++) {
67 shardSnapshots.add((ShardSnapshot) in.readObject());
70 datastoreSnapshot = new DatastoreSnapshot(type, shardManagerSnapshot, shardSnapshots);
73 private Object readResolve() {
74 return datastoreSnapshot;
78 private final String type;
79 private final byte[] shardManagerSnapshot;
80 private final List<ShardSnapshot> shardSnapshots;
82 @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
83 + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
84 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
85 public DatastoreSnapshot(@Nonnull String type, @Nullable byte[] shardManagerSnapshot,
86 @Nonnull List<ShardSnapshot> shardSnapshots) {
87 this.type = Preconditions.checkNotNull(type);
88 this.shardManagerSnapshot = shardManagerSnapshot;
89 this.shardSnapshots = ImmutableList.copyOf(Preconditions.checkNotNull(shardSnapshots));
93 public String getType() {
97 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
98 + "this is OK since this class is merely a DTO and does not process byte[] internally. "
99 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
101 public byte[] getShardManagerSnapshot() {
102 return shardManagerSnapshot;
106 public List<ShardSnapshot> getShardSnapshots() {
107 return shardSnapshots;
110 @SuppressWarnings("static-method")
111 private Object writeReplace() {
112 return new Proxy(this);
115 public static class ShardSnapshot implements Serializable {
116 private static final long serialVersionUID = 1L;
118 private static final class Proxy implements Externalizable {
119 private static final long serialVersionUID = 1L;
121 private ShardSnapshot shardSnapshot;
123 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
124 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
125 @SuppressWarnings("checkstyle:RedundantModifier")
127 // For Externalizable
130 Proxy(final ShardSnapshot shardSnapshot) {
131 this.shardSnapshot = shardSnapshot;
135 public void writeExternal(ObjectOutput out) throws IOException {
136 out.writeObject(shardSnapshot.name);
137 out.writeObject(shardSnapshot.snapshot);
141 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
142 shardSnapshot = new ShardSnapshot((String)in.readObject(), (Snapshot) in.readObject());
145 private Object readResolve() {
146 return shardSnapshot;
150 private final String name;
151 private final Snapshot snapshot;
153 public ShardSnapshot(@Nonnull String name, @Nonnull Snapshot snapshot) {
154 this.name = Preconditions.checkNotNull(name);
155 this.snapshot = Preconditions.checkNotNull(snapshot);
159 public String getName() {
164 public Snapshot getSnapshot() {
168 @SuppressWarnings("static-method")
169 private Object writeReplace() {
170 return new Proxy(this);