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
9 package org.opendaylight.netconf.topology.singleton.impl;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import com.google.common.base.Function;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.Collection;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException;
29 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
30 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
31 import org.opendaylight.netconf.topology.singleton.messages.SchemaPathMessage;
32 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage;
33 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
34 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import scala.concurrent.Future;
44 public class ProxyDOMRpcService implements DOMRpcService {
46 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManager.class);
48 private final ActorRef masterActorRef;
49 private final ActorSystem actorSystem;
50 private final RemoteDeviceId id;
52 public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
53 final RemoteDeviceId remoteDeviceId) {
54 this.actorSystem = actorSystem;
55 this.masterActorRef = masterActorRef;
61 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
62 @Nullable final NormalizedNode<?, ?> input) {
63 LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
65 final NormalizedNodeMessage normalizedNodeMessage =
66 new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input);
67 final Future<Object> scalaFuture =
68 Patterns.ask(masterActorRef,
69 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage),
70 NetconfTopologyUtils.TIMEOUT);
72 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
74 final CheckedFuture<DOMRpcResult, DOMRpcException> checkedFuture = Futures.makeChecked(settableFuture,
75 new Function<Exception, DOMRpcException>() {
79 public DOMRpcException apply(@Nullable final Exception exception) {
80 return new ClusteringRpcException(id + ": Exception during remote rpc invocation.",
85 scalaFuture.onComplete(new OnComplete<Object>() {
87 public void onComplete(final Throwable failure, final Object success) throws Throwable {
88 if (failure != null) {
89 settableFuture.setException(failure);
92 if (success instanceof Throwable) {
93 settableFuture.setException((Throwable) success);
96 if (success instanceof EmptyResultResponse || success == null) {
97 settableFuture.set(null);
100 final Collection<RpcError> errors = ((InvokeRpcMessageReply) success).getRpcErrors();
101 final NormalizedNodeMessage normalizedNodeMessageResult =
102 ((InvokeRpcMessageReply) success).getNormalizedNodeMessage();
103 final DOMRpcResult result;
104 if (normalizedNodeMessageResult == null ){
105 result = new DefaultDOMRpcResult(errors);
107 if (errors == null) {
108 result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode());
110 result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
113 settableFuture.set(result);
115 }, actorSystem.dispatcher());
117 return checkedFuture;
123 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
124 @Nonnull final T listener) {
126 throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");