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.util.concurrent.CheckedFuture;
14 import java.util.ArrayList;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
21 import org.opendaylight.controller.md.sal.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, List<DOMRpcImplementation>> impls) {
31 this.schemaPath = Preconditions.checkNotNull(schemaPath);
32 this.impls = Preconditions.checkNotNull(impls);
35 protected final SchemaPath getSchemaPath() {
39 protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
40 return impls.get(context);
43 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
47 public boolean containsContext(final YangInstanceIdentifier contextReference) {
48 return impls.containsKey(contextReference);
51 final Set<YangInstanceIdentifier> registeredIdentifiers() {
52 return impls.keySet();
57 * @param implementation
58 * @param newRpcs List of new RPCs, must be mutable
61 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
62 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
63 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
64 if (newRpcs.remove(ve.getKey())) {
65 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
66 i.addAll(ve.getValue());
67 i.add(implementation);
68 vb.put(ve.getKey(), i);
73 for(final YangInstanceIdentifier ii : newRpcs) {
74 final ArrayList<DOMRpcImplementation> impl = new ArrayList<>(1);
75 impl.add(implementation);
79 return newInstance(vb.build());
82 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
83 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
84 for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
85 if (removed.remove(ve.getKey())) {
86 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
87 i.remove(implementation);
88 // We could trimToSize(), but that may perform another copy just to get rid
89 // of a single element. That is probably not worth the trouble.
91 vb.put(ve.getKey(), i);
98 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
99 return v.isEmpty() ? null : newInstance(v);
102 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
103 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);