1 package org.opendaylight.controller.remote.rpc.registry.mbeans;
3 import akka.actor.Address;
4 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
5 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
6 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
7 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
8 import org.opendaylight.controller.sal.connector.api.RpcRouter;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import java.util.HashMap;
13 import java.util.HashSet;
18 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
20 protected final Logger log = LoggerFactory.getLogger(getClass());
22 private final String NULL_CONSTANT = "null";
24 private final String LOCAL_CONSTANT = "local";
26 private final String ROUTE_CONSTANT = "route:";
28 private final String NAME_CONSTANT = " | name:";
30 private final RpcRegistry rpcRegistry;
32 public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
33 super("RemoteRpcRegistry", "RemoteRpcBroker", null);
34 this.rpcRegistry = rpcRegistry;
39 public Set<String> getGlobalRpc() {
40 RoutingTable table = rpcRegistry.getLocalBucket().getData();
41 Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
42 for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
43 if(route.getRoute() == null) {
44 globalRpc.add(route.getType() != null ? route.getType().toString() : NULL_CONSTANT);
47 if(log.isDebugEnabled()) {
48 log.debug("Locally registered global RPCs {}", globalRpc);
54 public Set<String> getLocalRegisteredRoutedRpc() {
55 RoutingTable table = rpcRegistry.getLocalBucket().getData();
56 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
57 for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
58 if(route.getRoute() != null) {
59 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
60 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null ?
61 route.getType().toString() : NULL_CONSTANT);
62 routedRpc.add(builder.toString());
65 if(log.isDebugEnabled()) {
66 log.debug("Locally registered routed RPCs {}", routedRpc);
72 public Map<String, String> findRpcByName(final String name) {
73 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
74 // Get all RPCs from local bucket
75 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
77 // Get all RPCs from remote bucket
78 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
79 for(Address address : buckets.keySet()) {
80 RoutingTable table = buckets.get(address).getData();
81 rpcMap.putAll(getRpcMemberMapByName(table, name, address.toString()));
83 if(log.isDebugEnabled()) {
84 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
90 public Map<String, String> findRpcByRoute(String routeId) {
91 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
92 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
94 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
95 for(Address address : buckets.keySet()) {
96 RoutingTable table = buckets.get(address).getData();
97 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, address.toString()));
100 if(log.isDebugEnabled()) {
101 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
107 * Search if the routing table route String contains routeName
110 private Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
111 final String address) {
112 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
113 Map<String, String> rpcMap = new HashMap<>(routes.size());
114 for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
115 if(route.getRoute() != null) {
116 String routeString = route.getRoute().toString();
117 if(routeString.contains(routeName)) {
118 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
119 builder.append(routeString).append(NAME_CONSTANT).append(route.getType() != null ?
120 route.getType().toString() : NULL_CONSTANT);
121 rpcMap.put(builder.toString(), address);
129 * Search if the routing table route type contains name
131 private Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
132 final String address) {
133 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
134 Map<String, String> rpcMap = new HashMap<>(routes.size());
135 for(RpcRouter.RouteIdentifier<?, ?, ?> route : routes){
136 if(route.getType() != null) {
137 String type = route.getType().toString();
138 if(type.contains(name)) {
139 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
140 builder.append(route.getRoute() != null ? route.getRoute().toString(): NULL_CONSTANT)
141 .append(NAME_CONSTANT).append(type);
142 rpcMap.put(builder.toString(), address);
152 public String getBucketVersions() {
153 return rpcRegistry.getVersions().toString();