Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[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.base.Function;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
22 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.netconf.api.NetconfMessage;
24 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
26 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
27 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
28 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35
36 /**
37  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
38  */
39 public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
40
41     private final RemoteDeviceCommunicator<NetconfMessage> listener;
42     private final BaseRpcSchemalessTransformer baseRpcTransformer;
43     private final SchemalessMessageTransformer schemalessTransformer;
44     private final RemoteDeviceId deviceId;
45
46     public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
47                                       final RemoteDeviceCommunicator<NetconfMessage> listener,
48                                       final BaseRpcSchemalessTransformer baseRpcTransformer,
49                                       final SchemalessMessageTransformer messageTransformer) {
50         this.deviceId = deviceId;
51         this.listener = listener;
52         this.baseRpcTransformer = baseRpcTransformer;
53         this.schemalessTransformer = messageTransformer;
54     }
55
56     @Nonnull
57     @Override
58     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
59                                                                   @Nullable final NormalizedNode<?, ?> input) {
60         final MessageTransformer<NetconfMessage> transformer;
61         if (input instanceof AnyXmlNode) {
62             transformer = schemalessTransformer;
63         } else if (isBaseRpc(type)) {
64             transformer = baseRpcTransformer;
65         } else {
66             return Futures.immediateFailedCheckedFuture(
67                     new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
68         }
69         return handleRpc(type, input, transformer);
70     }
71
72     private CheckedFuture<DOMRpcResult, DOMRpcException> handleRpc(
73             @Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input,
74             final MessageTransformer<NetconfMessage> transformer) {
75         final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input);
76         final ListenableFuture<RpcResult<NetconfMessage>> rpcResultListenableFuture =
77                 listener.sendRequest(netconfMessage, type.getLastComponent());
78
79         final ListenableFuture<DOMRpcResult> transformed =
80             Futures.transform(rpcResultListenableFuture, (Function<RpcResult<NetconfMessage>, DOMRpcResult>) input1 -> {
81                 if (input1.isSuccessful()) {
82                     return transformer.toRpcResult(input1.getResult(), type);
83                 } else {
84                     return new DefaultDOMRpcResult(input1.getErrors());
85                 }
86             }, MoreExecutors.directExecutor());
87
88         return Futures.makeChecked(transformed,
89             e -> new DOMRpcImplementationNotAvailableException(e,
90                 "Unable to invoke rpc %s on device %s", type, deviceId));
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
103 }