3108ee681c16e14bf1904d1bce258485e003471f
[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 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;
28
29 /**
30  * Implementation of ShardDataTreeListenerInfoMXBean.
31  *
32  * @author Thomas Pantelis
33  */
34 public class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
35     private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
36
37     private final OnDemandShardStateCache stateCache;
38
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));
43     }
44
45     @Override
46     public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
47         return getListenerActorsInfo(getState().getTreeChangeListenerActors());
48     }
49
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     private OnDemandShardState getState() {
52         try {
53             return stateCache.get();
54         } catch (Exception e) {
55             Throwables.throwIfUnchecked(e);
56             throw new RuntimeException(e);
57         }
58     }
59
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     private List<DataTreeListenerInfo> getListenerActorsInfo(Collection<ActorSelection> actors) {
62         final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
63         final List<Future<Object>> futureList = new ArrayList<>(actors.size());
64         for (ActorSelection actor: actors) {
65             futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
66         }
67
68         try {
69             final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
70             Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
71                     timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
72             return listenerInfoList;
73         } catch (Exception e) {
74             throw new RuntimeException(e);
75         }
76     }
77 }