f972e4568454e32207e24980d97aa2f6ab6db527
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / ProxyDOMRpcService.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.singleton.impl;
10
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.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import scala.concurrent.Future;
42
43 public class ProxyDOMRpcService implements DOMRpcService {
44
45     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManager.class);
46
47     private final ActorRef masterActorRef;
48     private final ActorSystem actorSystem;
49     private final RemoteDeviceId id;
50     private final Timeout actorResponseWaitTime;
51
52     public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
53                               final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
54         this.actorSystem = actorSystem;
55         this.masterActorRef = masterActorRef;
56         id = remoteDeviceId;
57         this.actorResponseWaitTime = actorResponseWaitTime;
58     }
59
60     @Nonnull
61     @Override
62     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
63                                                                   @Nullable final NormalizedNode<?, ?> input) {
64         LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
65
66         final NormalizedNodeMessage normalizedNodeMessage =
67                 new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input);
68         final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
69                 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
70
71         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
72
73         scalaFuture.onComplete(new OnComplete<Object>() {
74             @Override
75             public void onComplete(final Throwable failure, final Object response) {
76                 if (failure != null) {
77                     settableFuture.setException(failure);
78                     return;
79                 }
80
81                 if (response instanceof EmptyResultResponse) {
82                     settableFuture.set(null);
83                     return;
84                 }
85
86                 final Collection<RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
87                 final NormalizedNodeMessage normalizedNodeMessageResult =
88                         ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
89                 final DOMRpcResult result;
90                 if (normalizedNodeMessageResult == null) {
91                     result = new DefaultDOMRpcResult(errors);
92                 } else {
93                     result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
94                 }
95                 settableFuture.set(result);
96             }
97         }, actorSystem.dispatcher());
98
99         return Futures.makeChecked(settableFuture,
100             ex -> new ClusteringRpcException(id + ": Exception during remote rpc invocation.", ex));
101     }
102
103     @Nonnull
104     @Override
105     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
106             @Nonnull final T listener) {
107         // NOOP, only proxy
108         throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");
109     }
110 }