Merge "Optimizations, Monitoring and Logging"
[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.cluster.Cluster;
14 import akka.cluster.ClusterEvent;
15 import com.google.common.base.Preconditions;
16
17 public class ClusterWrapperImpl implements ClusterWrapper {
18     private final Cluster cluster;
19     private final String currentMemberName;
20
21     public ClusterWrapperImpl(ActorSystem actorSystem){
22         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
23
24         cluster = Cluster.get(actorSystem);
25
26         Preconditions.checkState(cluster.getSelfRoles().size() > 0,
27             "No akka roles were specified\n" +
28                 "One way to specify the member name is to pass a property on the command line like so\n" +
29                 "   -Dakka.cluster.roles.0=member-3\n" +
30                 "member-3 here would be the name of the member"
31         );
32
33         currentMemberName = (String) cluster.getSelfRoles().toArray()[0];
34
35     }
36
37     public void subscribeToMemberEvents(ActorRef actorRef){
38         Preconditions.checkNotNull(actorRef, "actorRef should not be null");
39
40         cluster.subscribe(actorRef, ClusterEvent.initialStateAsEvents(),
41             ClusterEvent.MemberEvent.class,
42             ClusterEvent.UnreachableMember.class);
43     }
44
45     public String getCurrentMemberName() {
46         return currentMemberName;
47     }
48 }