Merge "Remove unused isMaster variable in ClusteredNetconfDevice"
[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.MessageCounter;
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 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
40  */
41 public final class SchemalessNetconfDeviceRpc implements DOMRpcService {
42
43     private final RemoteDeviceCommunicator<NetconfMessage> listener;
44     private final BaseRpcSchemalessTransformer baseRpcTransformer;
45     private final SchemalessMessageTransformer schemalessTransformer;
46     private final RemoteDeviceId deviceId;
47
48     public SchemalessNetconfDeviceRpc(final RemoteDeviceId deviceId,
49                                       final RemoteDeviceCommunicator<NetconfMessage> listener) {
50         this.deviceId = deviceId;
51         this.listener = listener;
52         final MessageCounter counter = new MessageCounter();
53         baseRpcTransformer = new BaseRpcSchemalessTransformer(counter);
54         schemalessTransformer = new SchemalessMessageTransformer(counter);
55     }
56
57     @Nonnull
58     @Override
59     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
60                                                                   @Nullable final NormalizedNode<?, ?> input) {
61         final MessageTransformer<NetconfMessage> transformer;
62         if (input instanceof AnyXmlNode) {
63             transformer = schemalessTransformer;
64         } else if (isBaseRpc(type)) {
65             transformer = baseRpcTransformer;
66         } else {
67             return Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
68         }
69         return handleRpc(type, input, transformer);
70     }
71
72     private CheckedFuture<DOMRpcResult, DOMRpcException> handleRpc(@Nonnull final SchemaPath type,
73                                                                    @Nullable final NormalizedNode<?, ?> input,
74                                                                    final MessageTransformer<NetconfMessage> transformer) {
75         final NetconfMessage netconfMessage = transformer.toRpcRequest(type, input);
76         final ListenableFuture<RpcResult<NetconfMessage>> rpcResultListenableFuture = listener.sendRequest(netconfMessage, type.getLastComponent());
77
78         final ListenableFuture<DOMRpcResult> transformed = Futures.transform(rpcResultListenableFuture, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
79             @Override
80             public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
81                 if (input.isSuccessful()) {
82                     return transformer.toRpcResult(input.getResult(), type);
83                 } else {
84                     return new DefaultDOMRpcResult(input.getErrors());
85                 }
86             }
87         });
88
89         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
90             @Nullable
91             @Override
92             public DOMRpcException apply(@Nullable final Exception e) {
93                 return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s on device %s", type, deviceId);
94             }
95         });
96     }
97
98
99     private boolean isBaseRpc(final SchemaPath type) {
100         return NetconfMessageTransformUtil.NETCONF_URI.equals(type.getLastComponent().getNamespace());
101     }
102
103     @Nonnull
104     @Override
105     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
106         throw new UnsupportedOperationException("Not available for netconf 1.0");
107     }
108
109 }