X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fsharding%2FRoleChangeListenerActor.java;fp=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fsharding%2FRoleChangeListenerActor.java;h=38b69e720aeadf4597b8fbe7dfb1571bd792505a;hp=0000000000000000000000000000000000000000;hb=d69af1f79ae4630be8c4d65b98096aa27b1665b6;hpb=0c05dff15e4f36c5ecbd26e82309de21f67c8cd5 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/RoleChangeListenerActor.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/RoleChangeListenerActor.java new file mode 100644 index 0000000000..38b69e720a --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/RoleChangeListenerActor.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.cluster.sharding; + +import akka.actor.ActorRef; +import akka.actor.Props; +import com.google.common.base.Preconditions; +import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor; +import org.opendaylight.controller.cluster.dom.api.LeaderLocation; +import org.opendaylight.controller.cluster.dom.api.LeaderLocationListener; +import org.opendaylight.controller.cluster.notifications.LeaderStateChanged; +import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener; +import org.opendaylight.controller.cluster.notifications.RoleChangeNotification; + +/** + * Proxy actor which acts as a facade for user-provided + * {@link LeaderLocationListener}. It subscribes for {@link LeaderStateChanged} + * notifications in its pre start hook and translates them to + * {@link LeaderLocationListener#onLeaderLocationChanged(LeaderLocation)} + * events. + */ +public class RoleChangeListenerActor extends AbstractUntypedActor { + private final LeaderLocationListener leaderLocationListener; + private final ActorRef roleChangeNotifier; + + private RoleChangeListenerActor(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) { + this.roleChangeNotifier = Preconditions.checkNotNull(roleChangeNotifier); + this.leaderLocationListener = Preconditions.checkNotNull(listener); + } + + @Override + public void preStart() throws Exception { + super.preStart(); + roleChangeNotifier.tell(new RegisterRoleChangeListener(), getSelf()); + } + + @Override + protected void handleReceive(final Object message) throws Exception { + if (message instanceof RoleChangeNotification) { + ignoreMessage(message); + } else if (message instanceof LeaderStateChanged) { + onLeaderStateChanged((LeaderStateChanged) message); + } else { + unknownMessage(message); + } + } + + private void onLeaderStateChanged(final LeaderStateChanged message) { + final LeaderLocation newLocation; + if (message.getLeaderId() == null) { + newLocation = LeaderLocation.UNKNOWN; + } else if (message.getMemberId().equals(message.getLeaderId())) { + newLocation = LeaderLocation.LOCAL; + } else { + newLocation = LeaderLocation.REMOTE; + } + + // TODO should we wrap this in try catch block? + leaderLocationListener.onLeaderLocationChanged(newLocation); + } + + public static Props props(final ActorRef roleChangeNotifier, final LeaderLocationListener listener) { + return Props.create(RoleChangeListenerActor.class, roleChangeNotifier, listener); + } +}