Cleanup warnings
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardManagerInfo.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.shardmanager;
10
11 import akka.actor.ActorRef;
12 import akka.pattern.Patterns;
13 import com.google.common.base.Preconditions;
14 import java.util.List;
15 import org.opendaylight.controller.cluster.access.concepts.MemberName;
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
17 import org.opendaylight.controller.cluster.raft.RaftState;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.Await;
22 import scala.concurrent.duration.Duration;
23
24 final class ShardManagerInfo extends AbstractMXBean implements ShardManagerInfoMBean {
25
26     public static final String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
27
28     private static final Logger LOG = LoggerFactory.getLogger(ShardManagerInfo.class);
29     private static final long ASK_TIMEOUT_MILLIS = 5000;
30
31     private final ActorRef shardManager;
32     private final MemberName memberName;
33
34     private volatile boolean syncStatus = false;
35
36
37     ShardManagerInfo(final ActorRef shardManager, final MemberName memberName, final String name,
38         final String mxBeanType) {
39         super(name, mxBeanType, JMX_CATEGORY_SHARD_MANAGER);
40         this.shardManager = Preconditions.checkNotNull(shardManager);
41         this.memberName = Preconditions.checkNotNull(memberName);
42     }
43
44     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
45     @Override
46     public List<String> getLocalShards() {
47         try {
48             return (List<String>) Await.result(
49                 Patterns.ask(shardManager, GetLocalShardIds.INSTANCE, ASK_TIMEOUT_MILLIS), Duration.Inf());
50         } catch (RuntimeException e) {
51             throw e;
52         } catch (Exception e) {
53             throw new RuntimeException(e);
54         }
55     }
56
57     @Override
58     public boolean getSyncStatus() {
59         return syncStatus;
60     }
61
62     void setSyncStatus(final boolean syncStatus) {
63         this.syncStatus = syncStatus;
64     }
65
66     @Override
67     public String getMemberName() {
68         return memberName.getName();
69     }
70
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     private void requestSwitchShardState(final ShardIdentifier shardId, final String newState, final long term) {
73         // Validates strings argument
74         final RaftState state = RaftState.valueOf(newState);
75
76         // Leader and Follower are the only states to which we can switch externally
77         switch (state) {
78             case Follower:
79             case Leader:
80                 try {
81                     Await.result(Patterns.ask(shardManager, new SwitchShardBehavior(shardId, state, term),
82                         ASK_TIMEOUT_MILLIS), Duration.Inf());
83                 } catch (RuntimeException e) {
84                     throw e;
85                 } catch (Exception e) {
86                     throw new RuntimeException(e);
87                 }
88                 break;
89             case Candidate:
90             case IsolatedLeader:
91             default:
92                 throw new IllegalArgumentException("Illegal target state " + state);
93         }
94     }
95
96     @Override
97     public void switchAllLocalShardsState(final String newState, final long term) {
98         LOG.info("switchAllLocalShardsState called newState = {}, term = {}", newState, term);
99         requestSwitchShardState(null, newState, term);
100     }
101
102     @Override
103     public void switchShardState(final String shardId, final String newState, final long term) {
104         final ShardIdentifier identifier = ShardIdentifier.fromShardIdString(shardId);
105         LOG.info("switchShardState called shardName = {}, newState = {}, term = {}", shardId, newState, term);
106         requestSwitchShardState(identifier, newState, term);
107     }
108 }