Add @SupressFBWarnings around Await.result()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / jmx / mbeans / shard / ShardDataTreeListenerInfoMXBeanImpl.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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.jmx.mbeans.shard;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.dispatch.Futures;
15 import akka.pattern.Patterns;
16 import akka.util.Timeout;
17 import com.google.common.base.Throwables;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.concurrent.TimeUnit;
23 import org.opendaylight.controller.cluster.datastore.messages.DataTreeListenerInfo;
24 import org.opendaylight.controller.cluster.datastore.messages.GetInfo;
25 import org.opendaylight.controller.cluster.datastore.messages.OnDemandShardState;
26 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
27 import scala.concurrent.Await;
28 import scala.concurrent.ExecutionContext;
29 import scala.concurrent.Future;
30
31 /**
32  * Implementation of ShardDataTreeListenerInfoMXBean.
33  *
34  * @author Thomas Pantelis
35  */
36 public class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
37     private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
38
39     private final OnDemandShardStateCache stateCache;
40
41     public ShardDataTreeListenerInfoMXBeanImpl(final String shardName, final String mxBeanType,
42             final ActorRef shardActor) {
43         super(shardName, mxBeanType, JMX_CATEGORY);
44         stateCache = new OnDemandShardStateCache(shardName, requireNonNull(shardActor));
45     }
46
47     @Override
48     public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
49         return getListenerActorsInfo(getState().getTreeChangeListenerActors());
50     }
51
52     @SuppressWarnings("checkstyle:IllegalCatch")
53     private OnDemandShardState getState() {
54         try {
55             return stateCache.get();
56         } catch (Exception e) {
57             Throwables.throwIfUnchecked(e);
58             throw new RuntimeException(e);
59         }
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Akka's Await.result() API contract")
64     private static List<DataTreeListenerInfo> getListenerActorsInfo(final Collection<ActorSelection> actors) {
65         final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
66         final List<Future<Object>> futureList = new ArrayList<>(actors.size());
67         for (ActorSelection actor: actors) {
68             futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
69         }
70
71         try {
72             final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
73             Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
74                     timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
75             return listenerInfoList;
76         } catch (Exception e) {
77             throw new RuntimeException(e);
78         }
79     }
80 }