Revert "Leader should always apply modifications as local"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardPeerAddressResolver.java
1 /*
2  * Copyright (c) 2015 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.shardmanager;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.Address;
13 import akka.actor.AddressFromURIString;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.controller.cluster.access.concepts.MemberName;
21 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
22 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
23 import org.opendaylight.controller.cluster.raft.PeerAddressResolver;
24
25 /**
26  * Implementation PeerAddressResolver that resolves address for shard peer ids. This class is owned by the
27  * ShardMaanager and passed to Shard actors via the ConfigParams.
28  *
29  * @author Thomas Pantelis
30  */
31 class ShardPeerAddressResolver implements PeerAddressResolver {
32     // Stores a mapping between a member name and the address of the member. The map is concurrent as it
33     // will be accessed by multiple threads via the public resolve method.
34     private final ConcurrentMap<MemberName, Address> memberNameToAddress = new ConcurrentHashMap<>();
35     private final String shardManagerIdentifier;
36     private final String shardManagerType;
37     private final MemberName localMemberName;
38
39     ShardPeerAddressResolver(final String shardManagerType, final MemberName localMemberName) {
40         this.shardManagerIdentifier = ShardManagerIdentifier.builder().type(shardManagerType).build().toString();
41         this.shardManagerType = shardManagerType;
42         this.localMemberName = requireNonNull(localMemberName);
43     }
44
45     void addPeerAddress(final MemberName memberName, final Address address) {
46         memberNameToAddress.put(memberName, address);
47     }
48
49     void removePeerAddress(final MemberName memberName) {
50         memberNameToAddress.remove(memberName);
51     }
52
53     Set<MemberName> getPeerMembers() {
54         return this.memberNameToAddress.keySet();
55     }
56
57     Address getPeerAddress(final MemberName memberName) {
58         return memberNameToAddress.get(memberName);
59     }
60
61     Collection<String> getShardManagerPeerActorAddresses() {
62         Collection<String> peerAddresses = new ArrayList<>();
63         for (Map.Entry<MemberName, Address> entry: memberNameToAddress.entrySet()) {
64             if (!localMemberName.equals(entry.getKey())) {
65                 peerAddresses.add(getShardManagerActorPathBuilder(entry.getValue()).toString());
66             }
67         }
68
69         return peerAddresses;
70     }
71
72     ShardIdentifier getShardIdentifier(final MemberName memberName, final String shardName) {
73         return ShardIdentifier.create(shardName, memberName, shardManagerType);
74     }
75
76     String getShardActorAddress(final String shardName, final MemberName memberName) {
77         Address memberAddress = memberNameToAddress.get(memberName);
78         if (memberAddress != null) {
79             return getShardManagerActorPathBuilder(memberAddress).append("/").append(
80                     getShardIdentifier(memberName, shardName)).toString();
81         }
82
83         return null;
84     }
85
86     StringBuilder getShardManagerActorPathBuilder(final Address address) {
87         return new StringBuilder().append(address.toString()).append("/user/").append(shardManagerIdentifier);
88     }
89
90     @Override
91     public String resolve(final String peerId) {
92         if (peerId == null) {
93             return null;
94         }
95
96         ShardIdentifier shardId = ShardIdentifier.fromShardIdString(peerId);
97         return getShardActorAddress(shardId.getShardName(), shardId.getMemberName());
98     }
99
100     @Override
101     public void setResolved(final String peerId, final String address) {
102         memberNameToAddress.put(ShardIdentifier.fromShardIdString(peerId).getMemberName(),
103                 AddressFromURIString.parse(address));
104     }
105 }

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