108b33ad14f9bf39807f7bd435678df622817c93
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
25 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
26 import org.opendaylight.controller.netconf.api.NetconfMessage;
27 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
28 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36
37 /**
38  * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
39  */
40 public final class NetconfDeviceRpc implements DOMRpcService {
41
42     private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER = new Function<RpcDefinition, DOMRpcIdentifier>() {
43         @Override
44         public DOMRpcIdentifier apply(final RpcDefinition input) {
45             // TODO add support for routed rpcs ... is it necessary in this case ?
46             return DOMRpcIdentifier.create(input.getPath());
47         }
48     };
49
50     private final RemoteDeviceCommunicator<NetconfMessage> listener;
51     private final MessageTransformer<NetconfMessage> transformer;
52     private final Collection<DOMRpcIdentifier> availableRpcs;
53
54     public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
55         this.listener = listener;
56         this.transformer = transformer;
57
58         availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
59     }
60
61     @Nonnull
62     @Override
63     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
64         Preconditions.checkArgument(input instanceof ContainerNode, "Epc payload has to be a %s, was %s", ContainerNode.class, input);
65
66         final NetconfMessage message = transformer.toRpcRequest(type, (ContainerNode) input);
67         final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(message, type.getLastComponent());
68
69         final ListenableFuture<DOMRpcResult> transformed = 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                     // TODO check whether the listener sets errors properly
76                     return new DefaultDOMRpcResult(input.getErrors());
77                 }
78             }
79         });
80
81         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
82             @Nullable
83             @Override
84             public DOMRpcException apply(@Nullable final Exception e) {
85                 // FIXME what other possible exceptions are there ?
86                 return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s", type);
87             }
88         });
89     }
90
91     @Nonnull
92     @Override
93     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
94
95         listener.onRpcAvailable(availableRpcs);
96
97         return new ListenerRegistration<T>() {
98             @Override
99             public void close() {
100                 // NOOP, no rpcs appear and disappear in this implementation
101             }
102
103             @Override
104             public T getInstance() {
105                 return listener;
106             }
107         };
108     }
109 }