f80aae4fe2d922c85e8d9dc9841b87ff56e9c495
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementation.java
1 /*
2  * Copyright (c) 2014, 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
9 package org.opendaylight.controller.remote.rpc;
10
11 import static akka.pattern.Patterns.ask;
12
13 import akka.actor.ActorRef;
14 import akka.dispatch.OnComplete;
15 import akka.japi.Pair;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.List;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
24 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
25 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
26 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.FindRoutersReply;
27 import org.opendaylight.controller.remote.rpc.utils.LatestEntryRoutingLogic;
28 import org.opendaylight.controller.sal.connector.api.RpcRouter;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.ExecutionContext;
33 import scala.concurrent.Future;
34
35 public class RemoteRpcImplementation implements DOMRpcImplementation {
36     private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcImplementation.class);
37
38     private final ActorRef rpcRegistry;
39     private final RemoteRpcProviderConfig config;
40
41     public RemoteRpcImplementation(final ActorRef rpcRegistry, final RemoteRpcProviderConfig config) {
42         this.config = config;
43         this.rpcRegistry = rpcRegistry;
44     }
45
46     @Override
47     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final DOMRpcIdentifier rpc,
48             final NormalizedNode<?, ?> input) {
49         if (input instanceof RemoteRpcInput) {
50             LOG.warn("Rpc {} was removed during execution or there is loop present. Failing received rpc.", rpc);
51             return Futures
52                     .<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException(
53                             "Rpc implementation for {} was removed during processing.", rpc));
54         }
55         final RemoteDOMRpcFuture frontEndFuture = RemoteDOMRpcFuture.create(rpc.getType().getLastComponent());
56         findRouteAsync(rpc).onComplete(new OnComplete<FindRoutersReply>() {
57
58             @Override
59             public void onComplete(final Throwable error, final FindRoutersReply routes) throws Throwable {
60                 if (error != null) {
61                     frontEndFuture.failNow(error);
62                 } else {
63                     final List<Pair<ActorRef, Long>> routePairs = routes.getRouterWithUpdateTime();
64                     if (routePairs == null || routePairs.isEmpty()) {
65                         frontEndFuture.failNow(new DOMRpcImplementationNotAvailableException(
66                                 "No local or remote implementation available for rpc %s", rpc.getType(), error));
67                     } else {
68                         final ActorRef remoteImplRef = new LatestEntryRoutingLogic(routePairs).select();
69                         final Object executeRpcMessage = ExecuteRpc.from(rpc, input);
70                         LOG.debug("Found remote actor {} for rpc {} - sending {}", remoteImplRef, rpc.getType(), executeRpcMessage);
71                         frontEndFuture.completeWith(ask(remoteImplRef, executeRpcMessage, config.getAskDuration()));
72                     }
73                 }
74             }
75         }, ExecutionContext.Implicits$.MODULE$.global());
76         return frontEndFuture;
77     }
78
79     @SuppressWarnings({"unchecked", "rawtypes"})
80     private Future<FindRoutersReply> findRouteAsync(final DOMRpcIdentifier rpc) {
81         // FIXME: Refactor routeId and message to use DOMRpcIdentifier directly.
82         final RpcRouter.RouteIdentifier<?, ?, ?> routeId =
83                 new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), rpc.getContextReference());
84         final RpcRegistry.Messages.FindRouters findMsg = new RpcRegistry.Messages.FindRouters(routeId);
85         return (Future) ask(rpcRegistry, findMsg, config.getAskDuration());
86     }
87 }