BUG-2138: Create DistributedShardFrontend
[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 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;
20 import java.util.Map;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
24
25 /**
26  * Represents the persisted snapshot state for the ShardManager.
27  *
28  * @author Thomas Pantelis
29  */
30 public class ShardManagerSnapshot implements Serializable {
31     private static final long serialVersionUID = 1L;
32
33     private static final class Proxy implements Externalizable {
34         private static final long serialVersionUID = 1L;
35
36         private ShardManagerSnapshot snapshot;
37
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")
41         public Proxy() {
42             // For Externalizable
43         }
44
45         Proxy(final ShardManagerSnapshot snapshot) {
46             this.snapshot = snapshot;
47         }
48
49         @Override
50         public void writeExternal(ObjectOutput out) throws IOException {
51             out.writeInt(snapshot.shardList.size());
52             for (String shard: snapshot.shardList) {
53                 out.writeObject(shard);
54             }
55
56             out.writeInt(snapshot.prefixShardConfiguration.size());
57             for (Map.Entry prefixShardConfigEntry : snapshot.prefixShardConfiguration.entrySet()) {
58                 out.writeObject(prefixShardConfigEntry.getKey());
59                 out.writeObject(prefixShardConfigEntry.getValue());
60             }
61         }
62
63         @Override
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());
69             }
70
71             size = in.readInt();
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());
76             }
77
78             snapshot = new ShardManagerSnapshot(shardList, prefixShardConfiguration);
79         }
80
81         private Object readResolve() {
82             return snapshot;
83         }
84     }
85
86     private final List<String> shardList;
87     private final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixShardConfiguration;
88
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);
93     }
94
95     public List<String> getShardList() {
96         return this.shardList;
97     }
98
99     private Object writeReplace() {
100         return new Proxy(this);
101     }
102
103     @Override
104     public String toString() {
105         return "ShardManagerSnapshot [ShardList = " + shardList + " ]";
106     }
107 }

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