eabb170db244ce260a65dcb8e2dde86eb304be21
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / mbeans / RemoteRpcRegistryMXBeanImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.remote.rpc.registry.mbeans;
10
11 import akka.actor.Address;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
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;
24
25
26 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
27
28     protected final Logger log = LoggerFactory.getLogger(getClass());
29
30     private static final String LOCAL_CONSTANT = "local";
31
32     private static final String ROUTE_CONSTANT = "route:";
33
34     private static final String NAME_CONSTANT = " | name:";
35
36     private final RpcRegistry rpcRegistry;
37
38     public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
39         super("RemoteRpcRegistry", "RemoteRpcBroker", null);
40         this.rpcRegistry = rpcRegistry;
41         registerMBean();
42     }
43
44     @Override
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());
51             }
52         }
53
54         log.debug("Locally registered global RPCs {}", globalRpc);
55         return globalRpc;
56     }
57
58     @Override
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());
67             }
68         }
69
70         log.debug("Locally registered routed RPCs {}", routedRpc);
71         return routedRpc;
72     }
73
74     @Override
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));
79
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()));
85         }
86
87         log.debug("list of RPCs {} searched by name {}", rpcMap, name);
88         return rpcMap;
89     }
90
91     @Override
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));
95
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()));
100         }
101
102         log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
103         return rpcMap;
104     }
105
106     /**
107      * Search if the routing table route String contains routeName.
108      */
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);
120                 }
121             }
122         }
123         return rpcMap;
124     }
125
126     /**
127      * Search if the routing table route type contains name.
128      */
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);
140                 }
141             }
142         }
143         return rpcMap;
144     }
145
146     @Override
147     public String getBucketVersions() {
148         return rpcRegistry.getVersions().toString();
149     }
150 }