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;
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 com.google.common.collect.Streams;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import java.util.stream.Collectors;
26 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardDataTreeListenerInfoMXBean;
27 import org.opendaylight.controller.cluster.datastore.messages.GetInfo;
28 import org.opendaylight.controller.cluster.datastore.messages.OnDemandShardState;
29 import org.opendaylight.controller.cluster.mgmt.api.DataTreeListenerInfo;
30 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
31 import scala.concurrent.Await;
32 import scala.concurrent.ExecutionContext;
33 import scala.concurrent.Future;
36 * Implementation of ShardDataTreeListenerInfoMXBean.
38 * @author Thomas Pantelis
40 final class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
41 private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
43 private final OnDemandShardStateCache stateCache;
45 ShardDataTreeListenerInfoMXBeanImpl(final String shardName, final String mxBeanType, final ActorRef shardActor) {
46 super(shardName, mxBeanType, JMX_CATEGORY);
47 stateCache = new OnDemandShardStateCache(shardName, requireNonNull(shardActor));
51 public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
52 return getListenerActorsInfo(getState().getTreeChangeListenerActors());
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 @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Akka's Await.result() API contract")
67 private static List<DataTreeListenerInfo> getListenerActorsInfo(final Collection<ActorSelection> actors) {
68 final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
69 final List<Future<Object>> futureList = new ArrayList<>(actors.size());
70 for (ActorSelection actor: actors) {
71 futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
74 final Iterable<Object> listenerInfos;
76 listenerInfos = Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
78 } catch (TimeoutException | InterruptedException e) {
79 throw new IllegalStateException("Failed to acquire listeners", e);
82 return Streams.stream(listenerInfos).map(DataTreeListenerInfo.class::cast).collect(Collectors.toList());