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 org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
13 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
14 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
15 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
16 import org.opendaylight.controller.sal.connector.api.RpcRouter;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.util.HashMap;
21 import java.util.HashSet;
26 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
28 protected final Logger log = LoggerFactory.getLogger(getClass());
30 private final String NULL_CONSTANT = "null";
32 private final String LOCAL_CONSTANT = "local";
34 private final String ROUTE_CONSTANT = "route:";
36 private 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.getLocalBucket().getData();
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);
55 if(log.isDebugEnabled()) {
56 log.debug("Locally registered global RPCs {}", globalRpc);
62 public Set<String> getLocalRegisteredRoutedRpc() {
63 RoutingTable table = rpcRegistry.getLocalBucket().getData();
64 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
65 for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
66 if(route.getRoute() != null) {
67 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
68 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null ?
69 route.getType().toString() : NULL_CONSTANT);
70 routedRpc.add(builder.toString());
73 if(log.isDebugEnabled()) {
74 log.debug("Locally registered routed RPCs {}", routedRpc);
80 public Map<String, String> findRpcByName(final String name) {
81 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
82 // Get all RPCs from local bucket
83 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
85 // Get all RPCs from remote bucket
86 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
87 for(Address address : buckets.keySet()) {
88 RoutingTable table = buckets.get(address).getData();
89 rpcMap.putAll(getRpcMemberMapByName(table, name, address.toString()));
91 if(log.isDebugEnabled()) {
92 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
98 public Map<String, String> findRpcByRoute(String routeId) {
99 RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
100 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
102 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
103 for(Address address : buckets.keySet()) {
104 RoutingTable table = buckets.get(address).getData();
105 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, address.toString()));
108 if(log.isDebugEnabled()) {
109 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
115 * Search if the routing table route String contains routeName
118 private Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
119 final String address) {
120 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
121 Map<String, String> rpcMap = new HashMap<>(routes.size());
122 for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
123 if(route.getRoute() != null) {
124 String routeString = route.getRoute().toString();
125 if(routeString.contains(routeName)) {
126 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
127 builder.append(routeString).append(NAME_CONSTANT).append(route.getType() != null ?
128 route.getType().toString() : NULL_CONSTANT);
129 rpcMap.put(builder.toString(), address);
137 * Search if the routing table route type contains name
139 private Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
140 final String address) {
141 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
142 Map<String, String> rpcMap = new HashMap<>(routes.size());
143 for(RpcRouter.RouteIdentifier<?, ?, ?> route : routes){
144 if(route.getType() != null) {
145 String type = route.getType().toString();
146 if(type.contains(name)) {
147 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
148 builder.append(route.getRoute() != null ? route.getRoute().toString(): NULL_CONSTANT)
149 .append(NAME_CONSTANT).append(type);
150 rpcMap.put(builder.toString(), address);
160 public String getBucketVersions() {
161 return rpcRegistry.getVersions().toString();