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>> impls;
31 private final SchemaPath schemaPath;
33 AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
34 List<DOMRpcImplementation>> impls) {
35 this.schemaPath = Preconditions.checkNotNull(schemaPath);
36 this.impls = Preconditions.checkNotNull(impls);
39 final SchemaPath getSchemaPath() {
43 final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
44 return impls.get(context);
47 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
51 final boolean containsContext(final YangInstanceIdentifier contextReference) {
52 return impls.containsKey(contextReference);
55 final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener listener) {
56 return Maps.filterValues(impls, list -> list.stream().anyMatch(listener::acceptsImplementation)).keySet();
60 final Set<YangInstanceIdentifier> registeredIdentifiers() {
61 return impls.keySet();
65 * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
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
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);
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);
88 for (final YangInstanceIdentifier ii : newRpcs) {
89 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
90 impl.add(implementation);
94 return newInstance(vb.build());
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.
107 vb.put(ve.getKey(), i);
114 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
115 return v.isEmpty() ? null : newInstance(v);
118 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(NormalizedNode<?, ?> input);
120 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
121 Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);