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 akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.dispatch.Futures;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Throwables;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.controller.cluster.datastore.messages.DataTreeListenerInfo;
22 import org.opendaylight.controller.cluster.datastore.messages.GetInfo;
23 import org.opendaylight.controller.cluster.datastore.messages.OnDemandShardState;
24 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
25 import scala.concurrent.Await;
26 import scala.concurrent.ExecutionContext;
27 import scala.concurrent.Future;
30 * Implementation of ShardDataTreeListenerInfoMXBean.
32 * @author Thomas Pantelis
34 public class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
35 private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
37 private final OnDemandShardStateCache stateCache;
39 public ShardDataTreeListenerInfoMXBeanImpl(final String shardName, final String mxBeanType,
40 final ActorRef shardActor) {
41 super(shardName, mxBeanType, JMX_CATEGORY);
42 stateCache = new OnDemandShardStateCache(shardName, Preconditions.checkNotNull(shardActor));
46 public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
47 return getListenerActorsInfo(getState().getTreeChangeListenerActors());
51 public List<DataTreeListenerInfo> getDataChangeListenerInfo() {
52 return getListenerActorsInfo(getState().getDataChangeListenerActors());
55 @SuppressWarnings("checkstyle:IllegalCatch")
56 private OnDemandShardState getState() {
58 return stateCache.get();
59 } catch (Exception e) {
60 Throwables.throwIfUnchecked(e);
61 throw new RuntimeException(e);
65 @SuppressWarnings("checkstyle:IllegalCatch")
66 private List<DataTreeListenerInfo> getListenerActorsInfo(Collection<ActorSelection> actors) {
67 final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
68 final List<Future<Object>> futureList = new ArrayList<>(actors.size());
69 for (ActorSelection actor: actors) {
70 futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
74 final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
75 Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
76 timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
77 return listenerInfoList;
78 } catch (Exception e) {
79 throw new RuntimeException(e);