Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-core-demo / src / main / java / org / opendaylight / controller / sal / demo / SALDemo.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.demo;\r
9 \r
10 import java.io.BufferedReader;\r
11 import java.io.IOException;\r
12 import java.io.InputStreamReader;\r
13 \r
14 import org.opendaylight.controller.sal.core.impl.BrokerImpl;\r
15 import org.opendaylight.controller.sal.core.impl.notify.NotificationModule;\r
16 \r
17 \r
18 public class SALDemo {\r
19 \r
20     static BrokerImpl broker;\r
21     static DemoProviderImpl provider;\r
22     static DemoConsumerImpl consumer1;\r
23     static DemoConsumerImpl consumer2;\r
24 \r
25     public static void main(String[] args) {\r
26 \r
27         initialize();\r
28         initializeProvider();\r
29         displayHelp();\r
30 \r
31         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));\r
32         String s;\r
33         try {\r
34             while (true) {\r
35 \r
36                 System.out.print("\nEnter your choice (0 - list): ");\r
37                 s = in.readLine();\r
38                 int choice = Integer.parseInt(s.trim());\r
39                 try {\r
40                     switch (choice) {\r
41                     case 0:\r
42                         displayHelp();\r
43                         break;\r
44                     case 1:\r
45                         registerProvider();\r
46                         break;\r
47                     case 2:\r
48                         registerConsumer1();\r
49                         break;\r
50                     case 3:\r
51                         registerConsumer2();\r
52                         break;\r
53                     case 4:\r
54                         sendAlert(in);\r
55                         break;\r
56                     case 5:\r
57                         sendChange(in);\r
58                         break;\r
59                     case 6:\r
60                         unregisterConsumer1();\r
61                         break;\r
62                     case 7:\r
63                         unregisterConsumer2();\r
64                         break;\r
65                     case 8:\r
66                         unregisterProvider();\r
67                         break;\r
68                     case 9:\r
69                         return;\r
70                     default:\r
71                         System.out.println("Please enter valid input.");\r
72                         break;\r
73                     }\r
74                 } catch (Exception e) {\r
75                     System.out\r
76                             .println("Operation failed. Reason exception raised: "\r
77                                     + e.getClass().getSimpleName());\r
78                     System.out.println("   Message: " + e.getMessage());\r
79                 }\r
80 \r
81             }\r
82         } catch (IOException e) {\r
83 \r
84             e.printStackTrace();\r
85         }\r
86     }\r
87 \r
88     private static void registerConsumer1() {\r
89         broker.registerConsumer(consumer1);\r
90     }\r
91 \r
92     private static void registerConsumer2() {\r
93         broker.registerConsumer(consumer2);\r
94     }\r
95 \r
96     private static void sendAlert(BufferedReader in) throws IOException {\r
97         System.out.print("Please enter notification content:");\r
98         String content = in.readLine();\r
99         provider.sendAlertNotification(content);\r
100     }\r
101 \r
102     private static void sendChange(BufferedReader in) throws IOException {\r
103         System.out.print("Please enter notification content:");\r
104         String content = in.readLine();\r
105         provider.sendChangeNotification(content);\r
106     }\r
107 \r
108     private static void unregisterConsumer1() {\r
109         consumer1.closeSession();\r
110     }\r
111 \r
112     private static void unregisterConsumer2() {\r
113         consumer2.closeSession();\r
114     }\r
115 \r
116     private static void unregisterProvider() {\r
117         provider.closeSession();\r
118     }\r
119 \r
120     private static void displayHelp() {\r
121         System.out.println("Usage: ");\r
122         System.out.println("  0) Display Help");\r
123         System.out.println("  1) Register Provider");\r
124         System.out.println("  2) Register Consumer 1 (listening on alert)");\r
125         System.out\r
126                 .println("  3) Register Consumer 2 (listening on alert,change)");\r
127         System.out.println("  4) Send Alert Notification");\r
128         System.out.println("  5) Send Change Notification");\r
129         System.out.println("  6) Unregister Consumer 1");\r
130         System.out.println("  7) Unregister Consumer 2");\r
131         System.out.println("  8) Unregister Provider");\r
132         System.out.println("  9) Exit");\r
133 \r
134     }\r
135 \r
136     private static void initializeProvider() {\r
137         provider = new DemoProviderImpl();\r
138     }\r
139 \r
140     private static void initialize() {\r
141         System.out.println("Initializing broker");\r
142         broker = new BrokerImpl();\r
143         NotificationModule notifyModule = new NotificationModule();\r
144         broker.addModule(notifyModule);\r
145 \r
146         consumer1 = new DemoConsumerImpl("Consumer 1");\r
147         consumer2 = new DemoConsumerImpl("Consumer 2");\r
148         consumer2.setChangeAware(true);\r
149     }\r
150 \r
151     private static void registerProvider() {\r
152         broker.registerProvider(provider);\r
153     }\r
154 }\r