Restart downed nodes.
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / ExplicitAsk.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.controller.cluster.common.actor;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.pattern.ExplicitAskSupport;
13 import akka.util.Timeout;
14 import com.google.common.annotations.Beta;
15 import java.util.function.Function;
16 import scala.Function1;
17 import scala.concurrent.Future;
18 import scala.runtime.AbstractFunction1;
19
20 /**
21  * Unfortunately Akka's explicit ask pattern does not work with its Java API, as it fails to invoke passed message.
22  * In order to make this work for now, we tap directly into ExplicitAskSupport and use a Scala function instead
23  * of akka.japi.Function.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class ExplicitAsk {
29     private static final ExplicitAskSupport ASK_SUPPORT = akka.pattern.extended.package$.MODULE$;
30
31     private ExplicitAsk() {
32         throw new UnsupportedOperationException();
33     }
34
35     public static <T> Function1<ActorRef, T> toScala(final Function<ActorRef, T> function) {
36         return new AbstractFunction1<ActorRef, T>() {
37             @Override
38             public T apply(final ActorRef askSender) {
39                 return function.apply(askSender);
40             }
41         };
42     }
43
44     @SuppressWarnings("unchecked")
45     public static Future<Object> ask(final ActorRef actor, final Function1<ActorRef, ?> function,
46             final Timeout timeout) {
47         return ASK_SUPPORT.ask(actor, (Function1<ActorRef, Object>)function, timeout);
48     }
49
50     @SuppressWarnings("unchecked")
51     public static Future<Object> ask(final ActorSelection actor, final Function1<ActorRef, ?> function,
52             final Timeout timeout) {
53         return ASK_SUPPORT.ask(actor, (Function1<ActorRef, Object>)function, timeout);
54     }
55
56     public static Future<Object> ask(final ActorRef actor, final Function<ActorRef, ?> function,
57             final Timeout timeout) {
58         return ask(actor, toScala(function), timeout);
59     }
60
61     public static Future<Object> ask(final ActorSelection actor, final Function<ActorRef, ?> function,
62             final Timeout timeout) {
63         return ask(actor, toScala(function), timeout);
64     }
65 }