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