2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
10 import static java.util.Objects.requireNonNull;
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;
31 * Implementation of ShardDataTreeListenerInfoMXBean.
33 * @author Thomas Pantelis
35 public class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
36 private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
38 private final OnDemandShardStateCache stateCache;
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));
47 public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
48 return getListenerActorsInfo(getState().getTreeChangeListenerActors());
51 @SuppressWarnings("checkstyle:IllegalCatch")
52 private OnDemandShardState getState() {
54 return stateCache.get();
55 } catch (Exception e) {
56 Throwables.throwIfUnchecked(e);
57 throw new RuntimeException(e);
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));
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);