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