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 static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.List;
16 import java.util.Optional;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
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.data.api.schema.NormalizedNodes;
25 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
31 private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
32 private final DOMRpcIdentifier globalRpcId;
33 private final YangInstanceIdentifier keyId;
35 private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId, final YangInstanceIdentifier keyId,
36 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
37 super(globalRpcId.getType(), impls);
38 this.keyId = requireNonNull(keyId);
39 this.globalRpcId = requireNonNull(globalRpcId);
42 RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId,
43 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
44 super(def.getPath(), impls);
45 this.keyId = requireNonNull(keyId);
46 this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
50 protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
51 final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
53 // Routing key is present, attempt to deliver as a routed RPC
54 if (maybeKey.isPresent()) {
55 final NormalizedNode<?, ?> key = maybeKey.get();
56 final Object value = key.getValue();
57 if (value instanceof YangInstanceIdentifier) {
58 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
60 // Find a DOMRpcImplementation for a specific iid
61 final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
62 if (specificImpls != null) {
63 return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
66 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
68 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
69 // implementation this way
70 final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
72 if (mayBeRemoteImpls != null) {
73 return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
77 LOG.warn("Ignoring wrong context value {}", value);
81 final List<DOMRpcImplementation> impls = getImplementations(null);
83 return impls.get(0).invokeRpc(globalRpcId, input);
85 return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
86 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
92 protected RoutedDOMRpcRoutingTableEntry newInstance(
93 final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
94 return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);