f56d6ce01fb7804f597957758e8d7449209bcab8
[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.InputStream;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.OutputStream;
18 import javax.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 class ShardSnapshotState implements Snapshot.State {
27     private static final long serialVersionUID = 1L;
28
29     private static final class Proxy implements Externalizable {
30         private static final long serialVersionUID = 1L;
31
32         private ShardSnapshotState snapshotState;
33
34         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final ShardSnapshotState snapshotState) {
42             this.snapshotState = snapshotState;
43         }
44
45         @Override
46         public void writeExternal(final ObjectOutput out) throws IOException {
47             snapshotState.snapshot.serialize(toOutputStream(out));
48         }
49
50         private static OutputStream toOutputStream(final ObjectOutput out) {
51             if (out instanceof OutputStream) {
52                 return (OutputStream) out;
53             }
54
55             return new OutputStream() {
56                 @Override
57                 public void write(final int value) throws IOException {
58                     out.write(value);
59                 }
60             };
61         }
62
63         @Override
64         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
65             snapshotState = new ShardSnapshotState(ShardDataTreeSnapshot.deserialize(toInputStream(in)));
66         }
67
68         private static InputStream toInputStream(final ObjectInput in) {
69             if (in instanceof InputStream) {
70                 return (InputStream) in;
71             }
72
73             return new InputStream() {
74                 @Override
75                 public int read() throws IOException {
76                     return in.read();
77                 }
78             };
79         }
80
81         private Object readResolve() {
82             return snapshotState;
83         }
84     }
85
86     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
87             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
88             + "aren't serialized. FindBugs does not recognize this.")
89     private final ShardDataTreeSnapshot snapshot;
90
91     public ShardSnapshotState(@Nonnull final ShardDataTreeSnapshot snapshot) {
92         this.snapshot = Preconditions.checkNotNull(snapshot);
93     }
94
95     @Nonnull
96     public ShardDataTreeSnapshot getSnapshot() {
97         return snapshot;
98     }
99
100     private Object writeReplace() {
101         return new Proxy(this);
102     }
103 }