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.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.ArrayList;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import org.opendaylight.mdsal.dom.api.DOMRpcException;
20 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 abstract class AbstractDOMRpcRoutingTableEntry {
27 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
28 private final SchemaPath schemaPath;
30 protected AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
31 List<DOMRpcImplementation>> impls) {
32 this.schemaPath = Preconditions.checkNotNull(schemaPath);
33 this.impls = Preconditions.checkNotNull(impls);
36 protected final SchemaPath getSchemaPath() {
40 protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
41 return impls.get(context);
44 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
48 public boolean containsContext(final YangInstanceIdentifier contextReference) {
49 return impls.containsKey(contextReference);
52 final Set<YangInstanceIdentifier> registeredIdentifiers() {
53 return impls.keySet();
57 * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
59 * @param implementation the DOMRpcImplementation instance to add
60 * @param newRpcs the List of new RPCs that the DOMRpcImplementation provides, must be mutable
61 * @return a new instance of AbstractDOMRpcRoutingTableEntry with the additions
63 final AbstractDOMRpcRoutingTableEntry add(
64 final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
65 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
66 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
67 if (newRpcs.remove(ve.getKey())) {
68 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
69 i.addAll(ve.getValue());
70 i.add(implementation);
71 vb.put(ve.getKey(), i);
76 for (final YangInstanceIdentifier ii : newRpcs) {
77 final ArrayList<DOMRpcImplementation> impl = new ArrayList<>(1);
78 impl.add(implementation);
82 return newInstance(vb.build());
85 final AbstractDOMRpcRoutingTableEntry remove(
86 final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
87 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
88 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
89 if (removed.remove(ve.getKey())) {
90 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
91 i.remove(implementation);
92 // We could trimToSize(), but that may perform another copy just to get rid
93 // of a single element. That is probably not worth the trouble.
95 vb.put(ve.getKey(), i);
102 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
103 return v.isEmpty() ? null : newInstance(v);
106 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
108 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
109 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);