Bug-2136 - fix for is-local-path
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ClusterWrapperImpl.java
1 /*
2  * Copyright (c) 2014 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;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.actor.Address;
14 import akka.cluster.Cluster;
15 import akka.cluster.ClusterEvent;
16 import com.google.common.base.Preconditions;
17
18 public class ClusterWrapperImpl implements ClusterWrapper {
19     private final Cluster cluster;
20     private final String currentMemberName;
21     private final Address selfAddress;
22
23     public ClusterWrapperImpl(ActorSystem actorSystem){
24         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
25
26         cluster = Cluster.get(actorSystem);
27
28         Preconditions.checkState(cluster.getSelfRoles().size() > 0,
29             "No akka roles were specified\n" +
30                 "One way to specify the member name is to pass a property on the command line like so\n" +
31                 "   -Dakka.cluster.roles.0=member-3\n" +
32                 "member-3 here would be the name of the member"
33         );
34
35         currentMemberName = (String) cluster.getSelfRoles().toArray()[0];
36         selfAddress = cluster.selfAddress();
37
38     }
39
40     public void subscribeToMemberEvents(ActorRef actorRef){
41         Preconditions.checkNotNull(actorRef, "actorRef should not be null");
42
43         cluster.subscribe(actorRef, ClusterEvent.initialStateAsEvents(),
44             ClusterEvent.MemberEvent.class,
45             ClusterEvent.UnreachableMember.class);
46     }
47
48     public String getCurrentMemberName() {
49         return currentMemberName;
50     }
51
52     public Address getSelfAddress() {
53         return selfAddress;
54     }
55 }