f6be1b808f648efb915edd6aa5264b55d1831937
[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 javax.annotation.Nonnull;
19
20 /**
21  * Represents the persisted snapshot state for the ShardManager.
22  *
23  * @author Thomas Pantelis
24  */
25 public class ShardManagerSnapshot implements Serializable {
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 ShardManagerSnapshot snapshot;
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 ShardManagerSnapshot snapshot) {
41             this.snapshot = snapshot;
42         }
43
44         @Override
45         public void writeExternal(ObjectOutput out) throws IOException {
46             out.writeInt(snapshot.shardList.size());
47             for (String shard: snapshot.shardList) {
48                 out.writeObject(shard);
49             }
50         }
51
52         @Override
53         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
54             int size = in.readInt();
55             List<String> shardList = new ArrayList<>(size);
56             for (int i = 0; i < size; i++) {
57                 shardList.add((String) in.readObject());
58             }
59
60             snapshot = new ShardManagerSnapshot(shardList);
61         }
62
63         private Object readResolve() {
64             return snapshot;
65         }
66     }
67
68     private final List<String> shardList;
69
70     public ShardManagerSnapshot(@Nonnull final List<String> shardList) {
71         this.shardList = ImmutableList.copyOf(shardList);
72     }
73
74     public List<String> getShardList() {
75         return this.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 }

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