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.md.sal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
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 LOCAL_CONSTANT = "local";
32 private static final String ROUTE_CONSTANT = "route:";
34 private static final String NAME_CONSTANT = " | name:";
36 private final RpcRegistry rpcRegistry;
38 public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
39 super("RemoteRpcRegistry", "RemoteRpcBroker", null);
40 this.rpcRegistry = rpcRegistry;
45 public Set<String> getGlobalRpc() {
46 RoutingTable table = rpcRegistry.getLocalData();
47 Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
48 for (DOMRpcIdentifier route : table.getRoutes()) {
49 if (route.getContextReference().isEmpty()) {
50 globalRpc.add(route.getType().toString());
54 log.debug("Locally registered global RPCs {}", globalRpc);
59 public Set<String> getLocalRegisteredRoutedRpc() {
60 RoutingTable table = rpcRegistry.getLocalData();
61 Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
62 for (DOMRpcIdentifier route : table.getRoutes()) {
63 if (!route.getContextReference().isEmpty()) {
64 routedRpc.add(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + route.getType());
68 log.debug("Locally registered routed RPCs {}", routedRpc);
73 public Map<String, String> findRpcByName(final String name) {
74 RoutingTable localTable = rpcRegistry.getLocalData();
75 // Get all RPCs from local bucket
76 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
78 // Get all RPCs from remote bucket
79 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
80 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
81 RoutingTable table = entry.getValue().getData();
82 rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
85 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
90 public Map<String, String> findRpcByRoute(final String routeId) {
91 RoutingTable localTable = rpcRegistry.getLocalData();
92 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
94 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
95 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
96 RoutingTable table = entry.getValue().getData();
97 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
100 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
105 * Search if the routing table route String contains routeName.
107 private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
108 final String address) {
109 Set<DOMRpcIdentifier> routes = table.getRoutes();
110 Map<String, String> rpcMap = new HashMap<>(routes.size());
111 for (DOMRpcIdentifier route : routes) {
112 if (!route.getContextReference().isEmpty()) {
113 String routeString = route.getContextReference().toString();
114 if (routeString.contains(routeName)) {
115 rpcMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
123 * Search if the routing table route type contains name.
125 private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
126 final String address) {
127 Set<DOMRpcIdentifier> routes = table.getRoutes();
128 Map<String, String> rpcMap = new HashMap<>(routes.size());
129 for (DOMRpcIdentifier route : routes) {
130 if (!route.getContextReference().isEmpty()) {
131 String type = route.getType().toString();
132 if (type.contains(name)) {
133 rpcMap.put(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + type, address);
141 public String getBucketVersions() {
142 return rpcRegistry.getVersions().toString();