2 * Copyright (c) 2016 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.topology.singleton.impl;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.dispatch.OnComplete;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Collection;
19 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
22 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
23 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
24 import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.netconf.topology.singleton.messages.SchemaPathMessage;
27 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage;
28 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.concurrent.Future;
39 public class ProxyDOMRpcService implements Rpcs.Normalized {
40 private static final Logger LOG = LoggerFactory.getLogger(ProxyDOMRpcService.class);
42 private final ActorRef masterActorRef;
43 private final ActorSystem actorSystem;
44 private final RemoteDeviceId id;
45 private final Timeout actorResponseWaitTime;
47 public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
48 final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
49 this.actorSystem = actorSystem;
50 this.masterActorRef = masterActorRef;
52 this.actorResponseWaitTime = actorResponseWaitTime;
56 public ListenableFuture<DOMRpcResult> invokeRpc(final QName type, final ContainerNode input) {
57 LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
59 final NormalizedNodeMessage normalizedNodeMessage = input != null
60 ? new NormalizedNodeMessage(YangInstanceIdentifier.of(), input) : null;
61 final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
62 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
64 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
66 scalaFuture.onComplete(new OnComplete<>() {
68 public void onComplete(final Throwable failure, final Object response) {
69 if (failure != null) {
70 if (failure instanceof ClusteringRpcException) {
71 settableFuture.setException(failure);
73 settableFuture.setException(
74 new ClusteringRpcException(id + ": Exception during remote rpc invocation.", failure));
79 if (response instanceof EmptyResultResponse) {
80 settableFuture.set(null);
84 final Collection<? extends RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
85 final NormalizedNodeMessage normalizedNodeMessageResult =
86 ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
87 final DOMRpcResult result;
88 if (normalizedNodeMessageResult == null) {
89 result = new DefaultDOMRpcResult(ImmutableList.copyOf(errors));
91 result = new DefaultDOMRpcResult((ContainerNode) normalizedNodeMessageResult.getNode(), errors);
93 settableFuture.set(result);
95 }, actorSystem.dispatcher());
97 return settableFuture;
101 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
103 throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");