BUG-7965 Switch distributed-data backend to a separate shard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / PrefixShardConfiguration.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.controller.cluster.datastore.config;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import org.opendaylight.controller.cluster.access.concepts.MemberName;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
22
23 /**
24  * Configuration for prefix based shards.
25  */
26 public class PrefixShardConfiguration implements Serializable {
27     private static final class Proxy implements Externalizable {
28         private static final long serialVersionUID = 1L;
29
30         private PrefixShardConfiguration prefixShardConfiguration;
31
32         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
33         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
34         @SuppressWarnings("checkstyle:RedundantModifier")
35         public Proxy() {
36         }
37
38         Proxy(final PrefixShardConfiguration prefixShardConfiguration) {
39             this.prefixShardConfiguration = prefixShardConfiguration;
40         }
41
42         @Override
43         public void writeExternal(final ObjectOutput objectOutput) throws IOException {
44             objectOutput.writeObject(prefixShardConfiguration.getPrefix());
45             objectOutput.writeObject(prefixShardConfiguration.getShardStrategyName());
46
47             objectOutput.writeInt(prefixShardConfiguration.getShardMemberNames().size());
48             for (MemberName name : prefixShardConfiguration.getShardMemberNames()) {
49                 name.writeTo(objectOutput);
50             }
51         }
52
53         @Override
54         public void readExternal(final ObjectInput objectInput) throws IOException, ClassNotFoundException {
55             final DOMDataTreeIdentifier prefix =  (DOMDataTreeIdentifier) objectInput.readObject();
56             final String strategyName = (String) objectInput.readObject();
57
58             final int size = objectInput.readInt();
59             final Collection<MemberName> shardMemberNames = new ArrayList<>(size);
60             for (int i = 0; i < size; i++) {
61                 shardMemberNames.add(MemberName.readFrom(objectInput));
62             }
63
64             prefixShardConfiguration = new PrefixShardConfiguration(prefix, strategyName, shardMemberNames);
65         }
66
67         private Object readResolve() {
68             return prefixShardConfiguration;
69         }
70     }
71
72     private static final long serialVersionUID = 1L;
73
74     private final DOMDataTreeIdentifier prefix;
75     private final String shardStrategyName;
76     private final Collection<MemberName> shardMemberNames;
77
78     public PrefixShardConfiguration(final DOMDataTreeIdentifier prefix,
79                                     final String shardStrategyName,
80                                     final Collection<MemberName> shardMemberNames) {
81         this.prefix = Preconditions.checkNotNull(prefix);
82         this.shardStrategyName = Preconditions.checkNotNull(shardStrategyName);
83         this.shardMemberNames = ImmutableSet.copyOf(shardMemberNames);
84     }
85
86     public DOMDataTreeIdentifier getPrefix() {
87         return prefix;
88     }
89
90     public String getShardStrategyName() {
91         return shardStrategyName;
92     }
93
94     public Collection<MemberName> getShardMemberNames() {
95         return shardMemberNames;
96     }
97
98     @Override
99     public String toString() {
100         return "PrefixShardConfiguration{"
101                 + "prefix=" + prefix
102                 + ", shardStrategyName='"
103                 + shardStrategyName + '\''
104                 + ", shardMemberNames=" + shardMemberNames
105                 + '}';
106     }
107
108     private Object writeReplace() {
109         return new Proxy(this);
110     }
111 }