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.md.sal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
22 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
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 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
89 builder.append(route.getContextReference().toString()).append(NAME_CONSTANT).append(route.getType());
90 routedRpc.add(builder.toString());
94 log.debug("Locally registered routed RPCs {}", routedRpc);
99 public Map<String, String> findRpcByName(final String name) {
100 RoutingTable localTable = getLocalData();
101 // Get all RPCs from local bucket
102 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
104 // Get all RPCs from remote bucket
105 Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
106 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
107 RoutingTable table = entry.getValue().getData();
108 rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
111 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
116 public Map<String, String> findRpcByRoute(final String routeId) {
117 RoutingTable localTable = getLocalData();
118 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
120 Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
121 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
122 RoutingTable table = entry.getValue().getData();
123 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
126 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
131 * Search if the routing table route String contains routeName.
133 private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
134 final String address) {
135 Set<DOMRpcIdentifier> routes = table.getRoutes();
136 Map<String, String> rpcMap = new HashMap<>(routes.size());
137 for (DOMRpcIdentifier route : routes) {
138 if (!route.getContextReference().isEmpty()) {
139 String routeString = route.getContextReference().toString();
140 if (routeString.contains(routeName)) {
141 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
142 builder.append(routeString).append(NAME_CONSTANT).append(route.getType());
143 rpcMap.put(builder.toString(), address);
151 * Search if the routing table route type contains name.
153 private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
154 final String address) {
155 Set<DOMRpcIdentifier> routes = table.getRoutes();
156 Map<String, String> rpcMap = new HashMap<>(routes.size());
157 for (DOMRpcIdentifier route : routes) {
158 if (!route.getContextReference().isEmpty()) {
159 String type = route.getType().toString();
160 if (type.contains(name)) {
161 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
162 builder.append(route.getContextReference()).append(NAME_CONSTANT).append(type);
163 rpcMap.put(builder.toString(), address);
171 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
172 public String getBucketVersions() {
174 return Await.result((Future)rpcRegistryAccess.getBucketVersions(), timeout.duration()).toString();
175 } catch (Exception e) {
176 throw new RuntimeException("getVersions failed", e);