2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.remote.rpc.registry.mbeans;
10 import static java.util.Objects.requireNonNull;
12 import akka.actor.Address;
13 import akka.util.Timeout;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.concurrent.TimeoutException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.opendaylight.controller.remote.rpc.registry.AbstractRoutingTable;
20 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.Await;
25 import scala.concurrent.Future;
26 import scala.concurrent.duration.FiniteDuration;
28 abstract class AbstractRegistryMXBean<T extends AbstractRoutingTable<T, I>, I> extends AbstractMXBean {
29 static final String LOCAL_CONSTANT = "local";
30 static final String ROUTE_CONSTANT = "route:";
31 static final String NAME_CONSTANT = " | name:";
33 @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
34 protected final Logger log = LoggerFactory.getLogger(getClass());
36 private final BucketStoreAccess bucketAccess;
37 private final FiniteDuration timeout;
39 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
40 justification = "registerMBean() is expected to be stateless")
41 AbstractRegistryMXBean(final @NonNull String beanName, final @NonNull String beanType,
42 final @NonNull BucketStoreAccess bucketAccess, final @NonNull Timeout timeout) {
43 super(beanName, beanType, null);
44 this.bucketAccess = requireNonNull(bucketAccess);
45 this.timeout = timeout.duration();
49 @SuppressWarnings({"unchecked", "rawtypes"})
52 return (T) Await.result((Future) bucketAccess.getLocalData(), timeout);
53 } catch (InterruptedException | TimeoutException e) {
54 throw new IllegalStateException("getLocalData failed", e);
58 @SuppressWarnings({"unchecked", "rawtypes"})
59 final Map<Address, Bucket<T>> remoteBuckets() {
61 return (Map<Address, Bucket<T>>) Await.result((Future)bucketAccess.getRemoteBuckets(), timeout);
62 } catch (InterruptedException | TimeoutException e) {
63 throw new IllegalStateException("getRemoteBuckets failed", e);
67 @SuppressWarnings({"unchecked", "rawtypes"})
68 final String bucketVersions() {
70 return Await.result((Future)bucketAccess.getBucketVersions(), timeout).toString();
71 } catch (InterruptedException | TimeoutException e) {
72 throw new IllegalStateException("getVersions failed", e);