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.DOMRpcAvailabilityListener;
17 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.mdsal.dom.api.DOMRpcService;
20 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.netconf.api.NetconfMessage;
22 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
23 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
24 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
25 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
26 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
37 public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
39 private final RemoteDeviceCommunicator listener;
40 private final BaseRpcSchemalessTransformer baseRpcTransformer;
41 private final SchemalessMessageTransformer schemalessTransformer;
42 private final RemoteDeviceId deviceId;
44 public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId, final RemoteDeviceCommunicator listener,
45 final BaseRpcSchemalessTransformer baseRpcTransformer,
46 final SchemalessMessageTransformer messageTransformer) {
47 this.deviceId = deviceId;
48 this.listener = listener;
49 this.baseRpcTransformer = baseRpcTransformer;
50 schemalessTransformer = messageTransformer;
54 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
55 final MessageTransformer transformer;
56 if (input instanceof DOMSourceAnyxmlNode) {
57 transformer = schemalessTransformer;
58 } else if (isBaseRpc(type)) {
59 transformer = baseRpcTransformer;
61 return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
62 "Unable to invoke rpc %s", type));
64 return handleRpc(type, input, transformer);
67 private ListenableFuture<DOMRpcResult> handleRpc(final @NonNull QName type, final @NonNull NormalizedNode input,
68 final MessageTransformer transformer) {
69 final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = listener.sendRequest(
70 transformer.toRpcRequest(type, input), type);
72 final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
73 Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
75 public void onSuccess(final RpcResult<NetconfMessage> result) {
76 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
77 : new DefaultDOMRpcResult(result.getErrors()));
81 public void onFailure(final Throwable cause) {
82 ret.setException(new DOMRpcImplementationNotAvailableException(cause,
83 "Unable to invoke rpc %s on device %s", type, deviceId));
86 }, MoreExecutors.directExecutor());
90 private static boolean isBaseRpc(final QName type) {
91 return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getNamespace());
95 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
96 throw new UnsupportedOperationException("Not available for netconf 1.0");