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.collect.ImmutableList;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.util.Collection;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
27 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException;
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.util.concurrent.ExceptionMapper;
37 import org.opendaylight.yangtools.yang.common.RpcError;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import scala.concurrent.Future;
45 public class ProxyDOMRpcService implements DOMRpcService {
47 private static final Logger LOG = LoggerFactory.getLogger(ProxyDOMRpcService.class);
49 private final ExceptionMapper<DOMRpcException> domRpcExceptionMapper =
50 new ExceptionMapper<DOMRpcException>("invokeRpc", DOMRpcException.class) {
52 protected DOMRpcException newWithCause(final String message, final Throwable cause) {
53 return new ClusteringRpcException(id + ": Exception during remote rpc invocation.", cause);
57 private final ActorRef masterActorRef;
58 private final ActorSystem actorSystem;
59 private final RemoteDeviceId id;
60 private final Timeout actorResponseWaitTime;
62 public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
63 final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
64 this.actorSystem = actorSystem;
65 this.masterActorRef = masterActorRef;
67 this.actorResponseWaitTime = actorResponseWaitTime;
72 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
73 @Nullable final NormalizedNode<?, ?> input) {
74 LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
76 final NormalizedNodeMessage normalizedNodeMessage = input != null
77 ? new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input) : null;
78 final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
79 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
81 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
83 scalaFuture.onComplete(new OnComplete<Object>() {
85 public void onComplete(final Throwable failure, final Object response) {
86 if (failure != null) {
87 settableFuture.setException(failure);
91 if (response instanceof EmptyResultResponse) {
92 settableFuture.set(null);
96 final Collection<? extends RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
97 final NormalizedNodeMessage normalizedNodeMessageResult =
98 ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
99 final DOMRpcResult result;
100 if (normalizedNodeMessageResult == null) {
101 result = new DefaultDOMRpcResult(ImmutableList.copyOf(errors));
103 result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
105 settableFuture.set(result);
107 }, actorSystem.dispatcher());
109 return Futures.makeChecked(settableFuture, domRpcExceptionMapper);
114 public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
115 @Nonnull final T listener) {
117 throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");