Define efficient serialization proxies
[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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
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;
21
22 /**
23  * Encapsulates the snapshot State for a Shard.
24  *
25  * @author Thomas Pantelis
26  */
27 public final class ShardSnapshotState implements Snapshot.State {
28     interface SerialForm extends Externalizable {
29
30         ShardSnapshotState snapshotState();
31
32         void resolveTo(@NonNull ShardSnapshotState newSnapshotState);
33
34         Object readResolve();
35
36         @Override
37         default void readExternal(final ObjectInput in) throws IOException {
38             resolveTo(ShardDataTreeSnapshot.deserialize(in));
39         }
40
41         @Override
42         default void writeExternal(final ObjectOutput out) throws IOException {
43             snapshotState().getSnapshot().serialize(out);
44         }
45     }
46
47     private static final class Proxy implements SerialForm {
48         private static final long serialVersionUID = 1L;
49
50         private ShardSnapshotState snapshotState;
51
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")
55         public Proxy() {
56             // For Externalizable
57         }
58
59         Proxy(final ShardSnapshotState snapshotState) {
60             this.snapshotState = snapshotState;
61         }
62
63         @Override
64         public ShardSnapshotState snapshotState() {
65             return snapshotState;
66         }
67
68         @Override
69         public void resolveTo(final ShardSnapshotState newSnapshotState) {
70             snapshotState = requireNonNull(newSnapshotState);
71         }
72
73         @Override
74         public Object readResolve() {
75             return verifyNotNull(snapshotState);
76         }
77     }
78
79     private static final long serialVersionUID = 1L;
80
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;
86
87     @VisibleForTesting
88     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot, final boolean migrated) {
89         this.snapshot = requireNonNull(snapshot);
90         this.migrated = migrated;
91     }
92
93     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot) {
94         this(snapshot, false);
95     }
96
97     public @NonNull ShardDataTreeSnapshot getSnapshot() {
98         return snapshot;
99     }
100
101     @Override
102     public boolean needsMigration() {
103         return migrated;
104     }
105
106     private Object writeReplace() {
107         return new Proxy(this);
108     }
109 }