--- /dev/null
+/*
+ * Copyright (c) 2014 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.datastore;
+
+import akka.actor.UntypedActor;
+import akka.event.Logging;
+import akka.event.LoggingAdapter;
+
+public abstract class AbstractUntypedActor extends UntypedActor {
+ protected final LoggingAdapter LOG =
+ Logging.getLogger(getContext().system(), this);
+
+ @Override public void onReceive(Object message) throws Exception {
+ LOG.debug("Received message {}", message);
+ handleReceive(message);
+ LOG.debug("Done handling message {}", message);
+ }
+
+ protected abstract void handleReceive(Object message) throws Exception;
+}
package org.opendaylight.controller.cluster.datastore;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.japi.Creator;
+import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
+import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-public class DataChangeListener extends UntypedActor {
- @Override public void onReceive(Object message) throws Exception {
- throw new UnsupportedOperationException("onReceive");
+public class DataChangeListener extends AbstractUntypedActor {
+ private final AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> listener;
+
+ public DataChangeListener(
+ AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> listener) {
+ this.listener = listener;
+ }
+
+ @Override public void handleReceive(Object message) throws Exception {
+ if(message instanceof DataChanged){
+ DataChanged reply = (DataChanged) message;
+ AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>>
+ change = reply.getChange();
+ this.listener.onDataChanged(change);
+
+ if(getSender() != null){
+ getSender().tell(new DataChangedReply(), getSelf());
+ }
+
+ }
}
- public static Props props() {
+ public static Props props(final AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> listener) {
return Props.create(new Creator<DataChangeListener>() {
@Override
public DataChangeListener create() throws Exception {
- return new DataChangeListener();
+ return new DataChangeListener(listener);
}
});
package org.opendaylight.controller.cluster.datastore;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.japi.Creator;
import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistrationReply;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-public class DataChangeListenerRegistration extends UntypedActor{
+public class DataChangeListenerRegistration extends AbstractUntypedActor{
private final org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration;
}
@Override
- public void onReceive(Object message) throws Exception {
+ public void handleReceive(Object message) throws Exception {
if(message instanceof CloseDataChangeListenerRegistration){
closeListenerRegistration((CloseDataChangeListenerRegistration) message);
}
AsyncDataBroker.DataChangeScope scope) {
ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
- DataChangeListener.props());
+ DataChangeListener.props(listener));
Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
new RegisterChangeListener(path, dataChangeListenerActor.path(),
import akka.actor.ActorRef;
import akka.actor.Address;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Creator;
* <li> When a local shard replica comes alive
* </p>
*/
-public class ShardManager extends UntypedActor {
+public class ShardManager extends AbstractUntypedActor {
// Stores a mapping between a shard name and the address of the current primary
private final Map<String, Address> shardNameToPrimaryAddress = new HashMap<>();
}
@Override
- public void onReceive(Object message) throws Exception {
+ public void handleReceive(Object message) throws Exception {
if (message instanceof FindPrimary) {
FindPrimary msg = ((FindPrimary) message);
String shardName = msg.getShardName();
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Creator;
* <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
* </p>
*/
-public class ShardTransaction extends UntypedActor {
+public class ShardTransaction extends AbstractUntypedActor {
private final ActorRef shardActor;
@Override
- public void onReceive(Object message) throws Exception {
- log.debug("Received message {}", message);
-
+ public void handleReceive(Object message) throws Exception {
if (message instanceof ReadData) {
readData((ReadData) message);
} else if (message instanceof WriteData) {
import akka.actor.ActorRef;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.japi.Creator;
import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
/**
* The ShardTransactionChain Actor represents a remote TransactionChain
*/
-public class ShardTransactionChain extends UntypedActor{
+public class ShardTransactionChain extends AbstractUntypedActor{
private final DOMStoreTransactionChain chain;
}
@Override
- public void onReceive(Object message) throws Exception {
+ public void handleReceive(Object message) throws Exception {
if(message instanceof CreateTransaction){
DOMStoreReadWriteTransaction transaction = chain.newReadWriteTransaction();
ActorRef transactionActor = getContext().actorOf(ShardTransaction.props(chain, transaction, getContext().parent()));
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
-import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.Creator;
import java.util.concurrent.ExecutionException;
-public class ThreePhaseCommitCohort extends UntypedActor {
+public class ThreePhaseCommitCohort extends AbstractUntypedActor {
private final DOMStoreThreePhaseCommitCohort cohort;
private final ActorRef shardActor;
private final CompositeModification modification;
@Override
- public void onReceive(Object message) throws Exception {
- log.debug("Received message {}", message);
-
+ public void handleReceive(Object message) throws Exception {
if (message instanceof CanCommitTransaction) {
canCommit((CanCommitTransaction) message);
} else if (message instanceof PreCommitTransaction) {
--- /dev/null
+/*
+ * Copyright (c) 2014 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.datastore.messages;
+
+public class DataChangedReply {
+}
--- /dev/null
+package org.opendaylight.controller.cluster.datastore;
+
+import akka.actor.ActorRef;
+import akka.actor.Props;
+import akka.testkit.JavaTestKit;
+import org.junit.Test;
+import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
+import org.opendaylight.controller.cluster.datastore.messages.DataChangedReply;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertTrue;
+
+public class DataChangeListenerTest extends AbstractActorTest {
+
+ private static class MockDataChangedEvent implements AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
+ throw new UnsupportedOperationException("getCreatedData");
+ }
+
+ @Override
+ public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
+ throw new UnsupportedOperationException("getUpdatedData");
+ }
+
+ @Override public Set<InstanceIdentifier> getRemovedPaths() {
+ throw new UnsupportedOperationException("getRemovedPaths");
+ }
+
+ @Override
+ public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
+ throw new UnsupportedOperationException("getOriginalData");
+ }
+
+ @Override public NormalizedNode<?, ?> getOriginalSubtree() {
+ throw new UnsupportedOperationException("getOriginalSubtree");
+ }
+
+ @Override public NormalizedNode<?, ?> getUpdatedSubtree() {
+ throw new UnsupportedOperationException("getUpdatedSubtree");
+ }
+ }
+
+ private class MockDataChangeListener implements AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> {
+ private boolean gotIt = false;
+
+ @Override public void onDataChanged(
+ AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
+ gotIt = true;
+ }
+
+ public boolean gotIt() {
+ return gotIt;
+ }
+ }
+
+ @Test
+ public void testDataChanged(){
+ new JavaTestKit(getSystem()) {{
+ final MockDataChangeListener listener = new MockDataChangeListener();
+ final Props props = DataChangeListener.props(listener);
+ final ActorRef subject =
+ getSystem().actorOf(props, "testDataChanged");
+
+ new Within(duration("1 seconds")) {
+ protected void run() {
+
+ subject.tell(
+ new DataChanged(new MockDataChangedEvent()),
+ getRef());
+
+ final Boolean out = new ExpectMsg<Boolean>("dataChanged") {
+ // do not put code outside this method, will run afterwards
+ protected Boolean match(Object in) {
+ if (in instanceof DataChangedReply) {
+ DataChangedReply reply =
+ (DataChangedReply) in;
+ return true;
+ } else {
+ throw noMatch();
+ }
+ }
+ }.get(); // this extracts the received message
+
+ assertTrue(out);
+ assertTrue(listener.gotIt());
+ // Will wait for the rest of the 3 seconds
+ expectNoMsg();
+ }
+
+
+ };
+ }};
+ }
+}