Merge "Optimize RESTCONF module/node parsing"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / TestDriver.java
1 package org.opendaylight.controller.cluster.example;
2
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;
9
10 import java.io.BufferedReader;
11 import java.io.InputStreamReader;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Random;
15 import java.util.concurrent.ConcurrentHashMap;
16
17
18 public class TestDriver {
19
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();;
25
26     /**
27      * Create nodes, add clients and start logging.
28      * Commands
29      *  bye
30      *  createNodes:{num}
31      *  addNodes:{num}
32      *  stopNode:{nodeName}
33      *  addClients:{num}
34      *  addClientsToNode:{nodeName, num}
35      *  startLogging
36      *  stopLogging
37      *  startLoggingForClient:{nodeName}
38      *  stopLoggingForClient:{nodeName}
39      *  printNodes
40      *  printState
41      * @param args
42      * @throws Exception
43      */
44     public static void main(String[] args) throws Exception {
45         TestDriver td = new TestDriver();
46
47         System.out.println("Enter command (type bye to exit):");
48
49
50         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
51         while(true) {
52             String command = br.readLine();
53             if (command.startsWith("bye")) {
54                 System.exit(0);
55
56             } else if (command.startsWith("createNodes")) {
57                 String[] arr = command.split(":");
58                 int n = Integer.parseInt(arr[1]);
59                 td.createNodes(n);
60
61             } else if (command.startsWith("addNodes")) {
62                 String[] arr = command.split(":");
63                 int n = Integer.parseInt(arr[1]);
64                 td.addNodes(n);
65
66             } else if (command.startsWith("addClients")) {
67                 String[] arr = command.split(":");
68                 int n = Integer.parseInt(arr[1]);
69                 td.addClients(n);
70
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);
76
77             } else if (command.startsWith("stopNode")) {
78                 String[] arr = command.split(":");
79                 td.stopNode(arr[1]);
80
81             } else if (command.startsWith("startLogging")) {
82                 td.startAllLogging();
83
84             } else if (command.startsWith("startLoggingForClient")) {
85                 String[] arr = command.split(":");
86                 td.startLoggingForClient(clientActorRefs.get(arr[1]));
87
88             } else if (command.startsWith("stopLogging")) {
89                 td.stopAllLogging();
90
91             } else if (command.startsWith("stopLoggingForClient")) {
92                 String[] arr = command.split(":");
93                 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
94
95             } else if (command.startsWith("printState")) {
96                 td.printState();
97             } else if (command.startsWith("printNodes")) {
98                 td.printNodes();
99             }
100
101         }
102     }
103
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);
108         }
109
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);
115
116         }
117     }
118
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);
126
127         }
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);
133
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);
137             }
138
139             System.out.println("Added node:" + entry);
140         }
141
142         actorRefs.putAll(newActorRefs);
143     }
144
145
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);
155             }
156         }
157     }
158
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);
167         }
168     }
169
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);
176         }
177         actorSystem.stop(actorRef);
178         actorRefs.remove(actorName);
179
180         for (ActorRef actor : actorRefs.values()) {
181             actor.tell(new RemoveRaftPeer(actorName), null);
182         }
183
184         allPeers.remove(actorName);
185
186     }
187
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());
193             }
194         } else {
195             System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
196         }
197
198     }
199
200     public void startLoggingForClient(ActorRef client) {
201         logGenerator.startLoggingForClient(client);
202     }
203
204     public void stopAllLogging() {
205         for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
206             logGenerator.stopLoggingForClient(client.getValue());
207         }
208     }
209
210     public void stopLoggingForClient(ActorRef client) {
211         logGenerator.stopLoggingForClient(client);
212     }
213
214     public void printState() {
215         for (ActorRef ref : actorRefs.values()) {
216             ref.tell(new PrintState(), null);
217         }
218     }
219
220     public void printNodes() {
221         for (ActorRef ref : actorRefs.values()) {
222             ref.tell(new PrintRole(), null);
223         }
224     }
225
226     public ActorRef getLeader() {
227         return null;
228     }
229
230     private int getUnusedRandom(int num) {
231         int rand = -1;
232         do {
233             rand = (new Random()).nextInt(num * num);
234         } while (allPeers.keySet().contains("example-"+rand));
235
236         return rand;
237     }
238
239     private static Map<String, String> withoutPeer(String peerId) {
240         Map<String, String> without = new ConcurrentHashMap<>(allPeers);
241         without.remove(peerId);
242
243         return without;
244     }
245 }
246