Bump MRI upstreams
[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<NetconfMessage> listener;
40     private final BaseRpcSchemalessTransformer baseRpcTransformer;
41     private final SchemalessMessageTransformer schemalessTransformer;
42     private final RemoteDeviceId deviceId;
43
44     public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
45                                       final RemoteDeviceCommunicator<NetconfMessage> listener,
46                                       final BaseRpcSchemalessTransformer baseRpcTransformer,
47                                       final SchemalessMessageTransformer messageTransformer) {
48         this.deviceId = deviceId;
49         this.listener = listener;
50         this.baseRpcTransformer = baseRpcTransformer;
51         this.schemalessTransformer = messageTransformer;
52     }
53
54     @Override
55     public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final NormalizedNode input) {
56         final MessageTransformer<NetconfMessage> transformer;
57         if (input instanceof DOMSourceAnyxmlNode) {
58             transformer = schemalessTransformer;
59         } else if (isBaseRpc(type)) {
60             transformer = baseRpcTransformer;
61         } else {
62             return Futures.immediateFailedFuture(new DOMRpcImplementationNotAvailableException(
63                 "Unable to invoke rpc %s", type));
64         }
65         return handleRpc(type, input, transformer);
66     }
67
68     private ListenableFuture<DOMRpcResult> handleRpc(final @NonNull QName type, final @NonNull NormalizedNode input,
69             final MessageTransformer<NetconfMessage> transformer) {
70         final ListenableFuture<RpcResult<NetconfMessage>> delegateFuture = listener.sendRequest(
71             transformer.toRpcRequest(type, input), type);
72
73         final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
74         Futures.addCallback(delegateFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
75             @Override
76             public void onSuccess(final RpcResult<NetconfMessage> result) {
77                 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
78                         : new DefaultDOMRpcResult(result.getErrors()));
79             }
80
81             @Override
82             public void onFailure(final Throwable cause) {
83                 ret.setException(new DOMRpcImplementationNotAvailableException(cause,
84                     "Unable to invoke rpc %s on device %s", type, deviceId));
85             }
86
87         }, MoreExecutors.directExecutor());
88         return ret;
89     }
90
91     private static boolean isBaseRpc(final QName type) {
92         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getNamespace());
93     }
94
95     @Override
96     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T lsnr) {
97         throw new UnsupportedOperationException("Not available for netconf 1.0");
98     }
99 }