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 java.util.ArrayList;
16 import java.util.Comparator;
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.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;
27 abstract class AbstractDOMRpcRoutingTableEntry {
28 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations;
29 private final DOMRpcIdentifier rpcId;
31 AbstractDOMRpcRoutingTableEntry(final DOMRpcIdentifier rpcId, final Map<YangInstanceIdentifier,
32 List<DOMRpcImplementation>> implementations) {
33 this.rpcId = Preconditions.checkNotNull(rpcId);
34 this.implementations = Preconditions.checkNotNull(implementations);
37 final SchemaPath getSchemaPath() {
38 return rpcId.getType();
41 final DOMRpcIdentifier getRpcId() {
45 final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
46 return implementations.get(context);
49 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
50 return implementations;
53 final boolean containsContext(final YangInstanceIdentifier contextReference) {
54 return implementations.containsKey(contextReference);
57 final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener listener) {
58 return Maps.filterValues(implementations, list -> list.stream()
59 .anyMatch(listener::acceptsImplementation)).keySet();
63 final Set<YangInstanceIdentifier> registeredIdentifiers() {
64 return implementations.keySet();
68 * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
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
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);
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);
91 for (final YangInstanceIdentifier ii : newRpcs) {
92 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
93 impl.add(implementation);
97 return newInstance(vb.build());
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.
110 vb.put(ve.getKey(), i);
117 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
118 return v.isEmpty() ? null : newInstance(v);
121 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
122 Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);