Fixup checkstyle
[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 package org.opendaylight.controller.cluster.example;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.PoisonPill;
13 import java.io.BufferedReader;
14 import java.io.InputStreamReader;
15 import java.nio.charset.Charset;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Optional;
20 import org.opendaylight.controller.cluster.example.messages.KeyValue;
21
22 public final class Main {
23     private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create();
24     // Create three example actors
25     private static Map<String, String> allPeers = new HashMap<>();
26
27     static {
28         allPeers.put("example-1", "akka://default/user/example-1");
29         allPeers.put("example-2", "akka://default/user/example-2");
30         allPeers.put("example-3", "akka://default/user/example-3");
31     }
32
33     private Main() {
34     }
35
36     @SuppressWarnings("checkstyle:RegexpSingleLineJava")
37     public static void main(final String[] args) throws Exception {
38         ActorRef example1Actor =
39             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
40                 withoutPeer("example-1"), Optional.empty()), "example-1");
41
42         ActorRef example2Actor =
43             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
44                 withoutPeer("example-2"), Optional.empty()), "example-2");
45
46         ActorRef example3Actor =
47             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
48                 withoutPeer("example-3"), Optional.empty()), "example-3");
49
50
51         final var examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
52
53         final var clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
54         final var br = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
55
56         System.out.println("Usage :");
57         System.out.println("s <1-3> to start a peer");
58         System.out.println("k <1-3> to kill a peer");
59
60         while (true) {
61             System.out.print("Enter command (0 to exit):");
62             try {
63                 String line = br.readLine();
64                 if (line == null) {
65                     continue;
66                 }
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.empty()), actorName));
82                         System.out.println("Created actor : " + actorName);
83                         continue;
84                     }
85                 }
86
87                 int num = Integer.parseInt(line);
88                 if (num == 0) {
89                     System.exit(0);
90                 }
91                 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
92             } catch (NumberFormatException nfe) {
93                 System.err.println("Invalid Format!");
94             }
95         }
96     }
97
98     private static Map<String, String> withoutPeer(final String peerId) {
99         Map<String, String> without = new HashMap<>(allPeers);
100         without.remove(peerId);
101         return without;
102     }
103 }