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