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 java.util.HashMap;
13 import java.util.HashSet;
15 import java.util.Map.Entry;
17 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
18 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
20 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
28 protected final Logger log = LoggerFactory.getLogger(getClass());
30 private static final String NULL_CONSTANT = "null";
32 private static final String LOCAL_CONSTANT = "local";
34 private static final String ROUTE_CONSTANT = "route:";
36 private static final String NAME_CONSTANT = " | name:";
38 private final RpcRegistry rpcRegistry;
40 public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
41 super("RemoteRpcRegistry", "RemoteRpcBroker", null);
42 this.rpcRegistry = rpcRegistry;
47 public Set<String> getGlobalRpc() {
48 RoutingTable table = rpcRegistry.getLocalData();
49 Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
50 for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
51 if (route.getRoute() == null) {
52 globalRpc.add(route.getType() != null ? route.getType().toString() : NULL_CONSTANT);
56 log.debug("Locally registered global RPCs {}", globalRpc);
61 public Set<String> getLocalRegisteredRoutedRpc() {
62 RoutingTable table = rpcRegistry.getLocalData();
63 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
64 for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
65 if (route.getRoute() != null) {
66 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
67 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null
68 ? route.getType().toString() : NULL_CONSTANT);
69 routedRpc.add(builder.toString());
73 log.debug("Locally registered routed RPCs {}", routedRpc);
78 public Map<String, String> findRpcByName(final String name) {
79 RoutingTable localTable = rpcRegistry.getLocalData();
80 // Get all RPCs from local bucket
81 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
83 // Get all RPCs from remote bucket
84 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
85 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
86 RoutingTable table = entry.getValue().getData();
87 rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
90 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
95 public Map<String, String> findRpcByRoute(final String routeId) {
96 RoutingTable localTable = rpcRegistry.getLocalData();
97 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
99 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
100 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
101 RoutingTable table = entry.getValue().getData();
102 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
105 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
110 * Search if the routing table route String contains routeName.
112 private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
113 final String address) {
114 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
115 Map<String, String> rpcMap = new HashMap<>(routes.size());
116 for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
117 if (route.getRoute() != null) {
118 String routeString = route.getRoute().toString();
119 if (routeString.contains(routeName)) {
120 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
121 builder.append(routeString).append(NAME_CONSTANT).append(route.getType() != null
122 ? route.getType().toString() : NULL_CONSTANT);
123 rpcMap.put(builder.toString(), address);
131 * Search if the routing table route type contains name.
133 private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
134 final String address) {
135 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
136 Map<String, String> rpcMap = new HashMap<>(routes.size());
137 for (RpcRouter.RouteIdentifier<?, ?, ?> route : routes) {
138 if (route.getType() != null) {
139 String type = route.getType().toString();
140 if (type.contains(name)) {
141 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
142 builder.append(route.getRoute() != null ? route.getRoute().toString() : NULL_CONSTANT)
143 .append(NAME_CONSTANT).append(type);
144 rpcMap.put(builder.toString(), address);
152 public String getBucketVersions() {
153 return rpcRegistry.getVersions().toString();