2 * Copyright (c) 2017 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.OutputStream;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
22 * Encapsulates the snapshot State for a Shard.
24 * @author Thomas Pantelis
26 public class ShardSnapshotState implements Snapshot.State {
27 private static final long serialVersionUID = 1L;
29 private static final class Proxy implements Externalizable {
30 private static final long serialVersionUID = 1L;
32 private ShardSnapshotState snapshotState;
34 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36 @SuppressWarnings("checkstyle:RedundantModifier")
41 Proxy(final ShardSnapshotState snapshotState) {
42 this.snapshotState = snapshotState;
46 public void writeExternal(final ObjectOutput out) throws IOException {
47 snapshotState.snapshot.serialize(toOutputStream(out));
50 private OutputStream toOutputStream(final ObjectOutput out) {
51 if (out instanceof OutputStream) {
52 return (OutputStream) out;
55 return new OutputStream() {
57 public void write(int value) throws IOException {
64 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
65 snapshotState = new ShardSnapshotState(ShardDataTreeSnapshot.deserialize(toInputStream(in)));
68 private InputStream toInputStream(final ObjectInput in) {
69 if (in instanceof InputStream) {
70 return (InputStream) in;
73 return new InputStream() {
75 public int read() throws IOException {
81 private Object readResolve() {
86 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
87 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
88 + "aren't serialized. FindBugs does not recognize this.")
89 private final ShardDataTreeSnapshot snapshot;
91 public ShardSnapshotState(@Nonnull ShardDataTreeSnapshot snapshot) {
92 this.snapshot = Preconditions.checkNotNull(snapshot);
96 public ShardDataTreeSnapshot getSnapshot() {
100 @SuppressWarnings("static-method")
101 private Object writeReplace() {
102 return new Proxy(this);