5ee3c499881e5a54bc0d6a7e4858a24edad1604b
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedPersistentActor.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.persistence.UntypedPersistentActor;
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 AbstractUntypedPersistentActor extends UntypedPersistentActor implements ExecuteInSelfActor {
19
20     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
21     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
22     @SuppressWarnings("checkstyle:MemberName")
23     protected final Logger LOG = LoggerFactory.getLogger(getClass());
24
25     protected AbstractUntypedPersistentActor() {
26         LOG.trace("Actor created {}", getSelf());
27         getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
28     }
29
30     @Override
31     public final void executeInSelf(@NonNull final Runnable runnable) {
32         final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
33         LOG.trace("Scheduling execution of {}", message);
34         self().tell(message, ActorRef.noSender());
35     }
36
37     @Override
38     public final void onReceiveCommand(final Object message) throws Exception {
39         final String messageType = message.getClass().getSimpleName();
40         LOG.trace("Received message {}", messageType);
41
42         if (message instanceof ExecuteInSelfMessage) {
43             LOG.trace("Executing {}", message);
44             ((ExecuteInSelfMessage) message).run();
45         } else {
46             handleCommand(message);
47         }
48
49         LOG.trace("Done handling message {}", messageType);
50     }
51
52     @Override
53     public final void onReceiveRecover(final Object message) throws Exception {
54         final String messageType = message.getClass().getSimpleName();
55         LOG.trace("Received message {}", messageType);
56         handleRecover(message);
57         LOG.trace("Done handling message {}", messageType);
58     }
59
60     protected abstract void handleRecover(Object message) throws Exception;
61
62     protected abstract void handleCommand(Object message) throws Exception;
63
64     protected void ignoreMessage(final Object message) {
65         LOG.debug("Unhandled message {} ", message);
66     }
67
68     protected void unknownMessage(final Object message) {
69         LOG.debug("Received unhandled message {}", message);
70         unhandled(message);
71     }
72 }