BUG-2138: Create DistributedShardFrontend
[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 akka.cluster.ddata.ReplicatedData;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableSet;
14 import java.io.Serializable;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19
20 /**
21  * Configuration for prefix based shards.
22  */
23 public class PrefixShardConfiguration implements ReplicatedData, Serializable {
24     private static final long serialVersionUID = 1L;
25
26     private final DOMDataTreeIdentifier prefix;
27     private final String shardStrategyName;
28     private final Collection<MemberName> shardMemberNames;
29
30     public PrefixShardConfiguration(final DOMDataTreeIdentifier prefix,
31                                     final String shardStrategyName,
32                                     final Collection<MemberName> shardMemberNames) {
33         this.prefix = Preconditions.checkNotNull(prefix);
34         this.shardStrategyName = Preconditions.checkNotNull(shardStrategyName);
35         this.shardMemberNames = ImmutableSet.copyOf(shardMemberNames);
36     }
37
38     public DOMDataTreeIdentifier getPrefix() {
39         return prefix;
40     }
41
42     public String getShardStrategyName() {
43         return shardStrategyName;
44     }
45
46     public Collection<MemberName> getShardMemberNames() {
47         return shardMemberNames;
48     }
49
50     @Override
51     public String toString() {
52         return "PrefixShardConfiguration{"
53                 + "prefix=" + prefix
54                 + ", shardStrategyName='"
55                 + shardStrategyName + '\''
56                 + ", shardMemberNames=" + shardMemberNames
57                 + '}';
58     }
59
60     public String toDataMapKey() {
61         return "prefix=" + prefix;
62     }
63
64     @Override
65     public ReplicatedData merge(final ReplicatedData replicatedData) {
66         if (!(replicatedData instanceof PrefixShardConfiguration)) {
67             throw new IllegalStateException("replicatedData expected to be instance of PrefixShardConfiguration");
68         }
69         final PrefixShardConfiguration entry = (PrefixShardConfiguration) replicatedData;
70         if (!entry.getPrefix().equals(prefix)) {
71             // this should never happen since the key is the prefix
72             // if it does just return current?
73             return this;
74         }
75         final HashSet<MemberName> members = new HashSet<>(shardMemberNames);
76         members.addAll(entry.getShardMemberNames());
77         return new PrefixShardConfiguration(prefix, shardStrategyName, members);
78     }
79 }