Bug 7521: Convert byte[] to ShardManagerSnapshot in DatastoreSnapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / DatastoreSnapshot.java
1 /*
2  * Copyright (c) 2015 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 com.google.common.collect.ImmutableList;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
22
23 /**
24  * Stores a snapshot of the internal state of a data store.
25  *
26  * @author Thomas Pantelis
27  */
28 public class DatastoreSnapshot implements Serializable {
29     private static final long serialVersionUID = 1L;
30
31     private static final class Proxy implements Externalizable {
32         private static final long serialVersionUID = 1L;
33
34         private DatastoreSnapshot datastoreSnapshot;
35
36         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
37         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
38         @SuppressWarnings("checkstyle:RedundantModifier")
39         public Proxy() {
40             // For Externalizable
41         }
42
43         Proxy(final DatastoreSnapshot datastoreSnapshot) {
44             this.datastoreSnapshot = datastoreSnapshot;
45         }
46
47         @Override
48         public void writeExternal(ObjectOutput out) throws IOException {
49             out.writeObject(datastoreSnapshot.type);
50             out.writeObject(datastoreSnapshot.shardManagerSnapshot);
51
52             out.writeInt(datastoreSnapshot.shardSnapshots.size());
53             for (ShardSnapshot shardSnapshot: datastoreSnapshot.shardSnapshots) {
54                 out.writeObject(shardSnapshot);
55             }
56         }
57
58         @Override
59         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
60             String type = (String)in.readObject();
61             ShardManagerSnapshot shardManagerSnapshot = (ShardManagerSnapshot) in.readObject();
62
63             int size = in.readInt();
64             List<ShardSnapshot> shardSnapshots = new ArrayList<>(size);
65             for (int i = 0; i < size; i++) {
66                 shardSnapshots.add((ShardSnapshot) in.readObject());
67             }
68
69             datastoreSnapshot = new DatastoreSnapshot(type, shardManagerSnapshot, shardSnapshots);
70         }
71
72         private Object readResolve() {
73             return datastoreSnapshot;
74         }
75     }
76
77     private final String type;
78     private final ShardManagerSnapshot shardManagerSnapshot;
79     private final List<ShardSnapshot> shardSnapshots;
80
81     public DatastoreSnapshot(@Nonnull String type, @Nullable ShardManagerSnapshot shardManagerSnapshot,
82             @Nonnull List<ShardSnapshot> shardSnapshots) {
83         this.type = Preconditions.checkNotNull(type);
84         this.shardManagerSnapshot = shardManagerSnapshot;
85         this.shardSnapshots = ImmutableList.copyOf(Preconditions.checkNotNull(shardSnapshots));
86     }
87
88     @Nonnull
89     public String getType() {
90         return type;
91     }
92
93     @Nullable
94     public ShardManagerSnapshot getShardManagerSnapshot() {
95         return shardManagerSnapshot;
96     }
97
98     @Nonnull
99     public List<ShardSnapshot> getShardSnapshots() {
100         return shardSnapshots;
101     }
102
103     @SuppressWarnings("static-method")
104     private Object writeReplace() {
105         return new Proxy(this);
106     }
107
108     public static class ShardSnapshot implements Serializable {
109         private static final long serialVersionUID = 1L;
110
111         private static final class Proxy implements Externalizable {
112             private static final long serialVersionUID = 1L;
113
114             private ShardSnapshot shardSnapshot;
115
116             // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
117             // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
118             @SuppressWarnings("checkstyle:RedundantModifier")
119             public Proxy() {
120                 // For Externalizable
121             }
122
123             Proxy(final ShardSnapshot shardSnapshot) {
124                 this.shardSnapshot = shardSnapshot;
125             }
126
127             @Override
128             public void writeExternal(ObjectOutput out) throws IOException {
129                 out.writeObject(shardSnapshot.name);
130                 out.writeObject(shardSnapshot.snapshot);
131             }
132
133             @Override
134             public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
135                 shardSnapshot = new ShardSnapshot((String)in.readObject(), (Snapshot) in.readObject());
136             }
137
138             private Object readResolve() {
139                 return shardSnapshot;
140             }
141         }
142
143         private final String name;
144         private final Snapshot snapshot;
145
146         public ShardSnapshot(@Nonnull String name, @Nonnull Snapshot snapshot) {
147             this.name = Preconditions.checkNotNull(name);
148             this.snapshot = Preconditions.checkNotNull(snapshot);
149         }
150
151         @Nonnull
152         public String getName() {
153             return name;
154         }
155
156         @Nonnull
157         public Snapshot getSnapshot() {
158             return snapshot;
159         }
160
161         @SuppressWarnings("static-method")
162         private Object writeReplace() {
163             return new Proxy(this);
164         }
165     }
166 }

©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.