Bug 2358: Fixed warnings in Restconf
[controller.git] / opendaylight / md-sal / sal-akka-raft-example / src / main / java / 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 org.opendaylight.controller.cluster.example.messages.KeyValue;
16 import org.opendaylight.controller.cluster.raft.ConfigParams;
17
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 public class Main {
26     private static final ActorSystem actorSystem = 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     public static void main(String[] args) throws Exception{
37         ActorRef example1Actor =
38             actorSystem.actorOf(ExampleActor.props("example-1",
39                 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
40
41         ActorRef example2Actor =
42             actorSystem.actorOf(ExampleActor.props("example-2",
43                 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
44
45         ActorRef example3Actor =
46             actorSystem.actorOf(ExampleActor.props("example-3",
47                 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
48
49
50         List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
51
52         ActorRef clientActor = actorSystem.actorOf(ClientActor.props(example1Actor));
53         BufferedReader br =
54             new BufferedReader(new InputStreamReader(System.in));
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 s = br.readLine();
64                 String[] split = s.split(" ");
65                 if(split.length > 1) {
66                     String command = split[0];
67                     String actor = split[1];
68
69                     if ("k".equals(command)) {
70                         int i = Integer.parseInt(actor);
71                         examples.get(i - 1)
72                             .tell(PoisonPill.getInstance(), null);
73                         continue;
74                     } else if ("s".equals(command)) {
75                         int i = Integer.parseInt(actor);
76                         String actorName = "example-" + i;
77                         examples.add(i - 1,
78                             actorSystem.actorOf(ExampleActor.props(actorName,
79                                 withoutPeer(actorName), Optional.<ConfigParams>absent()),
80                                 actorName));
81                         System.out.println("Created actor : " + actorName);
82                         continue;
83                     }
84                 }
85
86                 int i = Integer.parseInt(s);
87                 if(i == 0){
88                     System.exit(0);
89                 }
90                 clientActor.tell(new KeyValue("key " + i, "value " + i), null);
91             } catch (NumberFormatException nfe) {
92                 System.err.println("Invalid Format!");
93             }
94         }
95     }
96
97     private static Map<String, String> withoutPeer(String peerId) {
98         Map<String, String> without = new HashMap<>(allPeers);
99         without.remove(peerId);
100         return without;
101     }
102 }