1 package org.opendaylight.controller.cluster.example;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import org.opendaylight.controller.cluster.example.messages.PrintRole;
6 import org.opendaylight.controller.cluster.example.messages.PrintState;
7 import org.opendaylight.controller.cluster.raft.client.messages.AddRaftPeer;
8 import org.opendaylight.controller.cluster.raft.client.messages.RemoveRaftPeer;
10 import java.io.BufferedReader;
11 import java.io.InputStreamReader;
12 import java.util.HashMap;
14 import java.util.Random;
15 import java.util.concurrent.ConcurrentHashMap;
18 public class TestDriver {
20 private static final ActorSystem actorSystem = ActorSystem.create();
21 private static Map<String, String> allPeers = new HashMap<>();
22 private static Map<String, ActorRef> clientActorRefs = new HashMap<String, ActorRef>();
23 private static Map<String, ActorRef> actorRefs = new HashMap<String, ActorRef>();
24 private static LogGenerator logGenerator = new LogGenerator();;
27 * Create nodes, add clients and start logging.
34 * addClientsToNode:{nodeName, num}
37 * startLoggingForClient:{nodeName}
38 * stopLoggingForClient:{nodeName}
44 public static void main(String[] args) throws Exception {
45 TestDriver td = new TestDriver();
47 System.out.println("Enter command (type bye to exit):");
50 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
52 String command = br.readLine();
53 if (command.startsWith("bye")) {
56 } else if (command.startsWith("createNodes")) {
57 String[] arr = command.split(":");
58 int n = Integer.parseInt(arr[1]);
61 } else if (command.startsWith("addNodes")) {
62 String[] arr = command.split(":");
63 int n = Integer.parseInt(arr[1]);
66 } else if (command.startsWith("addClients")) {
67 String[] arr = command.split(":");
68 int n = Integer.parseInt(arr[1]);
71 } else if (command.startsWith("addClientsToNode")) {
72 String[] arr = command.split(":");
73 String nodeName = arr[1];
74 int n = Integer.parseInt(arr[1]);
75 td.addClientsToNode(nodeName, n);
77 } else if (command.startsWith("stopNode")) {
78 String[] arr = command.split(":");
81 } else if (command.startsWith("startLogging")) {
84 } else if (command.startsWith("startLoggingForClient")) {
85 String[] arr = command.split(":");
86 td.startLoggingForClient(clientActorRefs.get(arr[1]));
88 } else if (command.startsWith("stopLogging")) {
91 } else if (command.startsWith("stopLoggingForClient")) {
92 String[] arr = command.split(":");
93 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
95 } else if (command.startsWith("printState")) {
97 } else if (command.startsWith("printNodes")) {
104 public void createNodes(int num) {
105 for (int i=0; i < num; i++) {
106 int rand = getUnusedRandom(num);
107 allPeers.put("example-"+rand, "akka://default/user/example-"+rand);
110 for (String s : allPeers.keySet()) {
111 ActorRef exampleActor = actorSystem.actorOf(
112 ExampleActor.props(s, withoutPeer(s)), s);
113 actorRefs.put(s, exampleActor);
114 System.out.println("Created node:"+s);
119 // add new nodes , pass in the count
120 public void addNodes(int num) {
121 Map<String, String> newPeers = new HashMap<>();
122 for (int i=0; i < num; i++) {
123 int rand = getUnusedRandom(num);
124 newPeers.put("example-"+rand, "akka://default/user/example-"+rand);
125 allPeers.put("example-"+rand, "akka://default/user/example-"+rand);
128 Map<String, ActorRef> newActorRefs = new HashMap<String, ActorRef>(num);
129 for (Map.Entry<String, String> entry : newPeers.entrySet()) {
130 ActorRef exampleActor = actorSystem.actorOf(
131 ExampleActor.props(entry.getKey(), withoutPeer(entry.getKey())), entry.getKey());
132 newActorRefs.put(entry.getKey(), exampleActor);
134 //now also add these new nodes as peers from the previous nodes
135 for (ActorRef actor : actorRefs.values()) {
136 actor.tell(new AddRaftPeer(entry.getKey(), entry.getValue()), null);
139 System.out.println("Added node:" + entry);
142 actorRefs.putAll(newActorRefs);
146 // add num clients to all nodes in the system
147 public void addClients(int num) {
148 for(Map.Entry<String,ActorRef> actorRefEntry : actorRefs.entrySet()) {
149 for (int i=0; i < num; i++) {
150 String clientName = "client-" + i + "-" + actorRefEntry.getKey();
151 ActorRef clientActor = actorSystem.actorOf(
152 ClientActor.props(actorRefEntry.getValue()), clientName);
153 clientActorRefs.put(clientName, clientActor);
154 System.out.println("Created client-node:" + clientName);
159 // add num clients to a node
160 public void addClientsToNode(String actorName, int num) {
161 ActorRef actorRef = actorRefs.get(actorName);
162 for (int i=0; i < num; i++) {
163 String clientName = "client-" + i + "-" + actorRef;
164 clientActorRefs.put(clientName,
165 actorSystem.actorOf(ClientActor.props(actorRef), clientName));
166 System.out.println("Added client-node:" + clientName);
170 public void stopNode(String actorName) {
171 ActorRef actorRef = actorRefs.get(actorName);
172 String clientName = "client-"+actorName;
173 if(clientActorRefs.containsKey(clientName)) {
174 actorSystem.stop(clientActorRefs.get(clientName));
175 clientActorRefs.remove(clientName);
177 actorSystem.stop(actorRef);
178 actorRefs.remove(actorName);
180 for (ActorRef actor : actorRefs.values()) {
181 actor.tell(new RemoveRaftPeer(actorName), null);
184 allPeers.remove(actorName);
188 public void startAllLogging() {
189 if(!clientActorRefs.isEmpty()) {
190 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
191 logGenerator.startLoggingForClient(client.getValue());
192 System.out.println("Started logging for client:"+client.getKey());
195 System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
200 public void startLoggingForClient(ActorRef client) {
201 logGenerator.startLoggingForClient(client);
204 public void stopAllLogging() {
205 for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
206 logGenerator.stopLoggingForClient(client.getValue());
210 public void stopLoggingForClient(ActorRef client) {
211 logGenerator.stopLoggingForClient(client);
214 public void printState() {
215 for (ActorRef ref : actorRefs.values()) {
216 ref.tell(new PrintState(), null);
220 public void printNodes() {
221 for (ActorRef ref : actorRefs.values()) {
222 ref.tell(new PrintRole(), null);
226 public ActorRef getLeader() {
230 private int getUnusedRandom(int num) {
233 rand = (new Random()).nextInt(num * num);
234 } while (allPeers.keySet().contains("example-"+rand));
239 private static Map<String, String> withoutPeer(String peerId) {
240 Map<String, String> without = new ConcurrentHashMap<>(allPeers);
241 without.remove(peerId);