cd9311b9c32ada42988e6d96d63530beefdabf5b
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceRpc.java
1 /*
2  * Copyright (c) 2014 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.collect.Collections2;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collection;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
24 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 NetconfDeviceRpc implements DOMRpcService {
39
40     private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER =
41         new Function<RpcDefinition, DOMRpcIdentifier>() {
42             @Override
43             public DOMRpcIdentifier apply(final RpcDefinition input) {
44                 return DOMRpcIdentifier.create(input.getPath());
45             }
46         };
47
48     private final RemoteDeviceCommunicator<NetconfMessage> listener;
49     private final MessageTransformer<NetconfMessage> transformer;
50     private final Collection<DOMRpcIdentifier> availableRpcs;
51
52     public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener,
53                             final MessageTransformer<NetconfMessage> transformer) {
54         this.listener = listener;
55         this.transformer = transformer;
56
57         availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
58     }
59
60     @Nonnull
61     @Override
62     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
63                                                                   @Nullable final NormalizedNode<?, ?> input) {
64         final NetconfMessage message = transformer.toRpcRequest(type, input);
65         final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult =
66                 listener.sendRequest(message, type.getLastComponent());
67
68         final ListenableFuture<DOMRpcResult> transformed =
69             Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
70                 @Override
71                 public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
72                     if (input.isSuccessful()) {
73                         return transformer.toRpcResult(input.getResult(), type);
74                     } else {
75                         return new DefaultDOMRpcResult(input.getErrors());
76                     }
77                 }
78             });
79
80         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
81             @Nullable
82             @Override
83             public DOMRpcException apply(@Nullable final Exception exception) {
84                 return new DOMRpcImplementationNotAvailableException(exception, "Unable to invoke rpc %s", type);
85             }
86         });
87     }
88
89     @Nonnull
90     @Override
91     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
92             @Nonnull final T listener) {
93
94         listener.onRpcAvailable(availableRpcs);
95
96         return new ListenerRegistration<T>() {
97             @Override
98             public void close() {
99                 // NOOP, no rpcs appear and disappear in this implementation
100             }
101
102             @Override
103             public T getInstance() {
104                 return listener;
105             }
106         };
107     }
108 }