X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fpersisted%2FShardSnapshotState.java;fp=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fpersisted%2FShardSnapshotState.java;h=d4556cd374b18ff5e105382805b0bb70cdcab3f6;hp=0000000000000000000000000000000000000000;hb=2faf656bf68dd3843fd59520b27a7ec2abbdcc68;hpb=d04b71990a802071a786fe8f0df57bc4adbdec3f diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/ShardSnapshotState.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/ShardSnapshotState.java new file mode 100644 index 0000000000..d4556cd374 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/persisted/ShardSnapshotState.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2017 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.datastore.persisted; + +import com.google.common.base.Preconditions; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.Externalizable; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.OutputStream; +import javax.annotation.Nonnull; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; + +/** + * Encapsulates the snapshot State for a Shard. + * + * @author Thomas Pantelis + */ +public class ShardSnapshotState implements Snapshot.State { + private static final long serialVersionUID = 1L; + + private static final class Proxy implements Externalizable { + private static final long serialVersionUID = 1L; + + private ShardSnapshotState snapshotState; + + // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't + // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection. + @SuppressWarnings("checkstyle:RedundantModifier") + public Proxy() { + // For Externalizable + } + + Proxy(final ShardSnapshotState snapshotState) { + this.snapshotState = snapshotState; + } + + @Override + public void writeExternal(final ObjectOutput out) throws IOException { + snapshotState.snapshot.serialize(toOutputStream(out)); + } + + private OutputStream toOutputStream(final ObjectOutput out) { + if (out instanceof OutputStream) { + return (OutputStream) out; + } + + return new OutputStream() { + @Override + public void write(int value) throws IOException { + out.write(value); + } + }; + } + + @Override + public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { + snapshotState = new ShardSnapshotState(ShardDataTreeSnapshot.deserialize(toInputStream(in))); + } + + private InputStream toInputStream(final ObjectInput in) { + if (in instanceof InputStream) { + return (InputStream) in; + } + + return new InputStream() { + @Override + public int read() throws IOException { + return in.read(); + } + }; + } + + private Object readResolve() { + return snapshotState; + } + } + + @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class " + + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class " + + "aren't serialized. FindBugs does not recognize this.") + private final ShardDataTreeSnapshot snapshot; + + public ShardSnapshotState(@Nonnull ShardDataTreeSnapshot snapshot) { + this.snapshot = Preconditions.checkNotNull(snapshot); + } + + @Nonnull + public ShardDataTreeSnapshot getSnapshot() { + return snapshot; + } + + @SuppressWarnings("static-method") + private Object writeReplace() { + return new Proxy(this); + } +}