c0e260ae66a9f2dcc6c73e254449da1167f403ff
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedActor.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.cluster.common.actor;
10
11 import akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public abstract class AbstractUntypedActor extends UntypedActor implements ExecuteInSelfActor {
19     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
20     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
21     @SuppressWarnings("checkstyle:MemberName")
22     protected final Logger LOG = LoggerFactory.getLogger(getClass());
23
24     protected AbstractUntypedActor() {
25         LOG.debug("Actor created {}", getSelf());
26         getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
27     }
28
29     @Override
30     public final void executeInSelf(@NonNull final Runnable runnable) {
31         final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
32         self().tell(message, ActorRef.noSender());
33     }
34
35     @Override
36     public final void onReceive(final Object message) {
37         if (message instanceof ExecuteInSelfMessage) {
38             ((ExecuteInSelfMessage) message).run();
39         } else {
40             handleReceive(message);
41         }
42     }
43
44     /**
45      * Receive and handle an incoming message. If the implementation does not handle this particular message,
46      * it should call {@link #ignoreMessage(Object)} or {@link #unknownMessage(Object)}.
47      *
48      * @param message the incoming message
49      */
50     protected abstract void handleReceive(Object message);
51
52     protected final void ignoreMessage(final Object message) {
53         LOG.debug("Ignoring unhandled message {}", message);
54     }
55
56     protected final void unknownMessage(final Object message) {
57         LOG.debug("Received unhandled message {}", message);
58         unhandled(message);
59     }
60
61     protected boolean isValidSender(final ActorRef sender) {
62         // If the caller passes in a null sender (ActorRef.noSender()), akka translates that to the
63         // deadLetters actor.
64         return sender != null && !getContext().system().deadLetters().equals(sender);
65     }
66 }