8f22c0ec2dc2b2fe5bed0033b2bd18657263c27d
[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 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.ObjectInput;
15 import java.io.ObjectOutput;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
18
19 /**
20  * Encapsulates the snapshot State for a Shard.
21  *
22  * @author Thomas Pantelis
23  */
24 public class ShardSnapshotState implements Snapshot.State {
25     private static final long serialVersionUID = 1L;
26
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 = new ShardSnapshotState(ShardDataTreeSnapshot.deserialize(in));
51         }
52
53         private Object readResolve() {
54             return snapshotState;
55         }
56     }
57
58     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
59             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
60             + "aren't serialized. FindBugs does not recognize this.")
61     private final ShardDataTreeSnapshot snapshot;
62
63     public ShardSnapshotState(@Nonnull final ShardDataTreeSnapshot snapshot) {
64         this.snapshot = Preconditions.checkNotNull(snapshot);
65     }
66
67     @Nonnull
68     public ShardDataTreeSnapshot getSnapshot() {
69         return snapshot;
70     }
71
72     private Object writeReplace() {
73         return new Proxy(this);
74     }
75 }