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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import com.google.common.collect.Maps;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.ArrayList;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 abstract class AbstractDOMRpcRoutingTableEntry {
29 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
30 private final SchemaPath schemaPath;
32 AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath,
33 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
34 this.schemaPath = Preconditions.checkNotNull(schemaPath);
35 this.impls = Preconditions.checkNotNull(impls);
38 final SchemaPath getSchemaPath() {
42 final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
43 return impls.get(context);
46 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
50 final boolean containsContext(final YangInstanceIdentifier contextReference) {
51 return impls.containsKey(contextReference);
54 final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener l) {
55 return Maps.filterValues(impls, list -> list.stream().anyMatch(l::acceptsImplementation)).keySet();
60 * @param implementation
61 * @param newRpcs List of new RPCs, must be mutable
64 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation,
65 final List<YangInstanceIdentifier> newRpcs) {
66 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
67 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
68 if (newRpcs.remove(ve.getKey())) {
69 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
70 i.addAll(ve.getValue());
71 i.add(implementation);
73 // New implementation is at the end, this will move it to be the last among implementations
74 // with equal cost -- relying on sort() being stable.
75 i.sort((a, b) -> Long.compare(a.invocationCost(), b.invocationCost()));
76 vb.put(ve.getKey(), i);
81 for(final YangInstanceIdentifier ii : newRpcs) {
82 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
83 impl.add(implementation);
87 return newInstance(vb.build());
90 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation,
91 final List<YangInstanceIdentifier> removed) {
92 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
93 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
94 if (removed.remove(ve.getKey())) {
95 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
96 i.remove(implementation);
97 // We could trimToSize(), but that may perform another copy just to get rid
98 // of a single element. That is probably not worth the trouble.
100 vb.put(ve.getKey(), i);
107 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
108 return v.isEmpty() ? null : newInstance(v);
111 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
112 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);