Fix followerDistributedDataStore tear down
[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 org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
18 import org.opendaylight.controller.remote.rpc.registry.AbstractRoutingTable;
19 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
20 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Await;
24 import scala.concurrent.Future;
25 import scala.concurrent.duration.FiniteDuration;
26
27 abstract class AbstractRegistryMXBean<T extends AbstractRoutingTable<T, I>, I> extends AbstractMXBean {
28     static final String LOCAL_CONSTANT = "local";
29     static final String ROUTE_CONSTANT = "route:";
30     static final String NAME_CONSTANT = " | name:";
31
32     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
33     protected final Logger log = LoggerFactory.getLogger(getClass());
34
35     private final BucketStoreAccess bucketAccess;
36     private final FiniteDuration timeout;
37
38     AbstractRegistryMXBean(final @NonNull String beanName, final @NonNull String beanType,
39             final @NonNull BucketStoreAccess bucketAccess, final @NonNull Timeout timeout) {
40         super(beanName, beanType, null);
41         this.bucketAccess = requireNonNull(bucketAccess);
42         this.timeout = timeout.duration();
43         registerMBean();
44     }
45
46     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
47     final T localData() {
48         try {
49             return (T) Await.result((Future) bucketAccess.getLocalData(), timeout);
50         } catch (Exception e) {
51             throw new RuntimeException("getLocalData failed", e);
52         }
53     }
54
55     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
56     final Map<Address, Bucket<T>> remoteBuckets() {
57         try {
58             return (Map<Address, Bucket<T>>) Await.result((Future)bucketAccess.getRemoteBuckets(), timeout);
59         } catch (Exception e) {
60             throw new RuntimeException("getRemoteBuckets failed", e);
61         }
62     }
63
64     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
65     final String bucketVersions() {
66         try {
67             return Await.result((Future)bucketAccess.getBucketVersions(), timeout).toString();
68         } catch (Exception e) {
69             throw new RuntimeException("getVersions failed", e);
70         }
71     }
72 }