2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
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;
19 import java.util.Map.Entry;
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;
29 abstract class AbstractDOMRpcRoutingTableEntry {
30 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations;
31 private final SchemaPath schemaPath;
33 AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
34 List<DOMRpcImplementation>> implementations) {
35 this.schemaPath = Preconditions.checkNotNull(schemaPath);
36 this.implementations = Preconditions.checkNotNull(implementations);
39 final SchemaPath getSchemaPath() {
43 final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
44 return implementations.get(context);
47 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
48 return implementations;
51 final boolean containsContext(final YangInstanceIdentifier contextReference) {
52 return implementations.containsKey(contextReference);
55 final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener listener) {
56 return Maps.filterValues(implementations, list -> list.stream()
57 .anyMatch(listener::acceptsImplementation)).keySet();
61 final Set<YangInstanceIdentifier> registeredIdentifiers() {
62 return implementations.keySet();
66 * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
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
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);
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((i1, i2) -> Long.compare(i1.invocationCost(), i2.invocationCost()));
84 vb.put(ve.getKey(), i);
89 for (final YangInstanceIdentifier ii : newRpcs) {
90 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
91 impl.add(implementation);
95 return newInstance(vb.build());
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.
108 vb.put(ve.getKey(), i);
115 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
116 return v.isEmpty() ? null : newInstance(v);
119 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(NormalizedNode<?, ?> input);
121 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
122 Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);