10523ea2d1e92739911ee78128dc361d6d40a939
[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 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;
15
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;
21 import java.util.Map;
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 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, 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, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
41         super(def.getPath(), impls);
42         this.keyId = Preconditions.checkNotNull(keyId);
43         this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
44     }
45
46     @Override
47     protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
48         final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
49
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;
56
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);
61                 }
62
63                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
64
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);
68
69                 if(mayBeRemoteImpls != null){
70                     return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
71                 }
72
73             } else {
74                 LOG.warn("Ignoring wrong context value {}", value);
75             }
76         }
77
78         final List<DOMRpcImplementation> impls = getImplementations(null);
79         if (impls != null) {
80             return impls.get(0).invokeRpc(globalRpcId, input);
81         } else {
82             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
83         }
84     }
85
86     @Override
87     protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
88         return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
89     }
90 }