af6bd7515aa036a9f0ec0c813f21b0f85575d808
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / SchemalessNetconfDeviceRpc.java
1 /*
2  * Copyright (c) 2016 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.netconf.sal.connect.netconf.sal;
9
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;
33
34 /**
35  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
36  */
37 public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
38
39     private final RemoteDeviceCommunicator listener;
40     private final BaseRpcSchemalessTransformer baseRpcTransformer;
41     private final SchemalessMessageTransformer schemalessTransformer;
42     private final RemoteDeviceId deviceId;
43
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;
51     }
52
53     @Override
54     public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
55         final MessageTransformer<NetconfMessage> transformer;
56         if (input instanceof DOMSourceAnyxmlNode) {
57             transformer = schemalessTransformer;
58         } else if (isBaseRpc(type)) {
59             transformer = baseRpcTransformer;
60         } else {
61             return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
62                 "Unable to invoke rpc %s", type));
63         }
64         return handleRpc(type, input, transformer);
65     }
66
67     private ListenableFuture<DOMRpcResult> handleRpc(final @NonNull QName type, final @NonNull NormalizedNode input,
68             final MessageTransformer<NetconfMessage> transformer) {
69         final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = listener.sendRequest(
70             transformer.toRpcRequest(type, input), type);
71
72         final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
73         Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
74             @Override
75             public void onSuccess(final RpcResult<NetconfMessage> result) {
76                 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
77                         : new DefaultDOMRpcResult(result.getErrors()));
78             }
79
80             @Override
81             public void onFailure(final Throwable cause) {
82                 ret.setException(new DOMRpcImplementationNotAvailableException(cause,
83                     "Unable to invoke rpc %s on device %s", type, deviceId));
84             }
85
86         }, MoreExecutors.directExecutor());
87         return ret;
88     }
89
90     private static boolean isBaseRpc(final QName type) {
91         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getNamespace());
92     }
93
94     @Override
95     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
96         throw new UnsupportedOperationException("Not available for netconf 1.0");
97     }
98 }