bug 2266 : added more types of schema nodes to increase code coverage
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / TestDriver.java
index 18c14e71241c10f803b22990234548d60f60eea4..de6169791ed4cb4405e6f47e85e0ed3535155fd1 100644 (file)
@@ -2,10 +2,10 @@ package org.opendaylight.controller.cluster.example;
 
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
+import com.google.common.base.Optional;
 import org.opendaylight.controller.cluster.example.messages.PrintRole;
 import org.opendaylight.controller.cluster.example.messages.PrintState;
-import org.opendaylight.controller.cluster.raft.client.messages.AddRaftPeer;
-import org.opendaylight.controller.cluster.raft.client.messages.RemoveRaftPeer;
+import org.opendaylight.controller.cluster.raft.ConfigParams;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
@@ -27,13 +27,13 @@ public class TestDriver {
     private static Map<String, ActorRef> actorRefs = new HashMap<String, ActorRef>();
     private static LogGenerator logGenerator = new LogGenerator();
     private int nameCounter = 0;
+    private static ConfigParams configParams = new ExampleConfigParamsImpl();
 
     /**
      * Create nodes, add clients and start logging.
      * Commands
      *  bye
      *  createNodes:{num}
-     *  addNodes:{num}
      *  stopNode:{nodeName}
      *  reinstateNode:{nodeName}
      *  addClients:{num}
@@ -44,6 +44,11 @@ public class TestDriver {
      *  stopLoggingForClient:{nodeName}
      *  printNodes
      *  printState
+     *
+     *  Note: when run on IDE and on debug log level, the debug logs in
+     *  AbstractUptypedActor and AbstractUptypedPersistentActor would need to be commented out.
+     *  Also RaftActor handleCommand(), debug log which prints for every command other than AE/AER
+     *
      * @param args
      * @throws Exception
      */
@@ -64,11 +69,6 @@ public class TestDriver {
                 int n = Integer.parseInt(arr[1]);
                 td.createNodes(n);
 
-            } else if (command.startsWith("addNodes")) {
-                String[] arr = command.split(":");
-                int n = Integer.parseInt(arr[1]);
-                td.addNodes(n);
-
             } else if (command.startsWith("addClients")) {
                 String[] arr = command.split(":");
                 int n = Integer.parseInt(arr[1]);
@@ -106,11 +106,18 @@ public class TestDriver {
                 td.printState();
             } else if (command.startsWith("printNodes")) {
                 td.printNodes();
+            } else {
+                System.out.println("Invalid command:" + command);
             }
 
         }
     }
 
+    public static ActorRef createExampleActor(String name) {
+        return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
+            Optional.of(configParams)), name);
+    }
+
     public void createNodes(int num) {
         for (int i=0; i < num; i++)  {
             nameCounter = nameCounter + 1;
@@ -118,41 +125,13 @@ public class TestDriver {
         }
 
         for (String s : allPeers.keySet())  {
-            ActorRef exampleActor = actorSystem.actorOf(
-                ExampleActor.props(s, withoutPeer(s)), s);
+            ActorRef exampleActor = createExampleActor(s);
             actorRefs.put(s, exampleActor);
             System.out.println("Created node:"+s);
 
         }
     }
 
-    // add new nodes , pass in the count
-    public void addNodes(int num) {
-        Map<String, String> newPeers = new HashMap<>();
-        for (int i=0; i < num; i++)  {
-            nameCounter = nameCounter + 1;
-            newPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
-            allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
-
-        }
-        Map<String, ActorRef> newActorRefs = new HashMap<String, ActorRef>(num);
-        for (Map.Entry<String, String> entry : newPeers.entrySet())  {
-            ActorRef exampleActor = actorSystem.actorOf(
-                ExampleActor.props(entry.getKey(), withoutPeer(entry.getKey())), entry.getKey());
-            newActorRefs.put(entry.getKey(), exampleActor);
-
-            //now also add these new nodes as peers from the previous nodes
-            for (ActorRef actor : actorRefs.values()) {
-                actor.tell(new AddRaftPeer(entry.getKey(), entry.getValue()), null);
-            }
-
-            System.out.println("Added node:" + entry);
-        }
-
-        actorRefs.putAll(newActorRefs);
-    }
-
-
     // add num clients to all nodes in the system
     public void addClients(int num) {
         for(Map.Entry<String,ActorRef> actorRefEntry : actorRefs.entrySet()) {
@@ -188,11 +167,6 @@ public class TestDriver {
 
         actorSystem.stop(actorRef);
         actorRefs.remove(actorName);
-
-        for (ActorRef actor : actorRefs.values()) {
-            actor.tell(new RemoveRaftPeer(actorName), null);
-        }
-
         allPeers.remove(actorName);
     }
 
@@ -200,12 +174,7 @@ public class TestDriver {
         String address = "akka://default/user/"+actorName;
         allPeers.put(actorName, address);
 
-        ActorRef exampleActor = actorSystem.actorOf(ExampleActor.props(actorName, withoutPeer(actorName)), actorName);
-
-        for (ActorRef actor : actorRefs.values()) {
-            actor.tell(new AddRaftPeer(actorName, address), null);
-        }
-
+        ActorRef exampleActor = createExampleActor(actorName);
         actorRefs.put(actorName, exampleActor);
 
         addClientsToNode(actorName, 1);