Fix modernization issues
[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 java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.concurrent.TimeUnit;
22 import org.opendaylight.controller.cluster.datastore.messages.DataTreeListenerInfo;
23 import org.opendaylight.controller.cluster.datastore.messages.GetInfo;
24 import org.opendaylight.controller.cluster.datastore.messages.OnDemandShardState;
25 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
26 import scala.concurrent.Await;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
29
30 /**
31  * Implementation of ShardDataTreeListenerInfoMXBean.
32  *
33  * @author Thomas Pantelis
34  */
35 public class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
36     private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
37
38     private final OnDemandShardStateCache stateCache;
39
40     public ShardDataTreeListenerInfoMXBeanImpl(final String shardName, final String mxBeanType,
41             final ActorRef shardActor) {
42         super(shardName, mxBeanType, JMX_CATEGORY);
43         stateCache = new OnDemandShardStateCache(shardName, requireNonNull(shardActor));
44     }
45
46     @Override
47     public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
48         return getListenerActorsInfo(getState().getTreeChangeListenerActors());
49     }
50
51     @SuppressWarnings("checkstyle:IllegalCatch")
52     private OnDemandShardState getState() {
53         try {
54             return stateCache.get();
55         } catch (Exception e) {
56             Throwables.throwIfUnchecked(e);
57             throw new RuntimeException(e);
58         }
59     }
60
61     @SuppressWarnings("checkstyle:IllegalCatch")
62     private static List<DataTreeListenerInfo> getListenerActorsInfo(Collection<ActorSelection> actors) {
63         final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
64         final List<Future<Object>> futureList = new ArrayList<>(actors.size());
65         for (ActorSelection actor: actors) {
66             futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
67         }
68
69         try {
70             final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
71             Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
72                     timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
73             return listenerInfoList;
74         } catch (Exception e) {
75             throw new RuntimeException(e);
76         }
77     }
78 }