ae34b9101741fc0ece5961e6f442497e69352fb5
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / 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.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
20 import org.opendaylight.controller.md.sal.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;
27
28 @Deprecated
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;
33
34     private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId, 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);
39     }
40
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());
46     }
47
48     @Override
49     protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
50         final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
51
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;
58
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);
63                 }
64
65                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
66
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);
70
71                 if (mayBeRemoteImpls != null) {
72                     return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
73                 }
74
75             } else {
76                 LOG.warn("Ignoring wrong context value {}", value);
77             }
78         }
79
80         final List<DOMRpcImplementation> impls = getImplementations(null);
81         if (impls != null) {
82             return impls.get(0).invokeRpc(globalRpcId, input);
83         } else {
84             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
85                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
86                                                                   getSchemaPath()));
87         }
88     }
89
90     @Override
91     protected RoutedDOMRpcRoutingTableEntry newInstance(
92             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
93         return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
94     }
95 }