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 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
65 builder.append(route.getContextReference().toString()).append(NAME_CONSTANT).append(route.getType());
66 routedRpc.add(builder.toString());
70 log.debug("Locally registered routed RPCs {}", routedRpc);
75 public Map<String, String> findRpcByName(final String name) {
76 RoutingTable localTable = rpcRegistry.getLocalData();
77 // Get all RPCs from local bucket
78 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
80 // Get all RPCs from remote bucket
81 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
82 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
83 RoutingTable table = entry.getValue().getData();
84 rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
87 log.debug("list of RPCs {} searched by name {}", rpcMap, name);
92 public Map<String, String> findRpcByRoute(final String routeId) {
93 RoutingTable localTable = rpcRegistry.getLocalData();
94 Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
96 Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
97 for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
98 RoutingTable table = entry.getValue().getData();
99 rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
102 log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
107 * Search if the routing table route String contains routeName.
109 private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
110 final String address) {
111 Set<DOMRpcIdentifier> routes = table.getRoutes();
112 Map<String, String> rpcMap = new HashMap<>(routes.size());
113 for (DOMRpcIdentifier route : routes) {
114 if (!route.getContextReference().isEmpty()) {
115 String routeString = route.getContextReference().toString();
116 if (routeString.contains(routeName)) {
117 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
118 builder.append(routeString).append(NAME_CONSTANT).append(route.getType());
119 rpcMap.put(builder.toString(), address);
127 * Search if the routing table route type contains name.
129 private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
130 final String address) {
131 Set<DOMRpcIdentifier> routes = table.getRoutes();
132 Map<String, String> rpcMap = new HashMap<>(routes.size());
133 for (DOMRpcIdentifier route : routes) {
134 if (!route.getContextReference().isEmpty()) {
135 String type = route.getType().toString();
136 if (type.contains(name)) {
137 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
138 builder.append(route.getContextReference()).append(NAME_CONSTANT).append(type);
139 rpcMap.put(builder.toString(), address);
147 public String getBucketVersions() {
148 return rpcRegistry.getVersions().toString();