7025886e3438d01b4b80c8081a7ee0f9d55738ec
[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     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
119     public void handleCommand(String command) throws IOException {
120         char[] cbuf = new char[10];
121         Socket socket;
122         PrintWriter outToSocket;
123         BufferedReader inFromSocket;
124         StringBuilder sb = new StringBuilder();
125         int ip;
126         int ret;
127         StringBuilder temp;
128         StringBuilder temp2;
129         char ch;
130         char gt = '>';
131         char hashChar = '#';
132
133         try {
134             socket = new Socket(serverName, SERVER_PORT);
135
136         } catch (UnknownHostException ioe) {
137             System.out.println("No host exists: " + ioe.getMessage());
138             return;
139         } catch (IOException ioe) {
140             System.out.println("I/O error occured " + ioe.getMessage());
141             return;
142         }
143         try {
144             socket.setSoTimeout(SOCK_TIMEOUT * 1000);
145             outToSocket = new PrintWriter(socket.getOutputStream(), true);
146             inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
147
148         } catch (IOException ioe) {
149             System.out.println("IOException thrown.");
150             socket.close();
151             return;
152         }
153         while (true) {
154             try {
155                 ret = inFromSocket.read(cbuf);
156
157             } catch (SocketTimeoutException ste) {
158                 System.out.println("Read from Socket timed Out while asking for password.");
159                 socket.close();
160                 return;
161             }
162             if (ret == -1) {
163                 System.out.println("Connection closed by BGPd.");
164                 socket.close();
165                 return;
166             } else {
167                 sb.append(cbuf);
168
169                 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
170                     break;
171                 }
172             }
173         }
174
175         sb.setLength(0);
176         outToSocket.println(VTY_PASSWORD);
177
178         while (true) {
179             try {
180                 ip = inFromSocket.read();
181             } catch (SocketTimeoutException ste) {
182                 System.out.println(sb.toString());
183                 System.out.println("Read from Socket timed Out while verifying the password.");
184                 socket.close();
185                 return;
186             }
187             if (ip == gt || ip == hashChar) {
188                 if (ip == gt) {
189                     sb.append(gt);
190                 } else {
191                     sb.append(hashChar);
192                 }
193                 break;
194             } else if (ip == -1) {
195                 System.out.println(sb.toString());
196                 System.out.println("Connection closed by BGPd.");
197                 socket.close();
198                 return;
199             } else {
200                 ch = (char) ip;
201                 sb.append(ch);
202             }
203         }
204
205         String promptStr = sb.toString();
206         String prompt = promptStr.trim();
207         sb.setLength(0);
208         outToSocket.println(NO_PAGINATION_CMD);
209         while (true) {
210             try {
211                 ip = inFromSocket.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 == gt || ip == hashChar) {
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         sb.setLength(0);
231
232         String inputCmd = "show " + command;
233         outToSocket.println(inputCmd);
234         StringBuilder output = new StringBuilder();
235         String errorMsg = "";
236         while (true) {
237             char[] opBuf = new char[100];
238             temp = new StringBuilder();
239             temp2 = new StringBuilder();
240             try {
241                 ret = inFromSocket.read(opBuf);
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(opBuf);
252
253             if (temp2.toString().contains(inputCmd)) {
254
255                 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
256                 temp.append(replacedStr);
257                 temp2.setLength(0);
258
259             } else {
260                 temp.append(opBuf);
261                 temp2.setLength(0);
262
263             }
264
265             String outputStr = temp.toString().replaceAll("^\\s", "");
266             output.append(outputStr);
267             if (output.toString().trim().endsWith(prompt)) {
268                 int index = output.toString().lastIndexOf(prompt);
269                 String newString = output.toString().substring(0, index);
270                 output.setLength(0);
271                 output.append(newString);
272                 break;
273             }
274             temp.setLength(0);
275         }
276         System.out.println(output.toString().trim());
277         if (errorMsg.length() > 0) {
278             System.out.println(errorMsg);
279         }
280         socket.close();
281     }
282 }