Merge "Bug 2486: Get testAbortBeforeFinishCommit working again"
authorMoiz Raja <moraja@cisco.com>
Tue, 20 Jan 2015 02:49:06 +0000 (02:49 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 20 Jan 2015 02:49:07 +0000 (02:49 +0000)
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardManagerTest.java
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java

index a7c3db4fc246bcce77d23e8d324a4301d930568e..132e09d6de41e689ad0aef5f60de977f3783b196 100644 (file)
@@ -379,15 +379,18 @@ public abstract class RaftActor extends AbstractUntypedPersistentActor {
         if (oldBehavior != currentBehavior){
             onStateChanged();
         }
-        if (oldBehavior != null) {
-            // it can happen that the state has not changed but the leader has changed.
-            onLeaderChanged(oldBehavior.getLeaderId(), currentBehavior.getLeaderId());
-
-            if (getRoleChangeNotifier().isPresent() && oldBehavior.state() != currentBehavior.state()) {
-                // we do not want to notify when the behavior/role is set for the first time (i.e follower)
-                getRoleChangeNotifier().get().tell(new RoleChanged(getId(), oldBehavior.state().name(),
-                    currentBehavior.state().name()), getSelf());
-            }
+
+        String oldBehaviorLeaderId = oldBehavior == null? null : oldBehavior.getLeaderId();
+        String oldBehaviorState = oldBehavior == null? null : oldBehavior.state().name();
+
+        // it can happen that the state has not changed but the leader has changed.
+        onLeaderChanged(oldBehaviorLeaderId, currentBehavior.getLeaderId());
+
+        if (getRoleChangeNotifier().isPresent() &&
+                (oldBehavior == null || (oldBehavior.state() != currentBehavior.state()))) {
+            getRoleChangeNotifier().get().tell(
+                    new RoleChanged(getId(), oldBehaviorState , currentBehavior.state().name()),
+                    getSelf());
         }
     }
 
index c833a86e9b825c926002ce5fc5b163438a6a4ce0..d999bb2ba1de79e59d65b5cd78614bc8b3fc2c7c 100644 (file)
@@ -883,24 +883,27 @@ public class RaftActorTest extends AbstractActorTest {
             TestActorRef<MockRaftActor> mockActorRef = TestActorRef.create(getSystem(), MockRaftActor.props(id,
                 Collections.<String,String>emptyMap(), Optional.<ConfigParams>of(config), notifierActor), id);
 
-            MockRaftActor mockRaftActor = mockActorRef.underlyingActor();
-            mockRaftActor.setCurrentBehavior(new Follower(mockRaftActor.getRaftActorContext()));
-
             // sleeping for a minimum of 2 seconds, if it spans more its fine.
             Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
 
             List<Object> matches =  MessageCollectorActor.getAllMatching(notifierActor, RoleChanged.class);
             assertNotNull(matches);
-            assertEquals(2, matches.size());
+            assertEquals(3, matches.size());
 
-            // check if the notifier got a role change from Follower to Candidate
+            // check if the notifier got a role change from null to Follower
             RoleChanged raftRoleChanged = (RoleChanged) matches.get(0);
             assertEquals(id, raftRoleChanged.getMemberId());
+            assertNull(raftRoleChanged.getOldRole());
+            assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
+
+            // check if the notifier got a role change from Follower to Candidate
+            raftRoleChanged = (RoleChanged) matches.get(1);
+            assertEquals(id, raftRoleChanged.getMemberId());
             assertEquals(RaftState.Follower.name(), raftRoleChanged.getOldRole());
             assertEquals(RaftState.Candidate.name(), raftRoleChanged.getNewRole());
 
             // check if the notifier got a role change from Candidate to Leader
-            raftRoleChanged = (RoleChanged) matches.get(1);
+            raftRoleChanged = (RoleChanged) matches.get(2);
             assertEquals(id, raftRoleChanged.getMemberId());
             assertEquals(RaftState.Candidate.name(), raftRoleChanged.getOldRole());
             assertEquals(RaftState.Leader.name(), raftRoleChanged.getNewRole());
index e70d79c61a884abd7cef597b0bc43d3781b49eea..8c56efd41325e70911dfa644746803b4a8aae61a 100644 (file)
@@ -110,8 +110,6 @@ public class ShardManagerTest extends AbstractActorTest {
         new JavaTestKit(getSystem()) {{
             final ActorRef shardManager = getSystem().actorOf(newShardMgrProps());
 
-            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
-
             shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false).toSerializable(), getRef());
 
             expectMsgClass(duration("5 seconds"), ActorNotInitialized.class);
@@ -174,8 +172,6 @@ public class ShardManagerTest extends AbstractActorTest {
         new JavaTestKit(getSystem()) {{
             final ActorRef shardManager = getSystem().actorOf(newShardMgrProps());
 
-            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
-
             shardManager.tell(new FindLocalShard(Shard.DEFAULT_NAME, false), getRef());
 
             expectMsgClass(duration("5 seconds"), ActorNotInitialized.class);
index e24500a76ce57ff5a7b607dec7a7e208990c35c3..dd5124649331ccd56b18f45406465cca3de66fb1 100644 (file)
@@ -891,10 +891,6 @@ public class RestconfImpl implements RestconfService {
             final DataNodeContainer parentSchema = (DataNodeContainer) incompleteInstIdWithData.getSchemaNode();
             DOMMountPoint mountPoint = incompleteInstIdWithData.getMountPoint();
             final Module module = findModule(mountPoint, payload);
-            if (module == null) {
-                throw new RestconfDocumentedException("Module was not found for \"" + payloadNS + "\"",
-                        ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
-            }
 
             String payloadName = this.getName(payload);
             final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace(
@@ -941,11 +937,6 @@ public class RestconfImpl implements RestconfService {
         }
 
         final Module module = this.findModule(null, payload);
-        if (module == null) {
-            throw new RestconfDocumentedException(
-                    "Data has bad format. Root element node has incorrect namespace (XML format) or module name(JSON format)",
-                    ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE);
-        }
 
         String payloadName = this.getName(payload);
         final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace(module,
@@ -1112,19 +1103,23 @@ public class RestconfImpl implements RestconfService {
     }
 
     private Module findModule(final DOMMountPoint mountPoint, final Node<?> data) {
+        Module module = null;
         if (data instanceof NodeWrapper) {
-            return findModule(mountPoint, (NodeWrapper<?>) data);
+            module = findModule(mountPoint, (NodeWrapper<?>) data);
         } else if (data != null) {
             URI namespace = data.getNodeType().getNamespace();
             if (mountPoint != null) {
-                return this.controllerContext.findModuleByNamespace(mountPoint, namespace);
+                module = this.controllerContext.findModuleByNamespace(mountPoint, namespace);
             } else {
-                return this.controllerContext.findModuleByNamespace(namespace);
+                module = this.controllerContext.findModuleByNamespace(namespace);
             }
-        } else {
-            throw new IllegalArgumentException("Unhandled parameter types: "
-                    + Arrays.<Object> asList(mountPoint, data).toString());
         }
+        if (module != null) {
+            return module;
+        }
+        throw new RestconfDocumentedException(
+                "Data has bad format. Root element node has incorrect namespace (XML format) or module name(JSON format)",
+                ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE);
     }
 
     private Module findModule(final DOMMountPoint mountPoint, final NodeWrapper<?> data) {