CDS: Add stress test RPC to the cars model
[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.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.controller.netconf.api.NetconfMessage;
26 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
27 import org.opendaylight.controller.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 = new Function<RpcDefinition, DOMRpcIdentifier>() {
41         @Override
42         public DOMRpcIdentifier apply(final RpcDefinition input) {
43             // TODO add support for routed rpcs ... is it necessary in this case ?
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, final MessageTransformer<NetconfMessage> transformer) {
53         this.listener = listener;
54         this.transformer = transformer;
55
56         availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
57     }
58
59     @Nonnull
60     @Override
61     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type, @Nullable final NormalizedNode<?, ?> input) {
62         final NetconfMessage message = transformer.toRpcRequest(type, input);
63         final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(message, type.getLastComponent());
64
65         final ListenableFuture<DOMRpcResult> transformed = Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
66             @Override
67             public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
68                 if (input.isSuccessful()) {
69                     return transformer.toRpcResult(input.getResult(), type);
70                 } else {
71                     // TODO check whether the listener sets errors properly
72                     return new DefaultDOMRpcResult(input.getErrors());
73                 }
74             }
75         });
76
77         return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
78             @Nullable
79             @Override
80             public DOMRpcException apply(@Nullable final Exception e) {
81                 // FIXME what other possible exceptions are there ?
82                 return new DOMRpcImplementationNotAvailableException(e, "Unable to invoke rpc %s", type);
83             }
84         });
85     }
86
87     @Nonnull
88     @Override
89     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
90
91         listener.onRpcAvailable(availableRpcs);
92
93         return new ListenerRegistration<T>() {
94             @Override
95             public void close() {
96                 // NOOP, no rpcs appear and disappear in this implementation
97             }
98
99             @Override
100             public T getInstance() {
101                 return listener;
102             }
103         };
104     }
105 }