Migrate netconf to MD-SAL APIs
[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.FluentFuture;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import com.google.common.util.concurrent.SettableFuture;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
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.util.concurrent.FluentFutures;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34
35 /**
36  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
37  */
38 public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
39
40     private final RemoteDeviceCommunicator<NetconfMessage> listener;
41     private final BaseRpcSchemalessTransformer baseRpcTransformer;
42     private final SchemalessMessageTransformer schemalessTransformer;
43     private final RemoteDeviceId deviceId;
44
45     public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
46                                       final RemoteDeviceCommunicator<NetconfMessage> listener,
47                                       final BaseRpcSchemalessTransformer baseRpcTransformer,
48                                       final SchemalessMessageTransformer messageTransformer) {
49         this.deviceId = deviceId;
50         this.listener = listener;
51         this.baseRpcTransformer = baseRpcTransformer;
52         this.schemalessTransformer = messageTransformer;
53     }
54
55     @Override
56     public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
57         final MessageTransformer<NetconfMessage> transformer;
58         if (input instanceof AnyXmlNode) {
59             transformer = schemalessTransformer;
60         } else if (isBaseRpc(type)) {
61             transformer = baseRpcTransformer;
62         } else {
63             return FluentFutures.immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException(
64                 "Unable to invoke rpc %s", type));
65         }
66         return handleRpc(type, input, transformer);
67     }
68
69     private FluentFuture<DOMRpcResult> handleRpc(
70             @Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input,
71             final MessageTransformer<NetconfMessage> transformer) {
72         final FluentFuture<RpcResult<NetconfMessage>> delegateFuture = listener.sendRequest(
73             transformer.toRpcRequest(type, input), type.getLastComponent());
74
75         final SettableFuture<DOMRpcResult> ret = SettableFuture.create();
76         delegateFuture.addCallback(new FutureCallback<RpcResult<NetconfMessage>>() {
77             @Override
78             public void onSuccess(RpcResult<NetconfMessage> result) {
79                 ret.set(result.isSuccessful() ? transformer.toRpcResult(result.getResult(), type)
80                         : new DefaultDOMRpcResult(result.getErrors()));
81             }
82
83             @Override
84             public void onFailure(Throwable cause) {
85                 ret.setException(new DOMRpcImplementationNotAvailableException(cause,
86                     "Unable to invoke rpc %s on device %s", type, deviceId));
87             }
88
89         }, MoreExecutors.directExecutor());
90         return ret;
91     }
92
93     private static boolean isBaseRpc(final SchemaPath type) {
94         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace());
95     }
96
97     @Nonnull
98     @Override
99     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T lsnr) {
100         throw new UnsupportedOperationException("Not available for netconf 1.0");
101     }
102 }