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