Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcBroker.java
1 /*
2  * Copyright (c) 2014 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.controller.remote.rpc;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.japi.Creator;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Throwables;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
25 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
26 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Actor to initiate execution of remote RPC on other nodes of the cluster.
37  */
38
39 public class RpcBroker extends AbstractUntypedActor {
40
41     private static final Logger LOG = LoggerFactory.getLogger(RpcBroker.class);
42     private final DOMRpcService rpcService;
43
44     private RpcBroker(final DOMRpcService rpcService) {
45         this.rpcService = rpcService;
46     }
47
48     public static Props props(final DOMRpcService rpcService) {
49         Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null");
50         return Props.create(new RpcBrokerCreator(rpcService));
51     }
52
53     @Override
54     protected void handleReceive(final Object message) throws Exception {
55         if (message instanceof ExecuteRpc) {
56             executeRpc((ExecuteRpc) message);
57         }
58     }
59
60     private void executeRpc(final ExecuteRpc msg) {
61         LOG.debug("Executing rpc {}", msg.getRpc());
62         final NormalizedNode<?, ?> input = RemoteRpcInput.from(msg.getInputNormalizedNode());
63         final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
64         final ActorRef sender = getSender();
65         final ActorRef self = self();
66
67         try {
68             final CheckedFuture<DOMRpcResult, DOMRpcException> future = rpcService.invokeRpc(schemaPath, input);
69
70             Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
71                 @Override
72                 public void onSuccess(final DOMRpcResult result) {
73                     if (result.getErrors() != null && !result.getErrors().isEmpty()) {
74                         final String message = String.format("Execution of RPC %s failed", msg.getRpc());
75                         Collection<RpcError> errors = result.getErrors();
76                         if (errors == null || errors.size() == 0) {
77                             errors = Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, null, message));
78                         }
79
80                         sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, errors)), self);
81                     } else {
82                         LOG.debug("Sending response for execute rpc : {}", msg.getRpc());
83
84                         sender.tell(new RpcResponse(result.getResult()), self);
85                     }
86                 }
87
88                 @Override
89                 public void onFailure(final Throwable t) {
90                     LOG.error("executeRpc for {} failed with root cause: {}. For exception details, enable Debug logging.",
91                         msg.getRpc(), Throwables.getRootCause(t));
92                     if(LOG.isDebugEnabled()) {
93                         LOG.debug("Detailed exception for execute RPC failure :{}", t);
94                     }
95                     sender.tell(new akka.actor.Status.Failure(t), self);
96                 }
97             });
98         } catch (final Exception e) {
99             sender.tell(new akka.actor.Status.Failure(e), sender);
100         }
101     }
102
103     private static class RpcBrokerCreator implements Creator<RpcBroker> {
104         private static final long serialVersionUID = 1L;
105
106         final DOMRpcService rpcService;
107
108         RpcBrokerCreator(final DOMRpcService rpcService) {
109             this.rpcService = rpcService;
110         }
111
112         @Override
113         public RpcBroker create() throws Exception {
114             return new RpcBroker(rpcService);
115         }
116     }
117 }