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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
23 * Encapsulates the snapshot State for a Shard.
25 * @author Thomas Pantelis
27 public final class ShardSnapshotState implements Snapshot.State {
28 interface SerialForm extends Externalizable {
30 ShardSnapshotState snapshotState();
32 void resolveTo(@NonNull ShardSnapshotState newSnapshotState);
37 default void readExternal(final ObjectInput in) throws IOException {
38 resolveTo(ShardDataTreeSnapshot.deserialize(in));
42 default void writeExternal(final ObjectOutput out) throws IOException {
43 snapshotState().getSnapshot().serialize(out);
47 private static final class Proxy implements SerialForm {
48 private static final long serialVersionUID = 1L;
50 private ShardSnapshotState snapshotState;
52 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
53 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
54 @SuppressWarnings("checkstyle:RedundantModifier")
59 Proxy(final ShardSnapshotState snapshotState) {
60 this.snapshotState = snapshotState;
64 public ShardSnapshotState snapshotState() {
69 public void resolveTo(final ShardSnapshotState newSnapshotState) {
70 snapshotState = requireNonNull(newSnapshotState);
74 public Object readResolve() {
75 return verifyNotNull(snapshotState);
79 private static final long serialVersionUID = 1L;
81 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
82 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
83 + "aren't serialized. FindBugs does not recognize this.")
84 private final @NonNull ShardDataTreeSnapshot snapshot;
85 private final boolean migrated;
88 public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot, final boolean migrated) {
89 this.snapshot = requireNonNull(snapshot);
90 this.migrated = migrated;
93 public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot) {
94 this(snapshot, false);
97 public @NonNull ShardDataTreeSnapshot getSnapshot() {
102 public boolean needsMigration() {
106 private Object writeReplace() {
107 return new Proxy(this);