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 org.opendaylight.mdsal.dom.api.DOMRpcException;
11 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
12 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.util.ArrayList;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 abstract class AbstractDOMRpcRoutingTableEntry {
28 private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
29 private final SchemaPath schemaPath;
31 protected AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier, 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();
58 * @param implementation
59 * @param newRpcs List of new RPCs, must be mutable
62 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
63 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
64 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
65 if (newRpcs.remove(ve.getKey())) {
66 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
67 i.addAll(ve.getValue());
68 i.add(implementation);
69 vb.put(ve.getKey(), i);
74 for(final YangInstanceIdentifier ii : newRpcs) {
75 final ArrayList<DOMRpcImplementation> impl = new ArrayList<>(1);
76 impl.add(implementation);
80 return newInstance(vb.build());
83 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
84 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
85 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
86 if (removed.remove(ve.getKey())) {
87 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
88 i.remove(implementation);
89 // We could trimToSize(), but that may perform another copy just to get rid
90 // of a single element. That is probably not worth the trouble.
92 vb.put(ve.getKey(), i);
99 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
100 return v.isEmpty() ? null : newInstance(v);
103 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
104 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);