Bump upstreams for Silicon
[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             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.create(new NodeIdentifier(
37                 route.getType().lastNodeIdentifier()));
38             if (!actionPath.isEmpty()) {
39                 routedAction.add(ROUTE_CONSTANT + actionPath + NAME_CONSTANT + route.getType());
40             }
41         }
42
43         log.debug("Locally registered routed RPCs {}", routedAction);
44         return routedAction;
45     }
46
47     @Override
48     public Map<String, String> findActionByName(final String name) {
49         ActionRoutingTable localTable = localData();
50         // Get all Actions from local bucket
51         Map<String, String> rpcMap = new HashMap<>(getActionMemberMapByName(localTable, name, LOCAL_CONSTANT));
52
53         // Get all Actions from remote bucket
54         Map<Address, Bucket<ActionRoutingTable>> buckets = remoteBuckets();
55         for (Map.Entry<Address, Bucket<ActionRoutingTable>> entry : buckets.entrySet()) {
56             ActionRoutingTable table = entry.getValue().getData();
57             rpcMap.putAll(getActionMemberMapByName(table, name, entry.getKey().toString()));
58         }
59
60         log.debug("list of Actions {} searched by name {}", rpcMap, name);
61         return rpcMap;
62     }
63
64     @Override
65     public Map<String, String> findActionByRoute(final String routeId) {
66         ActionRoutingTable localTable = localData();
67         Map<String, String> rpcMap = new HashMap<>(getActionMemberMapByAction(localTable, routeId, LOCAL_CONSTANT));
68
69         Map<Address, Bucket<ActionRoutingTable>> buckets = remoteBuckets();
70         for (Map.Entry<Address, Bucket<ActionRoutingTable>> entry : buckets.entrySet()) {
71             ActionRoutingTable table = entry.getValue().getData();
72             rpcMap.putAll(getActionMemberMapByAction(table, routeId, entry.getKey().toString()));
73         }
74
75         log.debug("list of Actions {} searched by route {}", rpcMap, routeId);
76         return rpcMap;
77     }
78
79     /**
80      * Search if the routing table route String contains routeName.
81      */
82     private static Map<String, String> getActionMemberMapByAction(final ActionRoutingTable table,
83                                                                   final String routeName, final String address) {
84         Collection<DOMActionInstance> routes = table.getItems();
85         Map<String, String> actionMap = new HashMap<>(routes.size());
86         for (DOMActionInstance route : routes) {
87             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.create(new NodeIdentifier(
88                 route.getType().lastNodeIdentifier()));
89             if (!actionPath.isEmpty()) {
90                 String routeString = actionPath.toString();
91                 if (routeString.contains(routeName)) {
92                     actionMap.put(ROUTE_CONSTANT + routeString + NAME_CONSTANT + route.getType(), address);
93                 }
94             }
95         }
96         return actionMap;
97     }
98
99     /**
100      * Search if the routing table route type contains name.
101      */
102     private static Map<String, String> getActionMemberMapByName(final ActionRoutingTable table, final String name,
103                                                                 final String address) {
104         Collection<DOMActionInstance> routes = table.getItems();
105         Map<String, String> actionMap = new HashMap<>(routes.size());
106         for (DOMActionInstance route : routes) {
107             final YangInstanceIdentifier actionPath = YangInstanceIdentifier.create(new NodeIdentifier(
108                 route.getType().lastNodeIdentifier()));
109             if (!actionPath.isEmpty()) {
110                 String type = route.getType().toString();
111                 if (type.contains(name)) {
112                     actionMap.put(ROUTE_CONSTANT + actionPath + NAME_CONSTANT + type, address);
113                 }
114             }
115         }
116         return actionMap;
117     }
118
119     @Override
120     public String getBucketVersions() {
121         return bucketVersions();
122     }
123 }