03ffa9af793a2778c07fb119a4360e1b6819bb3d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardManagerSnapshot.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.collect.ImmutableList;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.io.Serializable;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNull;
19
20 /**
21  * Represents the persisted snapshot state for the ShardManager.
22  *
23  * @author Thomas Pantelis
24  */
25 public final class ShardManagerSnapshot implements Serializable {
26     private static final class Proxy implements Externalizable {
27         private static final long serialVersionUID = 1L;
28
29         private ShardManagerSnapshot snapshot;
30
31         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
32         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
33         @SuppressWarnings("checkstyle:RedundantModifier")
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final ShardManagerSnapshot snapshot) {
39             this.snapshot = snapshot;
40         }
41
42         @Override
43         public void writeExternal(final ObjectOutput out) throws IOException {
44             out.writeInt(snapshot.shardList.size());
45             for (String shard: snapshot.shardList) {
46                 out.writeObject(shard);
47             }
48         }
49
50         @Override
51         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
52             int size = in.readInt();
53             List<String> localShardList = new ArrayList<>(size);
54             for (int i = 0; i < size; i++) {
55                 localShardList.add((String) in.readObject());
56             }
57
58             snapshot = new ShardManagerSnapshot(localShardList);
59         }
60
61         private Object readResolve() {
62             return snapshot;
63         }
64     }
65
66     private static final long serialVersionUID = 1L;
67
68     private final List<String> shardList;
69
70     public ShardManagerSnapshot(final @NonNull List<String> shardList) {
71         this.shardList = ImmutableList.copyOf(shardList);
72     }
73
74     public List<String> getShardList() {
75         return shardList;
76     }
77
78     private Object writeReplace() {
79         return new Proxy(this);
80     }
81
82     @Override
83     public String toString() {
84         return "ShardManagerSnapshot [ShardList = " + shardList + " ]";
85     }
86 }