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