Migrate OSGI compendium reference
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.List;
15 import java.util.Map;
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;
28
29 @Deprecated(forRemoval = true)
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;
34
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);
40     }
41
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());
47     }
48
49     @Override
50     protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
51         final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
52
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;
59
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);
64                 }
65
66                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
67
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());
71
72                 if (mayBeRemoteImpls != null) {
73                     return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
74                 }
75
76             } else {
77                 LOG.warn("Ignoring wrong context value {}", value);
78             }
79         }
80
81         final List<DOMRpcImplementation> impls = getImplementations(null);
82         if (impls != null) {
83             return impls.get(0).invokeRpc(globalRpcId, input);
84         } else {
85             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
86                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
87                                                                   getSchemaPath()));
88         }
89     }
90
91     @Override
92     protected RoutedDOMRpcRoutingTableEntry newInstance(
93             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
94         return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
95     }
96 }