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;
16 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
17 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
18 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
19 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
20 import org.opendaylight.controller.sal.connector.api.RpcRouter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
27 protected final Logger log = LoggerFactory.getLogger(getClass());
29 private static final String NULL_CONSTANT = "null";
31 private static final String LOCAL_CONSTANT = "local";
33 private static final String ROUTE_CONSTANT = "route:";
35 private static final String NAME_CONSTANT = " | name:";
37 private final RpcRegistry rpcRegistry;
39 public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
40 super("RemoteRpcRegistry", "RemoteRpcBroker", null);
41 this.rpcRegistry = rpcRegistry;
46 public Set<String> getGlobalRpc() {
47 RoutingTable table = rpcRegistry.getLocalBucket().getData();
48 Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
49 for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
50 if (route.getRoute() == null) {
51 globalRpc.add(route.getType() != null ? route.getType().toString() : NULL_CONSTANT);
55 log.debug("Locally registered global RPCs {}", globalRpc);
60 public Set<String> getLocalRegisteredRoutedRpc() {
61 RoutingTable table = rpcRegistry.getLocalBucket().getData();
62 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
63 for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
64 if (route.getRoute() != null) {
65 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
66 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null
67 ? route.getType().toString() : NULL_CONSTANT);
68 routedRpc.add(builder.toString());
72 log.debug("Locally registered routed RPCs {}", routedRpc);
77 public Map<String, String> findRpcByName(final String name) {
78 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
79 // Get all RPCs from local bucket
80 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
82 // Get all RPCs from remote bucket
83 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
84 for (Address address : buckets.keySet()) {
85 RoutingTable table = buckets.get(address).getData();
86 rpcMap.putAll(getRpcMemberMapByName(table, name, address.toString()));
89 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
94 public Map<String, String> findRpcByRoute(String routeId) {
95 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
96 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
98 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
99 for (Address address : buckets.keySet()) {
100 RoutingTable table = buckets.get(address).getData();
101 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, address.toString()));
105 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
110 * Search if the routing table route String contains routeName.
112 private 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 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();