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