Restart downed nodes.
[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 package org.opendaylight.controller.cluster.common.actor;
9
10 import akka.actor.ActorRef;
11 import akka.persistence.AbstractPersistentActor;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public abstract class AbstractUntypedPersistentActor extends AbstractPersistentActor 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     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
21     @SuppressWarnings("checkstyle:MemberName")
22     protected final Logger LOG = LoggerFactory.getLogger(getClass());
23
24     protected AbstractUntypedPersistentActor() {
25         LOG.trace("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         LOG.trace("Scheduling execution of {}", message);
33         self().tell(message, ActorRef.noSender());
34     }
35
36     @Override
37     public final Receive createReceive() {
38         return receiveBuilder()
39                 .match(ExecuteInSelfMessage.class, ExecuteInSelfMessage::run)
40                 .matchAny(this::handleCommand)
41                 .build();
42     }
43
44     @Override
45     public final Receive createReceiveRecover() {
46         return receiveBuilder().matchAny(this::handleRecover).build();
47     }
48
49     protected abstract void handleRecover(Object message) throws Exception;
50
51     protected abstract void handleCommand(Object message) throws Exception;
52
53     protected void ignoreMessage(final Object message) {
54         LOG.debug("Unhandled message {} ", message);
55     }
56
57     protected void unknownMessage(final Object message) {
58         LOG.debug("Received unhandled message {}", message);
59         unhandled(message);
60     }
61 }