Bug 8153: Enforce check-style rules for netconf - sal-netconf-connector
[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 javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
21 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
22 import org.opendaylight.netconf.api.NetconfMessage;
23 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
25 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
26 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
27 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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     @Nonnull
56     @Override
57     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
58                                                                   @Nullable final NormalizedNode<?, ?> input) {
59         final MessageTransformer<NetconfMessage> transformer;
60         if (input instanceof AnyXmlNode) {
61             transformer = schemalessTransformer;
62         } else if (isBaseRpc(type)) {
63             transformer = baseRpcTransformer;
64         } else {
65             return Futures.immediateFailedCheckedFuture(
66                     new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
67         }
68         return handleRpc(type, input, transformer);
69     }
70
71     private CheckedFuture<DOMRpcResult, DOMRpcException> handleRpc(
72             @Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input,
73             final MessageTransformer<NetconfMessage> transformer) {
74         final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input);
75         final ListenableFuture<RpcResult<NetconfMessage>> rpcResultListenableFuture =
76                 listener.sendRequest(netconfMessage, type.getLastComponent());
77
78         final ListenableFuture<DOMRpcResult> transformed =
79             Futures.transform(rpcResultListenableFuture, (Function<RpcResult<NetconfMessage>, DOMRpcResult>) input1 -> {
80                 if (input1.isSuccessful()) {
81                     return transformer.toRpcResult(input1.getResult(), type);
82                 } else {
83                     return new DefaultDOMRpcResult(input1.getErrors());
84                 }
85             });
86
87         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
88             @Nullable
89             @Override
90             public DOMRpcException apply(@Nullable final Exception exception) {
91                 return new DOMRpcImplementationNotAvailableException(
92                         exception, "Unable to invoke rpc %s on device %s", type, deviceId);
93             }
94         });
95     }
96
97
98     private static boolean isBaseRpc(final SchemaPath type) {
99         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace());
100     }
101
102     @Nonnull
103     @Override
104     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
105             @Nonnull final T listener) {
106         throw new UnsupportedOperationException("Not available for netconf 1.0");
107     }
108
109 }