Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / VtyshCli.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.bgpmanager;
10
11 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.net.Socket;
16 import java.net.SocketTimeoutException;
17 import java.net.UnknownHostException;
18 import org.apache.karaf.shell.commands.Command;
19 import org.apache.karaf.shell.commands.Option;
20 import org.apache.karaf.shell.console.OsgiCommandSupport;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @Command(scope = "odl", name = "show-bgp", description = "")
25 public class VtyshCli extends OsgiCommandSupport {
26     // public class DisplayBgpConfigCli {
27
28     @Option(name = "--cmd", description = "command to run", required = true, multiValued = false)
29     String cmd = null;
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(VtyshCli.class);
32
33     private static int serverPort = 2605;
34     private static String serverName = "localhost";
35     private static int handlerModule = 0;
36     private static final int BGPd = 1;
37     public static String passwordCheckStr = "Password:";
38     public static String vtyPassword = "sdncbgpc";
39     public static String noPaginationCmd = "terminal length 0";
40     public static int sockTimeout = 5;
41
42     String[] validCommands = new String[]{
43             "display routing ip bgp vpnv4 all",
44             "display routing ip bgp vpnv4 rd <rd>",
45             "display routing ip bgp vpnv4 all neighbors",
46             "display routing ip bgp vpnv4 all neighbors  <ip> routes",
47             "display routing ip bgp vpnv4 all  <ip/mask>",
48             "display routing ip bgp vpnv4 all summary",
49             "display routing ip bgp vpnv4 all tags",
50             "display routing ip bgp vpnv4 rd <rd>  tags",
51             "display routing ip bgp vpnv4 rd <rd>  <ip>",
52             "display routing ip bgp vpnv4 rd <rd>  <ip/mask>",
53             "display routing ip bgp neighbors",
54             "display routing ip bgp summary",
55             "display routing ip bgp ipv4 unicast",
56             "display routing ip bgp ipv4 unicast <ip/mask>",
57             "display routing bgp neighbors",
58             "display routing bgp neighbors <ip>",
59             "display routing bgp ipv4 unicast <ip>",
60             "display routing bgp ipv4 unicast <ip/mask>"
61     };
62     private static final Logger logger = LoggerFactory.getLogger(VtyshCli.class);
63
64     // @Override
65     protected Object doExecute() throws Exception {
66         String sArg;
67         cmd = cmd.trim();
68         if (cmd.equals("") || cmd.equals("help") ||
69             cmd.equals("-help") || cmd.equals("--help")) {
70             for (String help : validCommands) {
71                 System.out.println(help);
72             }
73             return null;
74         }
75         String args[] = cmd.split(" ");
76         if (args.length == 0) {
77             return null;
78         }
79         sArg = args[0];
80         if (sArg == null || sArg.trim().equals("")) {
81             System.out.println("Please provide a valid input.");
82             return null;
83         }
84         switch (sArg) {
85         case "ip":
86         case "bgp":
87             handlerModule = BGPd;
88             break;
89         default:
90             System.out.println("Unknown command");
91             return null;
92         }
93
94         switch (handlerModule) {
95         case BGPd:
96             try {
97                 handleCommand(sArg, cmd);
98             } catch (IOException ioe) {
99                 System.out.println("IOException thrown.");
100             }
101             break;
102         default:
103             break;
104         }
105         return null;
106     }
107
108     public static void setHostAddr(String hostAddr) {
109         serverName = hostAddr;
110     }
111
112     public String getHostAddr() {
113         return serverName;
114     }
115
116     public static void handleCommand(String arg, String cmd) throws IOException {
117
118         StringBuilder inputBgpCmd = new StringBuilder();
119
120         String str, prompt, replacedStr, inputCmd = null;
121         char cbuf[] = new char[10];
122         char op_buf[];
123         Socket socket = null;
124         PrintWriter out_to_socket = null;
125         BufferedReader in_from_socket = null;
126         StringBuilder sb = new StringBuilder();
127         int ip = 0, ret;
128         StringBuilder temp, temp2;
129         char ch, gt = '>';
130
131         inputBgpCmd.append("show " + cmd);
132
133         inputCmd = inputBgpCmd.toString();
134
135         try {
136             socket = new Socket(serverName, serverPort);
137
138         } catch (UnknownHostException ioe) {
139             System.out.println("No host exists: " + ioe.getMessage());
140             return;
141         } catch (IOException ioe) {
142             System.out.println("I/O error occured " + ioe.getMessage());
143             return;
144         }
145         try {
146             socket.setSoTimeout(sockTimeout * 1000);
147             out_to_socket = new PrintWriter(socket.getOutputStream(), true);
148             in_from_socket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
149
150         } catch (IOException ioe) {
151             System.out.println("IOException thrown.");
152             socket.close();
153             return;
154         }
155         while (true) {
156             try {
157                 ret = in_from_socket.read(cbuf);
158
159             } catch (SocketTimeoutException ste) {
160                 System.out.println("Read from Socket timed Out while asking for password.");
161                 socket.close();
162                 return;
163             }
164             if (ret == -1) {
165                 System.out.println("Connection closed by BGPd.");
166                 socket.close();
167                 return;
168             } else {
169                 sb.append(cbuf);
170
171                 if (sb.toString().contains(passwordCheckStr)) {
172
173                     break;
174                 }
175             }
176         }
177
178         sb.setLength(0);
179         out_to_socket.println(vtyPassword);
180
181         while (true) {
182             try {
183                 ip = in_from_socket.read();
184             } catch (SocketTimeoutException ste) {
185                 System.out.println(sb.toString());
186                 System.out.println("Read from Socket timed Out while verifying the password.");
187                 socket.close();
188                 return;
189             }
190             if (ip == (int) gt) {
191                 sb.append(gt);
192                 break;
193             } else if (ip == -1) {
194                 System.out.println(sb.toString());
195                 System.out.println("Connection closed by BGPd.");
196                 socket.close();
197                 return;
198             } else {
199                 ch = (char) ip;
200                 sb.append(ch);
201
202             }
203         }
204
205         String promptStr = sb.toString();
206         prompt = promptStr.trim();
207         sb.setLength(0);
208         out_to_socket.println(noPaginationCmd);
209         while (true) {
210             try {
211                 ip = in_from_socket.read();
212             } catch (SocketTimeoutException ste) {
213                 System.out.println(sb.toString());
214                 System.out.println("Read from Socket timed Out while sending the term len command..");
215                 socket.close();
216                 return;
217             }
218             if (ip == (int) gt) {
219                 break;
220             } else if (ip == -1) {
221                 System.out.println(sb.toString());
222                 System.out.println("Connection closed by BGPd.");
223                 socket.close();
224                 return;
225             } else {
226                 ch = (char) ip;
227                 sb.append(ch);
228
229             }
230         }
231         sb.setLength(0);
232
233         out_to_socket.println(inputCmd);
234         StringBuffer output = new StringBuffer();
235         String errorMsg = "";
236         while (true) {
237             op_buf = new char[100];
238             temp = new StringBuilder();
239             temp2 = new StringBuilder();
240             try {
241                 ret = in_from_socket.read(op_buf);
242
243             } catch (SocketTimeoutException ste) {
244                 errorMsg = "Read from Socket timed Out while getting the data.";
245                 break;
246             }
247             if (ret == -1) {
248                 errorMsg = "Connection closed by BGPd";
249                 break;
250             }
251             temp2.append(op_buf);
252
253             if (temp2.toString().contains(inputCmd)) {
254
255                 replacedStr = temp2.toString().replaceAll(inputCmd, "");
256                 temp.append(replacedStr);
257                 temp2.setLength(0);
258
259             } else {
260                 temp.append(op_buf);
261                 temp2.setLength(0);
262
263             }
264
265             String outputStr = temp.toString();
266             outputStr.replaceAll("^\\s+|\\s+$", "");
267             output.append(outputStr);
268             if (output.toString().trim().endsWith(prompt)) {
269                 int index = output.toString().lastIndexOf(prompt);
270                 String newString = output.toString().substring(0, index);
271                 output.setLength(0);
272                 output.append(newString);
273                 break;
274             }
275             temp.setLength(0);
276         }
277         System.out.println(output.toString().trim());
278         if (errorMsg.length() > 0) {
279             System.out.println(errorMsg);
280         }
281         socket.close();
282         return;
283
284     }
285
286 }