1 package org.opendaylight.controller.cluster.example;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import com.google.common.base.Optional;
6 import org.opendaylight.controller.cluster.example.messages.PrintRole;
7 import org.opendaylight.controller.cluster.example.messages.PrintState;
8 import org.opendaylight.controller.cluster.raft.ConfigParams;
10 import java.io.BufferedReader;
11 import java.io.InputStreamReader;
12 import java.util.HashMap;
14 import java.util.concurrent.ConcurrentHashMap;
17 * This is a test driver for testing akka-raft implementation
18 * Its uses ExampleActors and threads to push content(key-vals) to these actors
19 * Each ExampleActor can have one or more ClientActors. Each ClientActor spawns
20 * a thread and starts push logs to the actor its assigned to.
22 public class TestDriver {
24 private static final ActorSystem actorSystem = ActorSystem.create();
25 private static Map<String, String> allPeers = new HashMap<>();
26 private static Map<String, ActorRef> clientActorRefs = new HashMap<String, ActorRef>();
27 private static Map<String, ActorRef> actorRefs = new HashMap<String, ActorRef>();
28 private static LogGenerator logGenerator = new LogGenerator();
29 private int nameCounter = 0;
30 private static ConfigParams configParams = new ExampleConfigParamsImpl();
33 * Create nodes, add clients and start logging.
38 * reinstateNode:{nodeName}
40 * addClientsToNode:{nodeName, num}
43 * startLoggingForClient:{nodeName}
44 * stopLoggingForClient:{nodeName}
50 public static void main(String[] args) throws Exception {
51 TestDriver td = new TestDriver();
53 System.out.println("Enter command (type bye to exit):");
56 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
58 String command = br.readLine();
59 if (command.startsWith("bye")) {
62 } else if (command.startsWith("createNodes")) {
63 String[] arr = command.split(":");
64 int n = Integer.parseInt(arr[1]);
67 } else if (command.startsWith("addClients")) {
68 String[] arr = command.split(":");
69 int n = Integer.parseInt(arr[1]);
72 } else if (command.startsWith("addClientsToNode")) {
73 String[] arr = command.split(":");
74 String nodeName = arr[1];
75 int n = Integer.parseInt(arr[1]);
76 td.addClientsToNode(nodeName, n);
78 } else if (command.startsWith("stopNode")) {
79 String[] arr = command.split(":");
82 } else if (command.startsWith("reinstateNode")) {
83 String[] arr = command.split(":");
84 td.reinstateNode(arr[1]);
86 } else if (command.startsWith("startLogging")) {
89 } else if (command.startsWith("startLoggingForClient")) {
90 String[] arr = command.split(":");
91 td.startLoggingForClient(clientActorRefs.get(arr[1]));
93 } else if (command.startsWith("stopLogging")) {
96 } else if (command.startsWith("stopLoggingForClient")) {
97 String[] arr = command.split(":");
98 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
100 } else if (command.startsWith("printState")) {
102 } else if (command.startsWith("printNodes")) {
105 System.out.println("Invalid command:" + command);
111 public static ActorRef createExampleActor(String name) {
112 return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
113 Optional.of(configParams)), name);
116 public void createNodes(int num) {
117 for (int i=0; i < num; i++) {
118 nameCounter = nameCounter + 1;
119 allPeers.put("example-"+nameCounter, "akka://default/user/example-"+nameCounter);
122 for (String s : allPeers.keySet()) {
123 ActorRef exampleActor = createExampleActor(s);
124 actorRefs.put(s, exampleActor);
125 System.out.println("Created node:"+s);
130 // add num clients to all nodes in the system
131 public void addClients(int num) {
132 for(Map.Entry<String,ActorRef> actorRefEntry : actorRefs.entrySet()) {
133 for (int i=0; i < num; i++) {
134 String clientName = "client-" + i + "-" + actorRefEntry.getKey();
135 ActorRef clientActor = actorSystem.actorOf(
136 ClientActor.props(actorRefEntry.getValue()), clientName);
137 clientActorRefs.put(clientName, clientActor);
138 System.out.println("Created client-node:" + clientName);
143 // add num clients to a node
144 public void addClientsToNode(String actorName, int num) {
145 ActorRef actorRef = actorRefs.get(actorName);
146 for (int i=0; i < num; i++) {
147 String clientName = "client-" + i + "-" + actorName;
148 clientActorRefs.put(clientName,
149 actorSystem.actorOf(ClientActor.props(actorRef), clientName));
150 System.out.println("Added client-node:" + clientName);
154 public void stopNode(String actorName) {
155 ActorRef actorRef = actorRefs.get(actorName);
157 for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) {
158 if (entry.getKey().endsWith(actorName)) {
159 actorSystem.stop(entry.getValue());
163 actorSystem.stop(actorRef);
164 actorRefs.remove(actorName);
165 allPeers.remove(actorName);
168 public void reinstateNode(String actorName) {
169 String address = "akka://default/user/"+actorName;
170 allPeers.put(actorName, address);
172 ActorRef exampleActor = createExampleActor(actorName);
173 actorRefs.put(actorName, exampleActor);
175 addClientsToNode(actorName, 1);
178 public void startAllLogging() {
179 if(!clientActorRefs.isEmpty()) {
180 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
181 logGenerator.startLoggingForClient(client.getValue());
182 System.out.println("Started logging for client:"+client.getKey());
185 System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
190 public void startLoggingForClient(ActorRef client) {
191 logGenerator.startLoggingForClient(client);
194 public void stopAllLogging() {
195 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
196 logGenerator.stopLoggingForClient(client.getValue());
200 public void stopLoggingForClient(ActorRef client) {
201 logGenerator.stopLoggingForClient(client);
204 public void printState() {
205 for (ActorRef ref : actorRefs.values()) {
206 ref.tell(new PrintState(), null);
210 public void printNodes() {
211 for (ActorRef ref : actorRefs.values()) {
212 ref.tell(new PrintRole(), null);
216 public ActorRef getLeader() {
221 private static Map<String, String> withoutPeer(String peerId) {
222 Map<String, String> without = new ConcurrentHashMap<>(allPeers);
223 without.remove(peerId);