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