Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteDOMRpcFuture.java
1 /*
2  * Copyright (c) 2015 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.controller.remote.rpc;
9
10 import akka.dispatch.OnComplete;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.AbstractFuture;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.ExecutionContext;
27 import scala.concurrent.Future;
28
29 /**
30  * @author tony
31  *
32  */
33 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
36
37     private final QName rpcName;
38
39     private RemoteDOMRpcFuture(final QName rpcName) {
40         this.rpcName = Preconditions.checkNotNull(rpcName,"rpcName");
41     }
42
43     public static RemoteDOMRpcFuture create(final QName rpcName) {
44         return new RemoteDOMRpcFuture(rpcName);
45     }
46
47     protected void failNow(final Throwable error) {
48         LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
49         setException(error);
50     }
51
52     protected void completeWith(final Future<Object> future) {
53         future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
54     }
55
56     @Override
57     public DOMRpcResult checkedGet() throws DOMRpcException {
58         try {
59             return get();
60         } catch (final ExecutionException e) {
61             throw mapException(e);
62         } catch (final InterruptedException e) {
63             throw Throwables.propagate(e);
64         }
65     }
66
67     @Override
68     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
69         try {
70             return get(timeout, unit);
71         } catch (final ExecutionException e) {
72             throw mapException(e);
73         } catch (final InterruptedException e) {
74             throw Throwables.propagate(e);
75         }
76     }
77
78     private DOMRpcException mapException(final ExecutionException e) {
79         final Throwable cause = e.getCause();
80         if (cause instanceof DOMRpcException) {
81             return (DOMRpcException) cause;
82         }
83         return new RemoteDOMRpcException("Exception during invoking RPC", e);
84     }
85
86     private final class FutureUpdater extends OnComplete<Object> {
87
88         @Override
89         public void onComplete(final Throwable error, final Object reply) throws Throwable {
90             if (error != null) {
91                 RemoteDOMRpcFuture.this.failNow(error);
92             } else if (reply instanceof RpcResponse) {
93                 final RpcResponse rpcReply = (RpcResponse) reply;
94                 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
95
96                 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
97
98                 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
99
100                 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
101             } else {
102                 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply
103                         + "from Akka"));
104             }
105         }
106     }
107
108 }