32f8ba6053d1e1895111d800abd8c5e8b6bf348a
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMRpcRoutingTableEntry.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.mdsal.dom.broker;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.Maps;
15 import java.util.ArrayList;
16 import java.util.Comparator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
22 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
23 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26
27 abstract class AbstractDOMRpcRoutingTableEntry {
28     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations;
29     private final DOMRpcIdentifier rpcId;
30
31     AbstractDOMRpcRoutingTableEntry(final DOMRpcIdentifier rpcId, final Map<YangInstanceIdentifier,
32             List<DOMRpcImplementation>> implementations) {
33         this.rpcId = Preconditions.checkNotNull(rpcId);
34         this.implementations = Preconditions.checkNotNull(implementations);
35     }
36
37     final SchemaPath getSchemaPath() {
38         return rpcId.getType();
39     }
40
41     final DOMRpcIdentifier getRpcId() {
42         return rpcId;
43     }
44
45     final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
46         return implementations.get(context);
47     }
48
49     final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
50         return implementations;
51     }
52
53     final boolean containsContext(final YangInstanceIdentifier contextReference) {
54         return implementations.containsKey(contextReference);
55     }
56
57     final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener listener) {
58         return Maps.filterValues(implementations, list -> list.stream()
59             .anyMatch(listener::acceptsImplementation)).keySet();
60     }
61
62     @VisibleForTesting
63     final Set<YangInstanceIdentifier> registeredIdentifiers() {
64         return implementations.keySet();
65     }
66
67     /**
68      * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
69      *
70      * @param implementation the DOMRpcImplementation instance to add
71      * @param newRpcs the List of new RPCs that the DOMRpcImplementation provides, must be mutable
72      * @return a new instance of AbstractDOMRpcRoutingTableEntry with the additions
73      */
74     final AbstractDOMRpcRoutingTableEntry add(
75             final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
76         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
77         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : implementations.entrySet()) {
78             if (newRpcs.remove(ve.getKey())) {
79                 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
80                 i.addAll(ve.getValue());
81                 i.add(implementation);
82
83                 // New implementation is at the end, this will move it to be the last among implementations
84                 // with equal cost -- relying on sort() being stable.
85                 i.sort(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
86                 vb.put(ve.getKey(), i);
87             } else {
88                 vb.put(ve);
89             }
90         }
91         for (final YangInstanceIdentifier ii : newRpcs) {
92             final List<DOMRpcImplementation> impl = new ArrayList<>(1);
93             impl.add(implementation);
94             vb.put(ii, impl);
95         }
96
97         return newInstance(vb.build());
98     }
99
100     final AbstractDOMRpcRoutingTableEntry remove(
101             final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
102         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
103         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : implementations.entrySet()) {
104             if (removed.remove(ve.getKey())) {
105                 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
106                 i.remove(implementation);
107                 // We could trimToSize(), but that may perform another copy just to get rid
108                 // of a single element. That is probably not worth the trouble.
109                 if (!i.isEmpty()) {
110                     vb.put(ve.getKey(), i);
111                 }
112             } else {
113                 vb.put(ve);
114             }
115         }
116
117         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
118         return v.isEmpty() ? null : newInstance(v);
119     }
120
121     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
122             Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
123 }