2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.remote.rpc.registry.mbeans;
11 import akka.actor.Address;
12 import akka.util.Timeout;
13 import java.util.HashMap;
14 import java.util.HashSet;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
20 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
22 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.Await;
26 import scala.concurrent.Future;
29 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
31 protected final Logger log = LoggerFactory.getLogger(getClass());
33 private static final String LOCAL_CONSTANT = "local";
35 private static final String ROUTE_CONSTANT = "route:";
37 private static final String NAME_CONSTANT = " | name:";
39 private final BucketStoreAccess rpcRegistryAccess;
40 private final Timeout timeout;
42 public RemoteRpcRegistryMXBeanImpl(final BucketStoreAccess rpcRegistryAccess, Timeout timeout) {
43 super("RemoteRpcRegistry", "RemoteRpcBroker", null);
44 this.rpcRegistryAccess = rpcRegistryAccess;
45 this.timeout = timeout;
49 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
50 private RoutingTable getLocalData() {
52 return (RoutingTable) Await.result((Future) rpcRegistryAccess.getLocalData(), timeout.duration());
53 } catch (Exception e) {
54 throw new RuntimeException("getLocalData failed", e);
58 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
59 private Map<Address, Bucket<RoutingTable>> getRemoteBuckets() {
61 return (Map<Address, Bucket<RoutingTable>>) Await.result((Future)rpcRegistryAccess.getRemoteBuckets(),
63 } catch (Exception e) {
64 throw new RuntimeException("getRemoteBuckets failed", e);
69 public Set<String> getGlobalRpc() {
70 RoutingTable table = getLocalData();
71 Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
72 for (DOMRpcIdentifier route : table.getRoutes()) {
73 if (route.getContextReference().isEmpty()) {
74 globalRpc.add(route.getType().toString());
78 log.debug("Locally registered global RPCs {}", globalRpc);
83 public Set<String> getLocalRegisteredRoutedRpc() {
84 RoutingTable table = getLocalData();
85 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
86 for (DOMRpcIdentifier route : table.getRoutes()) {
87 if (!route.getContextReference().isEmpty()) {
88 routedRpc.add(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + route.getType());
92 log.debug("Locally registered routed RPCs {}", routedRpc);
97 public Map<String, String> findRpcByName(final String name) {
98 RoutingTable localTable = getLocalData();
99 // Get all RPCs from local bucket
100 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
102 // Get all RPCs from remote bucket
103 Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
104 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
105 RoutingTable table = entry.getValue().getData();
106 rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
109 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
114 public Map<String, String> findRpcByRoute(final String routeId) {
115 RoutingTable localTable = getLocalData();
116 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
118 Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
119 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
120 RoutingTable table = entry.getValue().getData();
121 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
124 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
129 * Search if the routing table route String contains routeName.
131 private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
132 final String address) {
133 Set<DOMRpcIdentifier> routes = table.getRoutes();
134 Map<String, String> rpcMap = new HashMap<>(routes.size());
135 for (DOMRpcIdentifier route : routes) {
136 if (!route.getContextReference().isEmpty()) {
137 String routeString = route.getContextReference().toString();
138 if (routeString.contains(routeName)) {
139 rpcMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
147 * Search if the routing table route type contains name.
149 private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
150 final String address) {
151 Set<DOMRpcIdentifier> routes = table.getRoutes();
152 Map<String, String> rpcMap = new HashMap<>(routes.size());
153 for (DOMRpcIdentifier route : routes) {
154 if (!route.getContextReference().isEmpty()) {
155 String type = route.getType().toString();
156 if (type.contains(name)) {
157 rpcMap.put(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + type, address);
165 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
166 public String getBucketVersions() {
168 return Await.result((Future)rpcRegistryAccess.getBucketVersions(), timeout.duration()).toString();
169 } catch (Exception e) {
170 throw new RuntimeException("getVersions failed", e);