33fa7b96e7443f83e4e0bb1fe133c5b4699a8657
[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.base.Function;
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.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;
43
44 public class ProxyDOMRpcService implements DOMRpcService {
45
46     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManager.class);
47
48     private final ActorRef masterActorRef;
49     private final ActorSystem actorSystem;
50     private final RemoteDeviceId id;
51     private final Timeout actorResponseWaitTime;
52
53     public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
54                               final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
55         this.actorSystem = actorSystem;
56         this.masterActorRef = masterActorRef;
57         id = remoteDeviceId;
58         this.actorResponseWaitTime = actorResponseWaitTime;
59     }
60
61     @Nonnull
62     @Override
63     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
64                                                                   @Nullable final NormalizedNode<?, ?> input) {
65         LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
66
67         final NormalizedNodeMessage normalizedNodeMessage =
68                 new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input);
69         final Future<Object> scalaFuture =
70                 Patterns.ask(masterActorRef,
71                         new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage),
72                         actorResponseWaitTime);
73
74         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
75
76         final CheckedFuture<DOMRpcResult, DOMRpcException> checkedFuture = Futures.makeChecked(settableFuture,
77                 new Function<Exception, DOMRpcException>() {
78
79                 @Nullable
80                 @Override
81                 public DOMRpcException apply(@Nullable final Exception exception) {
82                     return new ClusteringRpcException(id + ": Exception during remote rpc invocation.",
83                             exception);
84                 }
85             });
86
87         scalaFuture.onComplete(new OnComplete<Object>() {
88             @Override
89             public void onComplete(final Throwable failure, final Object success) throws Throwable {
90                 if (failure != null) {
91                     settableFuture.setException(failure);
92                     return;
93                 }
94                 if (success instanceof Throwable) {
95                     settableFuture.setException((Throwable) success);
96                     return;
97                 }
98                 if (success instanceof EmptyResultResponse || success == null) {
99                     settableFuture.set(null);
100                     return;
101                 }
102                 final Collection<RpcError> errors = ((InvokeRpcMessageReply) success).getRpcErrors();
103                 final NormalizedNodeMessage normalizedNodeMessageResult =
104                         ((InvokeRpcMessageReply) success).getNormalizedNodeMessage();
105                 final DOMRpcResult result;
106                 if (normalizedNodeMessageResult == null) {
107                     result = new DefaultDOMRpcResult(errors);
108                 } else {
109                     if (errors == null) {
110                         result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode());
111                     } else {
112                         result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
113                     }
114                 }
115                 settableFuture.set(result);
116             }
117         }, actorSystem.dispatcher());
118
119         return checkedFuture;
120
121     }
122
123     @Nonnull
124     @Override
125     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
126             @Nonnull final T listener) {
127         // NOOP, only proxy
128         throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");
129     }
130 }