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.netconf.sal.connect.netconf.sal;
10 import com.google.common.collect.Collections2;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
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.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 * Invokes RPC by sending netconf message via listener. Also transforms result from NetconfMessage to CompositeNode.
37 public final class NetconfDeviceRpc implements DOMRpcService {
39 private final RemoteDeviceCommunicator<NetconfMessage> communicator;
40 private final MessageTransformer<NetconfMessage> transformer;
41 private final Collection<DOMRpcIdentifier> availableRpcs;
43 public NetconfDeviceRpc(final SchemaContext schemaContext,
44 final RemoteDeviceCommunicator<NetconfMessage> communicator,
45 final MessageTransformer<NetconfMessage> transformer) {
46 this.communicator = communicator;
47 this.transformer = transformer;
49 availableRpcs = Collections2.transform(schemaContext.getOperations(),
50 input -> DOMRpcIdentifier.create(input.getPath()));
55 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
56 @Nullable final NormalizedNode<?, ?> input) {
57 final NetconfMessage message = transformer.toRpcRequest(type, input);
58 final ListenableFuture<RpcResult<NetconfMessage>> delegateFutureWithPureResult =
59 communicator.sendRequest(message, type.getLastComponent());
61 final ListenableFuture<DOMRpcResult> transformed =
62 Futures.transform(delegateFutureWithPureResult, input1 -> {
63 if (input1.isSuccessful()) {
64 return transformer.toRpcResult(input1.getResult(), type);
66 return new DefaultDOMRpcResult(input1.getErrors());
68 }, MoreExecutors.directExecutor());
70 return Futures.makeChecked(transformed, exception ->
71 new DOMRpcImplementationNotAvailableException(exception, "Unable to invoke rpc %s", type));
76 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
77 @Nonnull final T listener) {
79 listener.onRpcAvailable(availableRpcs);
81 return new ListenerRegistration<T>() {
84 // NOOP, no rpcs appear and disappear in this implementation
88 public T getInstance() {