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.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;
36 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
38 public final class NetconfDeviceRpc implements DOMRpcService {
40 private static final Function<RpcDefinition, DOMRpcIdentifier> RPC_TO_RPC_IDENTIFIER = new Function<RpcDefinition, DOMRpcIdentifier>() {
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());
48 private final RemoteDeviceCommunicator<NetconfMessage> listener;
49 private final MessageTransformer<NetconfMessage> transformer;
50 private final Collection<DOMRpcIdentifier> availableRpcs;
52 public NetconfDeviceRpc(final SchemaContext schemaContext, final RemoteDeviceCommunicator<NetconfMessage> listener, final MessageTransformer<NetconfMessage> transformer) {
53 this.listener = listener;
54 this.transformer = transformer;
56 availableRpcs = Collections2.transform(schemaContext.getOperations(), RPC_TO_RPC_IDENTIFIER);
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());
65 final ListenableFuture<DOMRpcResult> transformed = Futures.transform(delegateFutureWithPureResult, new Function<RpcResult<NetconfMessage>, DOMRpcResult>() {
67 public DOMRpcResult apply(final RpcResult<NetconfMessage> input) {
68 if (input.isSuccessful()) {
69 return transformer.toRpcResult(input.getResult(), type);
71 // TODO check whether the listener sets errors properly
72 return new DefaultDOMRpcResult(input.getErrors());
77 return Futures.makeChecked(transformed, new Function<Exception, DOMRpcException>() {
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);
89 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(@Nonnull final T listener) {
91 listener.onRpcAvailable(availableRpcs);
93 return new ListenerRegistration<T>() {
96 // NOOP, no rpcs appear and disappear in this implementation
100 public T getInstance() {