d5c72fcea395f0009602ea56c9923e9f053e7d8e
[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 package org.opendaylight.controller.remote.rpc.registry.mbeans;
9
10 import akka.actor.Address;
11 import akka.util.Timeout;
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.remote.rpc.registry.RoutingTable;
18 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
19 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
20 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
21
22 public class RemoteRpcRegistryMXBeanImpl extends AbstractRegistryMXBean<RoutingTable, DOMRpcIdentifier>
23         implements RemoteRpcRegistryMXBean {
24     public RemoteRpcRegistryMXBeanImpl(final BucketStoreAccess rpcRegistryAccess, final Timeout timeout) {
25         super("RemoteRpcRegistry", "RemoteRpcBroker", rpcRegistryAccess, timeout);
26     }
27
28     @Override
29     public Set<String> getGlobalRpc() {
30         RoutingTable table = localData();
31         Set<String> globalRpc = new HashSet<>(table.getItems().size());
32         for (DOMRpcIdentifier route : table.getItems()) {
33             if (route.getContextReference().isEmpty()) {
34                 globalRpc.add(route.getType().toString());
35             }
36         }
37
38         log.debug("Locally registered global RPCs {}", globalRpc);
39         return globalRpc;
40     }
41
42     @Override
43     public Set<String> getLocalRegisteredRoutedRpc() {
44         RoutingTable table = localData();
45         Set<String> routedRpc = new HashSet<>(table.getItems().size());
46         for (DOMRpcIdentifier route : table.getItems()) {
47             if (!route.getContextReference().isEmpty()) {
48                 routedRpc.add(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + route.getType());
49             }
50         }
51
52         log.debug("Locally registered routed RPCs {}", routedRpc);
53         return routedRpc;
54     }
55
56     @Override
57     public Map<String, String> findRpcByName(final String name) {
58         RoutingTable localTable = localData();
59         // Get all RPCs from local bucket
60         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
61
62         // Get all RPCs from remote bucket
63         Map<Address, Bucket<RoutingTable>> buckets = remoteBuckets();
64         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
65             RoutingTable table = entry.getValue().getData();
66             rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
67         }
68
69         log.debug("list of RPCs {} searched by name {}", rpcMap, name);
70         return rpcMap;
71     }
72
73     @Override
74     public Map<String, String> findRpcByRoute(final String routeId) {
75         RoutingTable localTable = localData();
76         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
77
78         Map<Address, Bucket<RoutingTable>> buckets = remoteBuckets();
79         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
80             RoutingTable table = entry.getValue().getData();
81             rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
82         }
83
84         log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
85         return rpcMap;
86     }
87
88     /**
89      * Search if the routing table route String contains routeName.
90      */
91     private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
92                                                              final String address) {
93         Set<DOMRpcIdentifier> routes = table.getItems();
94         Map<String, String> rpcMap = new HashMap<>(routes.size());
95         for (DOMRpcIdentifier route : routes) {
96             if (!route.getContextReference().isEmpty()) {
97                 String routeString = route.getContextReference().toString();
98                 if (routeString.contains(routeName)) {
99                     rpcMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
100                 }
101             }
102         }
103         return rpcMap;
104     }
105
106     /**
107      * Search if the routing table route type contains name.
108      */
109     private static Map<String, String> getRpcMemberMapByName(final RoutingTable table, final String name,
110                                                              final String address) {
111         Set<DOMRpcIdentifier> routes = table.getItems();
112         Map<String, String> rpcMap = new HashMap<>(routes.size());
113         for (DOMRpcIdentifier route : routes) {
114             if (!route.getContextReference().isEmpty()) {
115                 String type = route.getType().toString();
116                 if (type.contains(name)) {
117                     rpcMap.put(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + type, address);
118                 }
119             }
120         }
121         return rpcMap;
122     }
123
124     @Override
125     public String getBucketVersions() {
126         return bucketVersions();
127     }
128 }