Fixup checkstyle
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / mbeans / AbstractRegistryMXBean.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.remote.rpc.registry.mbeans;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.Address;
13 import akka.util.Timeout;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Map;
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;
27
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:";
32
33     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
34     protected final Logger log = LoggerFactory.getLogger(getClass());
35
36     private final BucketStoreAccess bucketAccess;
37     private final FiniteDuration timeout;
38
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();
46         registerMBean();
47     }
48
49     @SuppressWarnings({"unchecked", "rawtypes"})
50     final T localData() {
51         try {
52             return (T) Await.result((Future) bucketAccess.getLocalData(), timeout);
53         } catch (InterruptedException | TimeoutException e) {
54             throw new IllegalStateException("getLocalData failed", e);
55         }
56     }
57
58     @SuppressWarnings({"unchecked", "rawtypes"})
59     final Map<Address, Bucket<T>> remoteBuckets() {
60         try {
61             return (Map<Address, Bucket<T>>) Await.result((Future)bucketAccess.getRemoteBuckets(), timeout);
62         } catch (InterruptedException | TimeoutException e) {
63             throw new IllegalStateException("getRemoteBuckets failed", e);
64         }
65     }
66
67     @SuppressWarnings({"unchecked", "rawtypes"})
68     final String bucketVersions() {
69         try {
70             return Await.result((Future)bucketAccess.getBucketVersions(), timeout).toString();
71         } catch (InterruptedException | TimeoutException e) {
72             throw new IllegalStateException("getVersions failed", e);
73         }
74     }
75 }