684bb158ab231c09159a7347ffe18afbb3fcd1b6
[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 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;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25
26 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
27
28     protected final Logger log = LoggerFactory.getLogger(getClass());
29
30     private final String NULL_CONSTANT = "null";
31
32     private final String LOCAL_CONSTANT = "local";
33
34     private final String ROUTE_CONSTANT = "route:";
35
36     private final String NAME_CONSTANT = " | name:";
37
38     private final RpcRegistry rpcRegistry;
39
40     public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
41         super("RemoteRpcRegistry", "RemoteRpcBroker", null);
42         this.rpcRegistry = rpcRegistry;
43         registerMBean();
44     }
45
46     @Override
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);
53             }
54         }
55         if(log.isDebugEnabled()) {
56             log.debug("Locally registered global RPCs {}", globalRpc);
57         }
58         return globalRpc;
59     }
60
61     @Override
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());
71             }
72         }
73         if(log.isDebugEnabled()) {
74             log.debug("Locally registered routed RPCs {}", routedRpc);
75         }
76         return routedRpc;
77     }
78
79     @Override
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));
84
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()));
90         }
91         if(log.isDebugEnabled()) {
92             log.debug("list of RPCs {} searched by name {}", rpcMap, name);
93         }
94         return rpcMap;
95     }
96
97     @Override
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));
101
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()));
106
107         }
108         if(log.isDebugEnabled()) {
109             log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
110         }
111         return rpcMap;
112     }
113
114     /**
115      * Search if the routing table route String contains routeName
116      */
117
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);
130                 }
131             }
132         }
133         return rpcMap;
134     }
135
136     /**
137      * Search if the routing table route type contains name
138      */
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);
151                 }
152             }
153         }
154         return rpcMap;
155     }
156
157
158
159     @Override
160     public String getBucketVersions() {
161         return rpcRegistry.getVersions().toString();
162     }
163
164 }