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