Fix modernization issues
[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 package org.opendaylight.controller.cluster.datastore;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.actor.Address;
16 import akka.cluster.Cluster;
17 import akka.cluster.ClusterEvent;
18 import org.opendaylight.controller.cluster.access.concepts.MemberName;
19
20 public class ClusterWrapperImpl implements ClusterWrapper {
21     private final Cluster cluster;
22     private final MemberName currentMemberName;
23     private final Address selfAddress;
24
25     public ClusterWrapperImpl(final ActorSystem actorSystem) {
26         cluster = Cluster.get(requireNonNull(actorSystem, "actorSystem should not be null"));
27
28         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         currentMemberName = MemberName.forName(cluster.getSelfRoles().iterator().next());
35         selfAddress = cluster.selfAddress();
36     }
37
38     @Override
39     public void subscribeToMemberEvents(final ActorRef actorRef) {
40         cluster.subscribe(requireNonNull(actorRef, "actorRef should not be null"), ClusterEvent.initialStateAsEvents(),
41             ClusterEvent.MemberEvent.class,
42             ClusterEvent.UnreachableMember.class,
43             ClusterEvent.ReachableMember.class);
44     }
45
46     @Override
47     public MemberName getCurrentMemberName() {
48         return currentMemberName;
49     }
50
51     @Override
52     public Address getSelfAddress() {
53         return selfAddress;
54     }
55 }