Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / mbeans / RemoteActionRegistryMXBeanImpl.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  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.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.controller.remote.rpc.registry.ActionRoutingTable;
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.DOMActionInstance;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22
23 public class RemoteActionRegistryMXBeanImpl extends AbstractRegistryMXBean<ActionRoutingTable, DOMActionInstance>
24         implements RemoteActionRegistryMXBean {
25     public RemoteActionRegistryMXBeanImpl(final BucketStoreAccess actionRegistryAccess, final Timeout timeout) {
26         super("RemoteActionRegistry", "RemoteActionBroker", actionRegistryAccess, timeout);
27     }
28
29     @Override
30     public Set<String> getLocalRegisteredAction() {
31         ActionRoutingTable table = localData();
32         Set<String> routedAction = new HashSet<>(table.getItems().size());
33         for (DOMActionInstance route : table.getItems()) {
34             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.of(route.getType().lastNodeIdentifier());
35             if (!actionPath.isEmpty()) {
36                 routedAction.add(ROUTE_CONSTANT + actionPath + NAME_CONSTANT + route.getType());
37             }
38         }
39
40         log.debug("Locally registered routed RPCs {}", routedAction);
41         return routedAction;
42     }
43
44     @Override
45     public Map<String, String> findActionByName(final String name) {
46         ActionRoutingTable localTable = localData();
47         // Get all Actions from local bucket
48         Map<String, String> rpcMap = new HashMap<>(getActionMemberMapByName(localTable, name, LOCAL_CONSTANT));
49
50         // Get all Actions from remote bucket
51         Map<Address, Bucket<ActionRoutingTable>> buckets = remoteBuckets();
52         for (Map.Entry<Address, Bucket<ActionRoutingTable>> entry : buckets.entrySet()) {
53             ActionRoutingTable table = entry.getValue().getData();
54             rpcMap.putAll(getActionMemberMapByName(table, name, entry.getKey().toString()));
55         }
56
57         log.debug("list of Actions {} searched by name {}", rpcMap, name);
58         return rpcMap;
59     }
60
61     @Override
62     public Map<String, String> findActionByRoute(final String routeId) {
63         ActionRoutingTable localTable = localData();
64         Map<String, String> rpcMap = new HashMap<>(getActionMemberMapByAction(localTable, routeId, LOCAL_CONSTANT));
65
66         Map<Address, Bucket<ActionRoutingTable>> buckets = remoteBuckets();
67         for (Map.Entry<Address, Bucket<ActionRoutingTable>> entry : buckets.entrySet()) {
68             ActionRoutingTable table = entry.getValue().getData();
69             rpcMap.putAll(getActionMemberMapByAction(table, routeId, entry.getKey().toString()));
70         }
71
72         log.debug("list of Actions {} searched by route {}", rpcMap, routeId);
73         return rpcMap;
74     }
75
76     /**
77      * Search if the routing table route String contains routeName.
78      */
79     private static Map<String, String> getActionMemberMapByAction(final ActionRoutingTable table,
80                                                                   final String routeName, final String address) {
81         Collection<DOMActionInstance> routes = table.getItems();
82         Map<String, String> actionMap = new HashMap<>(routes.size());
83         for (DOMActionInstance route : routes) {
84             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.of(route.getType().lastNodeIdentifier());
85             if (!actionPath.isEmpty()) {
86                 String routeString = actionPath.toString();
87                 if (routeString.contains(routeName)) {
88                     actionMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
89                 }
90             }
91         }
92         return actionMap;
93     }
94
95     /**
96      * Search if the routing table route type contains name.
97      */
98     private static Map<String, String> getActionMemberMapByName(final ActionRoutingTable table, final String name,
99                                                                 final String address) {
100         Collection<DOMActionInstance> routes = table.getItems();
101         Map<String, String> actionMap = new HashMap<>(routes.size());
102         for (DOMActionInstance route : routes) {
103             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.of(route.getType().lastNodeIdentifier());
104             if (!actionPath.isEmpty()) {
105                 String type = route.getType().toString();
106                 if (type.contains(name)) {
107                     actionMap.put(ROUTE_CONSTANT + actionPath + NAME_CONSTANT + type, address);
108                 }
109             }
110         }
111         return actionMap;
112     }
113
114     @Override
115     public String getBucketVersions() {
116         return bucketVersions();
117     }
118 }