5fbd91cc81c349ad0244ac4b0d96c39f6ab4ebf7
[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.remote.rpc.registry.RoutingTable;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
20 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
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 NULL_CONSTANT = "null";
31
32     private static final String LOCAL_CONSTANT = "local";
33
34     private static final String ROUTE_CONSTANT = "route:";
35
36     private static 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
56         log.debug("Locally registered global RPCs {}", globalRpc);
57         return globalRpc;
58     }
59
60     @Override
61     public Set<String> getLocalRegisteredRoutedRpc() {
62         RoutingTable table = rpcRegistry.getLocalBucket().getData();
63         Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
64         for (RpcRouter.RouteIdentifier<?, ?, ?> route : table.getRoutes()) {
65             if (route.getRoute() != null) {
66                 StringBuilder builder = new StringBuilder(ROUTE_CONSTANT);
67                 builder.append(route.getRoute().toString()).append(NAME_CONSTANT).append(route.getType() != null
68                     ? route.getType().toString() : NULL_CONSTANT);
69                 routedRpc.add(builder.toString());
70             }
71         }
72
73         log.debug("Locally registered routed RPCs {}", routedRpc);
74         return routedRpc;
75     }
76
77     @Override
78     public Map<String, String> findRpcByName(final String name) {
79         RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
80         // Get all RPCs from local bucket
81         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
82
83         // Get all RPCs from remote bucket
84         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
85         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
86             RoutingTable table = entry.getValue().getData();
87             rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
88         }
89
90         log.debug("list of RPCs {} searched by name {}", rpcMap, name);
91         return rpcMap;
92     }
93
94     @Override
95     public Map<String, String> findRpcByRoute(String routeId) {
96         RoutingTable localTable = rpcRegistry.getLocalBucket().getData();
97         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
98
99         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
100         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
101             RoutingTable table = entry.getValue().getData();
102             rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
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 }