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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.List;
16 import org.opendaylight.mdsal.dom.api.DOMRpcException;
17 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
29 private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
30 private final DOMRpcIdentifier globalRpcId;
31 private final YangInstanceIdentifier keyId;
33 private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId,
34 final YangInstanceIdentifier keyId,
35 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
36 super(globalRpcId.getType(), impls);
37 this.keyId = Preconditions.checkNotNull(keyId);
38 this.globalRpcId = Preconditions.checkNotNull(globalRpcId);
41 RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId,
42 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
43 super(def.getPath(), impls);
44 this.keyId = Preconditions.checkNotNull(keyId);
45 this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
49 protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
50 final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
52 // Routing key is present, attempt to deliver as a routed RPC
53 if (maybeKey.isPresent()) {
54 final NormalizedNode<?, ?> key = maybeKey.get();
55 final Object value = key.getValue();
56 if (value instanceof YangInstanceIdentifier) {
57 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
59 // Find a DOMRpcImplementation for a specific iid
60 final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
61 if (specificImpls != null) {
62 return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
65 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
67 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
68 // implementation this way
69 final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
71 if (mayBeRemoteImpls != null) {
72 return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
76 LOG.warn("Ignoring wrong context value {}", value);
80 final List<DOMRpcImplementation> impls = getImplementations(null);
82 return impls.get(0).invokeRpc(globalRpcId, input);
84 return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
85 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
91 protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier,
92 List<DOMRpcImplementation>> impls) {
93 return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);