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 akka.util.Timeout;
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.messages.NormalizedNodeMessage;
30 import org.opendaylight.netconf.topology.singleton.messages.SchemaPathMessage;
31 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage;
32 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
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 ExceptionMapper<DOMRpcException> domRpcExceptionMapper =
49 new ExceptionMapper<DOMRpcException>("invokeRpc", DOMRpcException.class) {
51 protected DOMRpcException newWithCause(String message, Throwable cause) {
52 return new ClusteringRpcException(id + ": Exception during remote rpc invocation.", cause);
56 private final ActorRef masterActorRef;
57 private final ActorSystem actorSystem;
58 private final RemoteDeviceId id;
59 private final Timeout actorResponseWaitTime;
61 public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
62 final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
63 this.actorSystem = actorSystem;
64 this.masterActorRef = masterActorRef;
66 this.actorResponseWaitTime = actorResponseWaitTime;
71 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
72 @Nullable final NormalizedNode<?, ?> input) {
73 LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
75 final NormalizedNodeMessage normalizedNodeMessage = input != null
76 ? new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input) : null;
77 final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
78 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
80 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
82 scalaFuture.onComplete(new OnComplete<Object>() {
84 public void onComplete(final Throwable failure, final Object response) {
85 if (failure != null) {
86 settableFuture.setException(failure);
90 if (response instanceof EmptyResultResponse) {
91 settableFuture.set(null);
95 final Collection<RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
96 final NormalizedNodeMessage normalizedNodeMessageResult =
97 ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
98 final DOMRpcResult result;
99 if (normalizedNodeMessageResult == null) {
100 result = new DefaultDOMRpcResult(errors);
102 result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
104 settableFuture.set(result);
106 }, actorSystem.dispatcher());
108 return Futures.makeChecked(settableFuture, domRpcExceptionMapper);
113 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
114 @Nonnull final T listener) {
116 throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");