Simplify QuarantinedMonitorActor
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / QuarantinedMonitorActor.java
1 /*
2  * Copyright (c) 2015 Huawei Technologies Co., Ltd. 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.Props;
12 import akka.actor.UntypedActor;
13 import akka.japi.Effect;
14 import akka.remote.ThisActorSystemQuarantinedEvent;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * This class listens to Akka RemotingLifecycleEvent events to detect when this node has been
20  * quarantined by another. Once this node gets quarantined, restart the ActorSystem to allow this
21  * node to rejoin the cluster.
22  *
23  * @author Gary Wu gary.wu1@huawei.com
24  *
25  */
26 public class QuarantinedMonitorActor extends UntypedActor {
27
28     private static final Logger LOG = LoggerFactory.getLogger(QuarantinedMonitorActor.class);
29
30     public static final String ADDRESS = "quarantined-monitor";
31
32     private final Effect callback;
33     private boolean quarantined;
34
35     protected QuarantinedMonitorActor(final Effect callback) {
36         this.callback = callback;
37
38         LOG.debug("Created QuarantinedMonitorActor");
39
40         getContext().system().eventStream().subscribe(getSelf(), ThisActorSystemQuarantinedEvent.class);
41     }
42
43     @Override
44     public void postStop() {
45         LOG.debug("Stopping QuarantinedMonitorActor");
46     }
47
48     @Override
49     public void onReceive(final Object message) throws Exception {
50         final String messageType = message.getClass().getSimpleName();
51         LOG.trace("onReceive {} {}", messageType, message);
52
53         // check to see if we got quarantined by another node
54         if (quarantined) {
55             return;
56         }
57
58         if (message instanceof ThisActorSystemQuarantinedEvent) {
59             final ThisActorSystemQuarantinedEvent event = (ThisActorSystemQuarantinedEvent) message;
60             LOG.warn("Got quarantined by {}", event.remoteAddress());
61             quarantined = true;
62
63             // execute the callback
64             callback.apply();
65         }
66     }
67
68     public static Props props(final Effect callback) {
69         return Props.create(QuarantinedMonitorActor.class, callback);
70     }
71 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.