2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.connect.netconf.sal;
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;
38 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
40 public final class NetconfDeviceRpc implements DOMRpcService {
42 private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER = new Function<RpcDefinition, DOMRpcIdentifier>() {
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());
50 private final RemoteDeviceCommunicator<NetconfMessage> listener;
51 private final MessageTransformer<NetconfMessage> transformer;
52 private final Collection<DOMRpcIdentifier> availableRpcs;
54 public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
55 this.listener = listener;
56 this.transformer = transformer;
58 availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
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);
66 final NetconfMessage message = transformer.toRpcRequest(type, (ContainerNode) input);
67 final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult = listener.sendRequest(message, type.getLastComponent());
69 final ListenableFuture<DOMRpcResult> transformed = Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
71 public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
72 if (input.isSuccessful()) {
73 return transformer.toRpcResult(input.getResult(), type);
75 // TODO check whether the listener sets errors properly
76 return new DefaultDOMRpcResult(input.getErrors());
81 return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
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);
93 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
95 listener.onRpcAvailable(availableRpcs);
97 return new ListenerRegistration<T>() {
100 // NOOP, no rpcs appear and disappear in this implementation
104 public T getInstance() {