Add MXBean to report shard registered DTCL/DCL info
[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     @Override
51     public List<DataTreeListenerInfo> getDataChangeListenerInfo() {
52         return getListenerActorsInfo(getState().getDataChangeListenerActors());
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     private OnDemandShardState getState() {
57         try {
58             return stateCache.get();
59         } catch (Exception e) {
60             Throwables.throwIfUnchecked(e);
61             throw new RuntimeException(e);
62         }
63     }
64
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));
71         }
72
73         try {
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);
80         }
81     }
82 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.