Add UnsignedLongBitmap
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / 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;
9
10 import static java.util.Objects.requireNonNull;
11
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;
34
35 /**
36  * Implementation of ShardDataTreeListenerInfoMXBean.
37  *
38  * @author Thomas Pantelis
39  */
40 final class ShardDataTreeListenerInfoMXBeanImpl extends AbstractMXBean implements ShardDataTreeListenerInfoMXBean {
41     private static final String JMX_CATEGORY = "ShardDataTreeListenerInfo";
42
43     private final OnDemandShardStateCache stateCache;
44
45     ShardDataTreeListenerInfoMXBeanImpl(final String shardName, final String mxBeanType, final ActorRef shardActor) {
46         super(shardName, mxBeanType, JMX_CATEGORY);
47         stateCache = new OnDemandShardStateCache(shardName, requireNonNull(shardActor));
48     }
49
50     @Override
51     public List<DataTreeListenerInfo> getDataTreeChangeListenerInfo() {
52         return getListenerActorsInfo(getState().getTreeChangeListenerActors());
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     @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));
72         }
73
74         final Iterable<Object> listenerInfos;
75         try {
76             listenerInfos = Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
77                 timeout.duration());
78         } catch (TimeoutException | InterruptedException e) {
79             throw new IllegalStateException("Failed to acquire listeners", e);
80         }
81
82         return Streams.stream(listenerInfos).map(DataTreeListenerInfo.class::cast).collect(Collectors.toList());
83     }
84 }