Change DOM invokeRpc to FluentFuture
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / RoutedDOMRpcRoutingTableEntry.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
17 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
23 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
28     private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
29     private final DOMRpcIdentifier globalRpcId;
30     private final YangInstanceIdentifier keyId;
31
32     private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId,
33             final YangInstanceIdentifier keyId,
34             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
35         super(globalRpcId.getType(), impls);
36         this.keyId = Preconditions.checkNotNull(keyId);
37         this.globalRpcId = Preconditions.checkNotNull(globalRpcId);
38     }
39
40     RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId,
41             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
42         super(def.getPath(), impls);
43         this.keyId = Preconditions.checkNotNull(keyId);
44         this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
45     }
46
47     @Override
48     protected FluentFuture<DOMRpcResult> invokeRpc(final NormalizedNode<?, ?> input) {
49         final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
50
51         // Routing key is present, attempt to deliver as a routed RPC
52         if (maybeKey.isPresent()) {
53             final NormalizedNode<?, ?> key = maybeKey.get();
54             final Object value = key.getValue();
55             if (value instanceof YangInstanceIdentifier) {
56                 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
57
58                 // Find a DOMRpcImplementation for a specific iid
59                 final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
60                 if (specificImpls != null) {
61                     return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
62                 }
63
64                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
65
66                 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
67                 // implementation this way
68                 final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
69
70                 if (mayBeRemoteImpls != null) {
71                     return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
72                 }
73
74             } else {
75                 LOG.warn("Ignoring wrong context value {}", value);
76             }
77         }
78
79         final List<DOMRpcImplementation> impls = getImplementations(null);
80         if (impls != null) {
81             return impls.get(0).invokeRpc(globalRpcId, input);
82         }
83
84         return FluentFutures.immediateFailedFluentFuture(
85             new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
86     }
87
88     @Override
89     protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier,
90             List<DOMRpcImplementation>> impls) {
91         return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
92     }
93 }