Merge changes Ibe016728,Ic719c519,I87a81a54
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / mbeans / RemoteRpcRegistryMXBeanImpl.java
1 package org.opendaylight.controller.remote.rpc.registry.mbeans;
2
3 import akka.actor.Address;
4 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
5 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
6 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
7 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
8 import org.opendaylight.controller.sal.connector.api.RpcRouter;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16
17
18 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
19
20     protected final Logger log = LoggerFactory.getLogger(getClass());
21
22     private final String NULL_CONSTANT = "null";
23
24     private final String LOCAL_CONSTANT = "local";
25
26     private final String ROUTE_CONSTANT = "route:";
27
28     private final String NAME_CONSTANT = " | name:";
29
30     private final RpcRegistry rpcRegistry;
31
32     public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
33         super("RemoteRpcRegistry", "RemoteRpcBroker", null);
34         this.rpcRegistry = rpcRegistry;
35         registerMBean();
36     }
37
38     @Override
39     public Set<String> getGlobalRpc() {
40         RoutingTable table = rpcRegistry.getLocalBucket().getData();
41         Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
42         for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
43             if(route.getRoute() == null) {
44                 globalRpc.add(route.getType() != null ? route.getType().toString() : NULL_CONSTANT);
45             }
46         }
47         if(log.isDebugEnabled()) {
48             log.debug("Locally registered global RPCs {}", globalRpc);
49         }
50         return globalRpc;
51     }
52
53     @Override
54     public Set<String> getLocalRegisteredRoutedRpc() {
55         RoutingTable table = rpcRegistry.getLocalBucket().getData();
56         Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
57         for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
58             if(route.getRoute() != null) {
59                 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
60                 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null ?
61                     route.getType().toString() : NULL_CONSTANT);
62                 routedRpc.add(builder.toString());
63             }
64         }
65         if(log.isDebugEnabled()) {
66             log.debug("Locally registered routed RPCs {}", routedRpc);
67         }
68         return routedRpc;
69     }
70
71     @Override
72     public Map<String, String> findRpcByName(final String name) {
73         RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
74         // Get all RPCs from local bucket
75         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
76
77         // Get all RPCs from remote bucket
78         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
79         for(Address address : buckets.keySet()) {
80             RoutingTable table = buckets.get(address).getData();
81             rpcMap.putAll(getRpcMemberMapByName(table, name, address.toString()));
82         }
83         if(log.isDebugEnabled()) {
84             log.debug("list of RPCs {} searched by name {}", rpcMap, name);
85         }
86         return rpcMap;
87     }
88
89     @Override
90     public Map<String, String> findRpcByRoute(String routeId) {
91         RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
92         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
93
94         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
95         for(Address address : buckets.keySet()) {
96             RoutingTable table = buckets.get(address).getData();
97             rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, address.toString()));
98
99         }
100         if(log.isDebugEnabled()) {
101             log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
102         }
103         return rpcMap;
104     }
105
106     /**
107      * Search if the routing table route String contains routeName
108      */
109
110     private Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
111                                                       final String address) {
112         Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
113         Map<String, String> rpcMap = new HashMap<>(routes.size());
114         for(RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()){
115             if(route.getRoute() != null) {
116                 String routeString = route.getRoute().toString();
117                 if(routeString.contains(routeName)) {
118                     StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
119                     builder.append(routeString).append(NAME_CONSTANT).append(route.getType() != null ?
120                         route.getType().toString() : NULL_CONSTANT);
121                     rpcMap.put(builder.toString(), address);
122                 }
123             }
124         }
125         return rpcMap;
126     }
127
128     /**
129      * Search if the routing table route type contains name
130      */
131     private Map<String, String>  getRpcMemberMapByName(final RoutingTable table, final String name,
132                                                        final String address) {
133         Set<RpcRouter.RouteIdentifier<?, ?, ?>> routes = table.getRoutes();
134         Map<String, String> rpcMap = new HashMap<>(routes.size());
135         for(RpcRouter.RouteIdentifier<?, ?, ?> route : routes){
136             if(route.getType() != null) {
137                 String type = route.getType().toString();
138                 if(type.contains(name)) {
139                     StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
140                     builder.append(route.getRoute() != null ? route.getRoute().toString(): NULL_CONSTANT)
141                         .append(NAME_CONSTANT).append(type);
142                     rpcMap.put(builder.toString(), address);
143                 }
144             }
145         }
146         return rpcMap;
147     }
148
149
150
151     @Override
152     public String getBucketVersions() {
153         return rpcRegistry.getVersions().toString();
154     }
155
156 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.