Migrate YangInstanceIdentifier.EMPTY users
[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 package org.opendaylight.netconf.topology.singleton.impl;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.dispatch.OnComplete;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.FluentFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Collection;
19 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.impl.utils.ClusteringRpcException;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.netconf.topology.singleton.messages.SchemaPathMessage;
27 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage;
28 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.concurrent.Future;
38
39 public class ProxyDOMRpcService implements DOMRpcService {
40     private static final Logger LOG = LoggerFactory.getLogger(ProxyDOMRpcService.class);
41
42     private final ActorRef masterActorRef;
43     private final ActorSystem actorSystem;
44     private final RemoteDeviceId id;
45     private final Timeout actorResponseWaitTime;
46
47     public ProxyDOMRpcService(final ActorSystem actorSystem, final ActorRef masterActorRef,
48                               final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime) {
49         this.actorSystem = actorSystem;
50         this.masterActorRef = masterActorRef;
51         id = remoteDeviceId;
52         this.actorResponseWaitTime = actorResponseWaitTime;
53     }
54
55     @Override
56     public FluentFuture<DOMRpcResult> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
57         LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
58
59         final NormalizedNodeMessage normalizedNodeMessage = input != null
60                 ? new NormalizedNodeMessage(YangInstanceIdentifier.empty(), input) : null;
61         final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
62                 new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
63
64         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
65
66         scalaFuture.onComplete(new OnComplete<Object>() {
67             @Override
68             public void onComplete(final Throwable failure, final Object response) {
69                 if (failure != null) {
70                     if (failure instanceof ClusteringRpcException) {
71                         settableFuture.setException(failure);
72                     } else {
73                         settableFuture.setException(
74                                 new ClusteringRpcException(id + ": Exception during remote rpc invocation.", failure));
75                     }
76                     return;
77                 }
78
79                 if (response instanceof EmptyResultResponse) {
80                     settableFuture.set(null);
81                     return;
82                 }
83
84                 final Collection<? extends RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
85                 final NormalizedNodeMessage normalizedNodeMessageResult =
86                         ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
87                 final DOMRpcResult result;
88                 if (normalizedNodeMessageResult == null) {
89                     result = new DefaultDOMRpcResult(ImmutableList.copyOf(errors));
90                 } else {
91                     result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
92                 }
93                 settableFuture.set(result);
94             }
95         }, actorSystem.dispatcher());
96
97         return FluentFuture.from(settableFuture);
98     }
99
100     @Override
101     public <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(final T listener) {
102         // NOOP, only proxy
103         throw new UnsupportedOperationException("RegisterRpcListener: DOMRpc service not working in cluster.");
104     }
105 }