Merge "BUG 2317 : StatisticsManager does not unregister from yang notifications on...
[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 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;
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.concurrent.ConcurrentHashMap;
15
16 /**
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.
21  */
22 public class TestDriver {
23
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();
31
32     /**
33      * Create nodes, add clients and start logging.
34      * Commands
35      *  bye
36      *  createNodes:{num}
37      *  stopNode:{nodeName}
38      *  reinstateNode:{nodeName}
39      *  addClients:{num}
40      *  addClientsToNode:{nodeName, num}
41      *  startLogging
42      *  stopLogging
43      *  startLoggingForClient:{nodeName}
44      *  stopLoggingForClient:{nodeName}
45      *  printNodes
46      *  printState
47      * @param args
48      * @throws Exception
49      */
50     public static void main(String[] args) throws Exception {
51         TestDriver td = new TestDriver();
52
53         System.out.println("Enter command (type bye to exit):");
54
55
56         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
57         while(true) {
58             String command = br.readLine();
59             if (command.startsWith("bye")) {
60                 System.exit(0);
61
62             } else if (command.startsWith("createNodes")) {
63                 String[] arr = command.split(":");
64                 int n = Integer.parseInt(arr[1]);
65                 td.createNodes(n);
66
67             } else if (command.startsWith("addClients")) {
68                 String[] arr = command.split(":");
69                 int n = Integer.parseInt(arr[1]);
70                 td.addClients(n);
71
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);
77
78             } else if (command.startsWith("stopNode")) {
79                 String[] arr = command.split(":");
80                 td.stopNode(arr[1]);
81
82             } else if (command.startsWith("reinstateNode")) {
83                 String[] arr = command.split(":");
84                 td.reinstateNode(arr[1]);
85
86             } else if (command.startsWith("startLogging")) {
87                 td.startAllLogging();
88
89             } else if (command.startsWith("startLoggingForClient")) {
90                 String[] arr = command.split(":");
91                 td.startLoggingForClient(clientActorRefs.get(arr[1]));
92
93             } else if (command.startsWith("stopLogging")) {
94                 td.stopAllLogging();
95
96             } else if (command.startsWith("stopLoggingForClient")) {
97                 String[] arr = command.split(":");
98                 td.stopLoggingForClient(clientActorRefs.get(arr[1]));
99
100             } else if (command.startsWith("printState")) {
101                 td.printState();
102             } else if (command.startsWith("printNodes")) {
103                 td.printNodes();
104             } else {
105                 System.out.println("Invalid command:" + command);
106             }
107
108         }
109     }
110
111     public static ActorRef createExampleActor(String name) {
112         return actorSystem.actorOf(ExampleActor.props(name, withoutPeer(name),
113             Optional.of(configParams)), name);
114     }
115
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);
120         }
121
122         for (String s : allPeers.keySet())  {
123             ActorRef exampleActor = createExampleActor(s);
124             actorRefs.put(s, exampleActor);
125             System.out.println("Created node:"+s);
126
127         }
128     }
129
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);
139             }
140         }
141     }
142
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);
151         }
152     }
153
154     public void stopNode(String actorName) {
155         ActorRef actorRef = actorRefs.get(actorName);
156
157         for (Map.Entry<String,ActorRef> entry : clientActorRefs.entrySet()) {
158             if (entry.getKey().endsWith(actorName)) {
159                 actorSystem.stop(entry.getValue());
160             }
161         }
162
163         actorSystem.stop(actorRef);
164         actorRefs.remove(actorName);
165         allPeers.remove(actorName);
166     }
167
168     public void reinstateNode(String actorName) {
169         String address = "akka://default/user/"+actorName;
170         allPeers.put(actorName, address);
171
172         ActorRef exampleActor = createExampleActor(actorName);
173         actorRefs.put(actorName, exampleActor);
174
175         addClientsToNode(actorName, 1);
176     }
177
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());
183             }
184         } else {
185             System.out.println("There are no clients for any nodes. First create clients using commands- addClients:<num> or addClientsToNode:<nodename>:<num>");
186         }
187
188     }
189
190     public void startLoggingForClient(ActorRef client) {
191         logGenerator.startLoggingForClient(client);
192     }
193
194     public void stopAllLogging() {
195         for(Map.Entry<String,ActorRef> client : clientActorRefs.entrySet()) {
196             logGenerator.stopLoggingForClient(client.getValue());
197         }
198     }
199
200     public void stopLoggingForClient(ActorRef client) {
201         logGenerator.stopLoggingForClient(client);
202     }
203
204     public void printState() {
205         for (ActorRef ref : actorRefs.values()) {
206             ref.tell(new PrintState(), null);
207         }
208     }
209
210     public void printNodes() {
211         for (ActorRef ref : actorRefs.values()) {
212             ref.tell(new PrintRole(), null);
213         }
214     }
215
216     public ActorRef getLeader() {
217         return null;
218     }
219
220
221     private static Map<String, String> withoutPeer(String peerId) {
222         Map<String, String> without = new ConcurrentHashMap<>(allPeers);
223         without.remove(peerId);
224
225         return without;
226     }
227 }
228