2 * Copyright (c) 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
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.HashMap;
19 import java.util.List;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
26 * Represents the persisted snapshot state for the ShardManager.
28 * @author Thomas Pantelis
30 public class ShardManagerSnapshot implements Serializable {
31 private static final long serialVersionUID = 1L;
33 private static final class Proxy implements Externalizable {
34 private static final long serialVersionUID = 1L;
36 private ShardManagerSnapshot snapshot;
38 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
39 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
40 @SuppressWarnings("checkstyle:RedundantModifier")
45 Proxy(final ShardManagerSnapshot snapshot) {
46 this.snapshot = snapshot;
50 public void writeExternal(ObjectOutput out) throws IOException {
51 out.writeInt(snapshot.shardList.size());
52 for (String shard: snapshot.shardList) {
53 out.writeObject(shard);
56 out.writeInt(snapshot.prefixShardConfiguration.size());
57 for (Map.Entry prefixShardConfigEntry : snapshot.prefixShardConfiguration.entrySet()) {
58 out.writeObject(prefixShardConfigEntry.getKey());
59 out.writeObject(prefixShardConfigEntry.getValue());
64 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
65 int size = in.readInt();
66 List<String> shardList = new ArrayList<>(size);
67 for (int i = 0; i < size; i++) {
68 shardList.add((String) in.readObject());
72 Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixShardConfiguration = new HashMap<>(size);
73 for (int i = 0; i < size; i++) {
74 prefixShardConfiguration.put((DOMDataTreeIdentifier) in.readObject(),
75 (PrefixShardConfiguration) in.readObject());
78 snapshot = new ShardManagerSnapshot(shardList, prefixShardConfiguration);
81 private Object readResolve() {
86 private final List<String> shardList;
87 private final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixShardConfiguration;
89 public ShardManagerSnapshot(@Nonnull final List<String> shardList,
90 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixShardConfiguration) {
91 this.shardList = ImmutableList.copyOf(shardList);
92 this.prefixShardConfiguration = ImmutableMap.copyOf(prefixShardConfiguration);
95 public List<String> getShardList() {
96 return this.shardList;
99 private Object writeReplace() {
100 return new Proxy(this);
104 public String toString() {
105 return "ShardManagerSnapshot [ShardList = " + shardList + " ]";