3d2eb974c03ae8244da33794f5b21c02b2bc4ca6
[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.Optional;
11 import com.google.common.base.Preconditions;
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 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 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
29     private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
30     private final DOMRpcIdentifier globalRpcId;
31     private final YangInstanceIdentifier keyId;
32
33     private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId, final YangInstanceIdentifier keyId, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
34         super(globalRpcId.getType(), impls);
35         this.keyId = Preconditions.checkNotNull(keyId);
36         this.globalRpcId = Preconditions.checkNotNull(globalRpcId);
37     }
38
39     RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
40         super(def.getPath(), impls);
41         this.keyId = Preconditions.checkNotNull(keyId);
42         this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
43     }
44
45     @Override
46     protected CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input) {
47         final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
48
49         // Routing key is present, attempt to deliver as a routed RPC
50         if (maybeKey.isPresent()) {
51             final NormalizedNode<?, ?> key = maybeKey.get();
52             final Object value = key.getValue();
53             if (value instanceof YangInstanceIdentifier) {
54                 final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
55
56                 // Find a DOMRpcImplementation for a specific iid
57                 final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
58                 if (specificImpls != null) {
59                     return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
60                 }
61
62                 LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
63
64                 // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
65                 // implementation this way
66                 final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
67
68                 if(mayBeRemoteImpls != null){
69                     return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
70                 }
71
72             } else {
73                 LOG.warn("Ignoring wrong context value {}", value);
74             }
75         }
76
77         final List<DOMRpcImplementation> impls = getImplementations(null);
78         if (impls != null) {
79             return impls.get(0).invokeRpc(globalRpcId, input);
80         } else {
81             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
82         }
83     }
84
85     @Override
86     protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
87         return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
88     }
89 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.