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 org.opendaylight.mdsal.dom.api.DOMRpcException;
11 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
12 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
13 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.Futures;
20 import java.util.List;
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;
29 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
30 private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
31 private final DOMRpcIdentifier globalRpcId;
32 private final YangInstanceIdentifier keyId;
34 private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId, final YangInstanceIdentifier keyId, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
35 super(globalRpcId.getType(), impls);
36 this.keyId = Preconditions.checkNotNull(keyId);
37 this.globalRpcId = Preconditions.checkNotNull(globalRpcId);
40 RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
41 super(def.getPath(), impls);
42 this.keyId = Preconditions.checkNotNull(keyId);
43 this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
47 protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
48 final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
50 // Routing key is present, attempt to deliver as a routed RPC
51 if (maybeKey.isPresent()) {
52 final NormalizedNode<?, ?> key = maybeKey.get();
53 final Object value = key.getValue();
54 if (value instanceof YangInstanceIdentifier) {
55 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
57 // Find a DOMRpcImplementation for a specific iid
58 final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
59 if (specificImpls != null) {
60 return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
63 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
65 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
66 // implementation this way
67 final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
69 if(mayBeRemoteImpls != null){
70 return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
74 LOG.warn("Ignoring wrong context value {}", value);
78 final List<DOMRpcImplementation> impls = getImplementations(null);
80 return impls.get(0).invokeRpc(globalRpcId, input);
82 return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
87 protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
88 return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);