152ffd368fdd68b76b8cdda063860902dfbd4460
[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 = new ShardSnapshotState(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 ShardDataTreeSnapshot snapshot;
63
64     public ShardSnapshotState(final @NonNull ShardDataTreeSnapshot snapshot) {
65         this.snapshot = requireNonNull(snapshot);
66     }
67
68     public @NonNull ShardDataTreeSnapshot getSnapshot() {
69         return snapshot;
70     }
71
72     private Object writeReplace() {
73         return new Proxy(this);
74     }
75 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.