ShardSnapshotState is final
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardSnapshotState.java
1 /*
2  * Copyright (c) 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
20
21 /**
22  * Encapsulates the snapshot State for a Shard.
23  *
24  * @author Thomas Pantelis
25  */
26 public final class ShardSnapshotState implements Snapshot.State {
27     private static final class Proxy implements Externalizable {
28         private static final long serialVersionUID = 1L;
29
30         private ShardSnapshotState snapshotState;
31
32         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
33         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
34         @SuppressWarnings("checkstyle:RedundantModifier")
35         public Proxy() {
36             // For Externalizable
37         }
38
39         Proxy(final ShardSnapshotState snapshotState) {
40             this.snapshotState = snapshotState;
41         }
42
43         @Override
44         public void writeExternal(final ObjectOutput out) throws IOException {
45             snapshotState.snapshot.serialize(out);
46         }
47
48         @Override
49         public void readExternal(final ObjectInput in) throws IOException {
50             snapshotState = ShardDataTreeSnapshot.deserialize(in);
51         }
52
53         private Object readResolve() {
54             return snapshotState;
55         }
56     }
57
58     private static final long serialVersionUID = 1L;
59
60     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
61             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
62             + "aren't serialized. FindBugs does not recognize this.")
63     private final @NonNull ShardDataTreeSnapshot snapshot;
64     private final boolean migrated;
65
66     @VisibleForTesting
67     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot, final boolean migrated) {
68         this.snapshot = requireNonNull(snapshot);
69         this.migrated = migrated;
70     }
71
72     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot) {
73         this(snapshot, false);
74     }
75
76     public @NonNull ShardDataTreeSnapshot getSnapshot() {
77         return snapshot;
78     }
79
80     @Override
81     public boolean needsMigration() {
82         return migrated;
83     }
84
85     private Object writeReplace() {
86         return new Proxy(this);
87     }
88 }