54ff92993a9ad23e2236b26c2944a28c944f9ba3
[controller.git] / opendaylight / md-sal / sal-akka-raft-example / src / main / java / org / opendaylight / controller / cluster / example / Main.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.example;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.actor.PoisonPill;
14 import com.google.common.base.Optional;
15 import java.io.BufferedReader;
16 import java.io.InputStreamReader;
17 import java.nio.charset.Charset;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.opendaylight.controller.cluster.example.messages.KeyValue;
23 import org.opendaylight.controller.cluster.raft.ConfigParams;
24
25 public final class Main {
26     private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create();
27     // Create three example actors
28     private static Map<String, String> allPeers = new HashMap<>();
29
30     static {
31         allPeers.put("example-1", "akka://default/user/example-1");
32         allPeers.put("example-2", "akka://default/user/example-2");
33         allPeers.put("example-3", "akka://default/user/example-3");
34     }
35
36     private Main() {
37     }
38
39     @SuppressWarnings("checkstyle:RegexpSingleLineJava")
40     public static void main(String[] args) throws Exception {
41         ActorRef example1Actor =
42             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
43                 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
44
45         ActorRef example2Actor =
46             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
47                 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
48
49         ActorRef example3Actor =
50             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
51                 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
52
53
54         List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
55
56         ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
57         BufferedReader br =
58             new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
59
60         System.out.println("Usage :");
61         System.out.println("s <1-3> to start a peer");
62         System.out.println("k <1-3> to kill a peer");
63
64         while (true) {
65             System.out.print("Enter command (0 to exit):");
66             try {
67                 String line = br.readLine();
68                 if (line == null) {
69                     continue;
70                 }
71                 String[] split = line.split(" ");
72                 if (split.length > 1) {
73                     String command = split[0];
74                     String actor = split[1];
75
76                     if ("k".equals(command)) {
77                         int num = Integer.parseInt(actor);
78                         examples.get(num - 1).tell(PoisonPill.getInstance(), null);
79                         continue;
80                     } else if ("s".equals(command)) {
81                         int num = Integer.parseInt(actor);
82                         String actorName = "example-" + num;
83                         examples.add(num - 1,
84                             ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
85                                 withoutPeer(actorName), Optional.<ConfigParams>absent()),
86                                 actorName));
87                         System.out.println("Created actor : " + actorName);
88                         continue;
89                     }
90                 }
91
92                 int num = Integer.parseInt(line);
93                 if (num == 0) {
94                     System.exit(0);
95                 }
96                 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
97             } catch (NumberFormatException nfe) {
98                 System.err.println("Invalid Format!");
99             }
100         }
101     }
102
103     private static Map<String, String> withoutPeer(String peerId) {
104         Map<String, String> without = new HashMap<>(allPeers);
105         without.remove(peerId);
106         return without;
107     }
108 }