Fix for upstream quagga: vtysh using #
[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 = '>', hashChar = '#';
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) || (ip == (int)hashChar)) {
191                 if (ip == (int) gt) {
192                     sb.append(gt);
193                 } else {
194                     sb.append(hashChar);
195                 }
196                 break;
197             } else if (ip == -1) {
198                 System.out.println(sb.toString());
199                 System.out.println("Connection closed by BGPd.");
200                 socket.close();
201                 return;
202             } else {
203                 ch = (char) ip;
204                 sb.append(ch);
205
206             }
207         }
208
209         String promptStr = sb.toString();
210         prompt = promptStr.trim();
211         sb.setLength(0);
212         out_to_socket.println(noPaginationCmd);
213         while (true) {
214             try {
215                 ip = in_from_socket.read();
216             } catch (SocketTimeoutException ste) {
217                 System.out.println(sb.toString());
218                 System.out.println("Read from Socket timed Out while sending the term len command..");
219                 socket.close();
220                 return;
221             }
222             if ((ip == (int) gt) || (ip == (int) hashChar)) {
223                 break;
224             } else if (ip == -1) {
225                 System.out.println(sb.toString());
226                 System.out.println("Connection closed by BGPd.");
227                 socket.close();
228                 return;
229             } else {
230                 ch = (char) ip;
231                 sb.append(ch);
232
233             }
234         }
235         sb.setLength(0);
236
237         out_to_socket.println(inputCmd);
238         StringBuffer output = new StringBuffer();
239         String errorMsg = "";
240         while (true) {
241             op_buf = new char[100];
242             temp = new StringBuilder();
243             temp2 = new StringBuilder();
244             try {
245                 ret = in_from_socket.read(op_buf);
246
247             } catch (SocketTimeoutException ste) {
248                 errorMsg = "Read from Socket timed Out while getting the data.";
249                 break;
250             }
251             if (ret == -1) {
252                 errorMsg = "Connection closed by BGPd";
253                 break;
254             }
255             temp2.append(op_buf);
256
257             if (temp2.toString().contains(inputCmd)) {
258
259                 replacedStr = temp2.toString().replaceAll(inputCmd, "");
260                 temp.append(replacedStr);
261                 temp2.setLength(0);
262
263             } else {
264                 temp.append(op_buf);
265                 temp2.setLength(0);
266
267             }
268
269             String outputStr = temp.toString();
270             outputStr.replaceAll("^\\s+|\\s+$", "");
271             output.append(outputStr);
272             if (output.toString().trim().endsWith(prompt)) {
273                 int index = output.toString().lastIndexOf(prompt);
274                 String newString = output.toString().substring(0, index);
275                 output.setLength(0);
276                 output.append(newString);
277                 break;
278             }
279             temp.setLength(0);
280         }
281         System.out.println(output.toString().trim());
282         if (errorMsg.length() > 0) {
283             System.out.println(errorMsg);
284         }
285         socket.close();
286         return;
287
288     }
289
290 }