Fix intermittent RemoteRpcRegistryMXBeanImplTest failures
[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 akka.util.Timeout;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
22 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.Await;
26 import scala.concurrent.Future;
27
28
29 public class RemoteRpcRegistryMXBeanImpl extends AbstractMXBean implements RemoteRpcRegistryMXBean {
30
31     protected final Logger log = LoggerFactory.getLogger(getClass());
32
33     private static final String LOCAL_CONSTANT = "local";
34
35     private static final String ROUTE_CONSTANT = "route:";
36
37     private static final String NAME_CONSTANT = " | name:";
38
39     private final BucketStoreAccess rpcRegistryAccess;
40     private final Timeout timeout;
41
42     public RemoteRpcRegistryMXBeanImpl(final BucketStoreAccess rpcRegistryAccess, Timeout timeout) {
43         super("RemoteRpcRegistry", "RemoteRpcBroker", null);
44         this.rpcRegistryAccess = rpcRegistryAccess;
45         this.timeout = timeout;
46         registerMBean();
47     }
48
49     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
50     private RoutingTable getLocalData() {
51         try {
52             return (RoutingTable) Await.result((Future) rpcRegistryAccess.getLocalData(), timeout.duration());
53         } catch (Exception e) {
54             throw new RuntimeException("getLocalData failed", e);
55         }
56     }
57
58     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
59     private Map<Address, Bucket<RoutingTable>> getRemoteBuckets() {
60         try {
61             return (Map<Address, Bucket<RoutingTable>>) Await.result((Future)rpcRegistryAccess.getRemoteBuckets(),
62                     timeout.duration());
63         } catch (Exception e) {
64             throw new RuntimeException("getRemoteBuckets failed", e);
65         }
66     }
67
68     @Override
69     public Set<String> getGlobalRpc() {
70         RoutingTable table = getLocalData();
71         Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
72         for (DOMRpcIdentifier route : table.getRoutes()) {
73             if (route.getContextReference().isEmpty()) {
74                 globalRpc.add(route.getType().toString());
75             }
76         }
77
78         log.debug("Locally registered global RPCs {}", globalRpc);
79         return globalRpc;
80     }
81
82     @Override
83     public Set<String> getLocalRegisteredRoutedRpc() {
84         RoutingTable table = getLocalData();
85         Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
86         for (DOMRpcIdentifier route : table.getRoutes()) {
87             if (!route.getContextReference().isEmpty()) {
88                 routedRpc.add(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + route.getType());
89             }
90         }
91
92         log.debug("Locally registered routed RPCs {}", routedRpc);
93         return routedRpc;
94     }
95
96     @Override
97     public Map<String, String> findRpcByName(final String name) {
98         RoutingTable localTable = getLocalData();
99         // Get all RPCs from local bucket
100         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
101
102         // Get all RPCs from remote bucket
103         Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
104         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
105             RoutingTable table = entry.getValue().getData();
106             rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
107         }
108
109         log.debug("list of RPCs {} searched by name {}", rpcMap, name);
110         return rpcMap;
111     }
112
113     @Override
114     public Map<String, String> findRpcByRoute(final String routeId) {
115         RoutingTable localTable = getLocalData();
116         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
117
118         Map<Address, Bucket<RoutingTable>> buckets = getRemoteBuckets();
119         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
120             RoutingTable table = entry.getValue().getData();
121             rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
122         }
123
124         log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
125         return rpcMap;
126     }
127
128     /**
129      * Search if the routing table route String contains routeName.
130      */
131     private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
132                                                       final String address) {
133         Set<DOMRpcIdentifier> routes = table.getRoutes();
134         Map<String, String> rpcMap = new HashMap<>(routes.size());
135         for (DOMRpcIdentifier route : routes) {
136             if (!route.getContextReference().isEmpty()) {
137                 String routeString = route.getContextReference().toString();
138                 if (routeString.contains(routeName)) {
139                     rpcMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
140                 }
141             }
142         }
143         return rpcMap;
144     }
145
146     /**
147      * Search if the routing table route type contains name.
148      */
149     private static Map<String, String>  getRpcMemberMapByName(final RoutingTable table, final String name,
150                                                        final String address) {
151         Set<DOMRpcIdentifier> routes = table.getRoutes();
152         Map<String, String> rpcMap = new HashMap<>(routes.size());
153         for (DOMRpcIdentifier route : routes) {
154             if (!route.getContextReference().isEmpty()) {
155                 String type = route.getType().toString();
156                 if (type.contains(name)) {
157                     rpcMap.put(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + type, address);
158                 }
159             }
160         }
161         return rpcMap;
162     }
163
164     @Override
165     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch", "rawtypes"})
166     public String getBucketVersions() {
167         try {
168             return Await.result((Future)rpcRegistryAccess.getBucketVersions(), timeout.duration()).toString();
169         } catch (Exception e) {
170             throw new RuntimeException("getVersions failed", e);
171         }
172     }
173 }