2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
17 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
18 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
19 import org.opendaylight.netconf.api.NetconfMessage;
20 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
21 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
22 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
23 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
24 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
25 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
36 public final class SchemalessNetconfDeviceRpc implements Rpcs.Schemaless {
37 private final RemoteDeviceCommunicator listener;
38 private final BaseRpcSchemalessTransformer baseRpcTransformer;
39 private final SchemalessMessageTransformer schemalessTransformer;
40 private final RemoteDeviceId deviceId;
42 public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId, final RemoteDeviceCommunicator listener,
43 final BaseRpcSchemalessTransformer baseRpcTransformer,
44 final SchemalessMessageTransformer messageTransformer) {
45 this.deviceId = deviceId;
46 this.listener = listener;
47 this.baseRpcTransformer = baseRpcTransformer;
48 schemalessTransformer = messageTransformer;
52 public ListenableFuture<? extends DOMRpcResult> invokeNetconf(final QName type, final ContainerNode input) {
53 if (!isBaseRpc(type)) {
54 throw new IllegalArgumentException("Cannot handle " + type);
56 return handleRpc(type, input, baseRpcTransformer);
60 public ListenableFuture<? extends DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
61 final MessageTransformer transformer;
62 if (input instanceof DOMSourceAnyxmlNode) {
63 transformer = schemalessTransformer;
64 } else if (isBaseRpc(type)) {
65 transformer = baseRpcTransformer;
67 return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
68 "Unable to invoke rpc %s", type));
70 return handleRpc(type, input, transformer);
73 private @NonNull ListenableFuture<DOMRpcResult> handleRpc(final @NonNull QName type,
74 final @NonNull NormalizedNode input, final MessageTransformer transformer) {
75 final var delegateFuture = listener.sendRequest(transformer.toRpcRequest(type, input), type);
76 final var ret = SettableFuture.<DOMRpcResult>create();
77 Futures.addCallback(delegateFuture, new FutureCallback<>() {
79 public void onSuccess(final RpcResult<NetconfMessage> result) {
80 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
81 : new DefaultDOMRpcResult(result.getErrors()));
85 public void onFailure(final Throwable cause) {
86 ret.setException(new DOMRpcImplementationNotAvailableException(cause,
87 "Unable to invoke rpc %s on device %s", type, deviceId));
90 }, MoreExecutors.directExecutor());
94 private static boolean isBaseRpc(final QName type) {
95 return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getNamespace());