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();
55 final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
56 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
57 for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
58 if (newRpcs.remove(ve.getKey())) {
59 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
60 i.addAll(ve.getValue());
61 i.add(implementation);
62 vb.put(ve.getKey(), i);
68 return newInstance(vb.build());
71 final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
72 final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
73 for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
74 if (removed.remove(ve.getKey())) {
75 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
76 i.remove(implementation);
77 // We could trimToSize(), but that may perform another copy just to get rid
78 // of a single element. That is probably not worth the trouble.
80 vb.put(ve.getKey(), i);
87 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
88 return v.isEmpty() ? null : newInstance(v);
91 protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
92 protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);