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