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.Comparator;
17 import java.util.List;
19 import java.util.Map.Entry;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.controller.md.sal.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,
34 final Map<YangInstanceIdentifier, 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 l) {
56 return Maps.filterValues(impls, list -> list.stream().anyMatch(l::acceptsImplementation)).keySet();
61 * @param implementation
62 * @param newRpcs List of new RPCs, must be mutable
65 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation,
66 final List<YangInstanceIdentifier> newRpcs) {
67 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
68 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
69 if (newRpcs.remove(ve.getKey())) {
70 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
71 i.addAll(ve.getValue());
72 i.add(implementation);
74 // New implementation is at the end, this will move it to be the last among implementations
75 // with equal cost -- relying on sort() being stable.
76 i.sort(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
77 vb.put(ve.getKey(), i);
82 for(final YangInstanceIdentifier ii : newRpcs) {
83 final List<DOMRpcImplementation> impl = new ArrayList<>(1);
84 impl.add(implementation);
88 return newInstance(vb.build());
91 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation,
92 final List<YangInstanceIdentifier> removed) {
93 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
94 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
95 if (removed.remove(ve.getKey())) {
96 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
97 i.remove(implementation);
98 // We could trimToSize(), but that may perform another copy just to get rid
99 // of a single element. That is probably not worth the trouble.
101 vb.put(ve.getKey(), i);
108 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
109 return v.isEmpty() ? null : newInstance(v);
112 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
113 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);