Fix findbugs violations in md-sal - part 1
[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.util.Arrays;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import org.opendaylight.controller.cluster.example.messages.KeyValue;
22 import org.opendaylight.controller.cluster.raft.ConfigParams;
23
24 public final class Main {
25     private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create();
26     // Create three example actors
27     private static Map<String, String> allPeers = new HashMap<>();
28
29     static {
30         allPeers.put("example-1", "akka://default/user/example-1");
31         allPeers.put("example-2", "akka://default/user/example-2");
32         allPeers.put("example-3", "akka://default/user/example-3");
33     }
34
35     private Main() {
36     }
37
38     @SuppressWarnings("checkstyle:RegexpSingleLineJava")
39     public static void main(String[] args) throws Exception {
40         ActorRef example1Actor =
41             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
42                 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
43
44         ActorRef example2Actor =
45             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
46                 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
47
48         ActorRef example3Actor =
49             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
50                 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
51
52
53         List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
54
55         ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
56         BufferedReader br =
57             new BufferedReader(new InputStreamReader(System.in));
58
59         System.out.println("Usage :");
60         System.out.println("s <1-3> to start a peer");
61         System.out.println("k <1-3> to kill a peer");
62
63         while (true) {
64             System.out.print("Enter command (0 to exit):");
65             try {
66                 String line = br.readLine();
67                 String[] split = line.split(" ");
68                 if (split.length > 1) {
69                     String command = split[0];
70                     String actor = split[1];
71
72                     if ("k".equals(command)) {
73                         int num = Integer.parseInt(actor);
74                         examples.get(num - 1).tell(PoisonPill.getInstance(), null);
75                         continue;
76                     } else if ("s".equals(command)) {
77                         int num = Integer.parseInt(actor);
78                         String actorName = "example-" + num;
79                         examples.add(num - 1,
80                             ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
81                                 withoutPeer(actorName), Optional.<ConfigParams>absent()),
82                                 actorName));
83                         System.out.println("Created actor : " + actorName);
84                         continue;
85                     }
86                 }
87
88                 int num = Integer.parseInt(line);
89                 if (num == 0) {
90                     System.exit(0);
91                 }
92                 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
93             } catch (NumberFormatException nfe) {
94                 System.err.println("Invalid Format!");
95             }
96         }
97     }
98
99     private static Map<String, String> withoutPeer(String peerId) {
100         Map<String, String> without = new HashMap<>(allPeers);
101         without.remove(peerId);
102         return without;
103     }
104 }