58abcac0d5b87ba0995c664d33ad06b1485005d6
[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.md.sal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.controller.remote.rpc.registry.RoutingTable;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.controller.remote.rpc.registry.gossip.Bucket;
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 LOCAL_CONSTANT = "local";
31
32     private static final String ROUTE_CONSTANT = "route:";
33
34     private static final String NAME_CONSTANT = " | name:";
35
36     private final RpcRegistry rpcRegistry;
37
38     public RemoteRpcRegistryMXBeanImpl(final RpcRegistry rpcRegistry) {
39         super("RemoteRpcRegistry", "RemoteRpcBroker", null);
40         this.rpcRegistry = rpcRegistry;
41         registerMBean();
42     }
43
44     @Override
45     public Set<String> getGlobalRpc() {
46         RoutingTable table = rpcRegistry.getLocalData();
47         Set<String> globalRpc = new HashSet<>(table.getRoutes().size());
48         for (DOMRpcIdentifier route : table.getRoutes()) {
49             if (route.getContextReference().isEmpty()) {
50                 globalRpc.add(route.getType().toString());
51             }
52         }
53
54         log.debug("Locally registered global RPCs {}", globalRpc);
55         return globalRpc;
56     }
57
58     @Override
59     public Set<String> getLocalRegisteredRoutedRpc() {
60         RoutingTable table = rpcRegistry.getLocalData();
61         Set<String> routedRpc = new HashSet<>(table.getRoutes().size());
62         for (DOMRpcIdentifier route : table.getRoutes()) {
63             if (!route.getContextReference().isEmpty()) {
64                 routedRpc.add(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + route.getType());
65             }
66         }
67
68         log.debug("Locally registered routed RPCs {}", routedRpc);
69         return routedRpc;
70     }
71
72     @Override
73     public Map<String, String> findRpcByName(final String name) {
74         RoutingTable localTable = rpcRegistry.getLocalData();
75         // Get all RPCs from local bucket
76         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByName(localTable, name, LOCAL_CONSTANT));
77
78         // Get all RPCs from remote bucket
79         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
80         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
81             RoutingTable table = entry.getValue().getData();
82             rpcMap.putAll(getRpcMemberMapByName(table, name, entry.getKey().toString()));
83         }
84
85         log.debug("list of RPCs {} searched by name {}", rpcMap, name);
86         return rpcMap;
87     }
88
89     @Override
90     public Map<String, String> findRpcByRoute(final String routeId) {
91         RoutingTable localTable = rpcRegistry.getLocalData();
92         Map<String, String> rpcMap = new HashMap<>(getRpcMemberMapByRoute(localTable, routeId, LOCAL_CONSTANT));
93
94         Map<Address, Bucket<RoutingTable>> buckets = rpcRegistry.getRemoteBuckets();
95         for (Entry<Address, Bucket<RoutingTable>> entry : buckets.entrySet()) {
96             RoutingTable table = entry.getValue().getData();
97             rpcMap.putAll(getRpcMemberMapByRoute(table, routeId, entry.getKey().toString()));
98         }
99
100         log.debug("list of RPCs {} searched by route {}", rpcMap, routeId);
101         return rpcMap;
102     }
103
104     /**
105      * Search if the routing table route String contains routeName.
106      */
107     private static Map<String,String> getRpcMemberMapByRoute(final RoutingTable table, final String routeName,
108                                                       final String address) {
109         Set<DOMRpcIdentifier> routes = table.getRoutes();
110         Map<String, String> rpcMap = new HashMap<>(routes.size());
111         for (DOMRpcIdentifier route : routes) {
112             if (!route.getContextReference().isEmpty()) {
113                 String routeString = route.getContextReference().toString();
114                 if (routeString.contains(routeName)) {
115                     rpcMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
116                 }
117             }
118         }
119         return rpcMap;
120     }
121
122     /**
123      * Search if the routing table route type contains name.
124      */
125     private static Map<String, String>  getRpcMemberMapByName(final RoutingTable table, final String name,
126                                                        final String address) {
127         Set<DOMRpcIdentifier> routes = table.getRoutes();
128         Map<String, String> rpcMap = new HashMap<>(routes.size());
129         for (DOMRpcIdentifier route : routes) {
130             if (!route.getContextReference().isEmpty()) {
131                 String type = route.getType().toString();
132                 if (type.contains(name)) {
133                     rpcMap.put(ROUTE_CONSTANT + route.getContextReference() + NAME_CONSTANT + type, address);
134                 }
135             }
136         }
137         return rpcMap;
138     }
139
140     @Override
141     public String getBucketVersions() {
142         return rpcRegistry.getVersions().toString();
143     }
144 }