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.controller.md.sal.dom.broker.impl;
10 import static java.util.Objects.requireNonNull;
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.Comparator;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 abstract class AbstractDOMRpcRoutingTableEntry {
32 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations;
33 private final SchemaPath schemaPath;
35 AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath,
36 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations) {
37 this.schemaPath = requireNonNull(schemaPath);
38 this.implementations = requireNonNull(implementations);
41 final SchemaPath getSchemaPath() {
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().anyMatch(listener::acceptsImplementation))
63 * Adds an entry to the DOM RPC routing table.
65 * @param implementation RPC implementation
66 * @param newRpcs List of new RPCs, must be mutable
68 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation,
69 final List<YangInstanceIdentifier> newRpcs) {
70 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
71 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : implementations.entrySet()) {
72 if (newRpcs.remove(ve.getKey())) {
73 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
74 i.addAll(ve.getValue());
75 i.add(implementation);
77 // New implementation is at the end, this will move it to be the last among implementations
78 // with equal cost -- relying on sort() being stable.
79 i.sort(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
80 vb.put(ve.getKey(), i);
85 for (final YangInstanceIdentifier ii : newRpcs) {
86 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
87 impl.add(implementation);
91 return newInstance(vb.build());
94 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation,
95 final List<YangInstanceIdentifier> removed) {
96 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
97 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : implementations.entrySet()) {
98 if (removed.remove(ve.getKey())) {
99 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
100 i.remove(implementation);
101 // We could trimToSize(), but that may perform another copy just to get rid
102 // of a single element. That is probably not worth the trouble.
104 vb.put(ve.getKey(), i);
111 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
112 return v.isEmpty() ? null : newInstance(v);
115 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(NormalizedNode<?, ?> input);
117 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
118 Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);