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

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