Enhancements in BGP module
[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 vpnv6 all",
51         "display routing ip bgp vpnv6 rd <rd>",
52         "display routing ip bgp vpnv6 all summary",
53         "display routing ip bgp vpnv6 all tags",
54         "display routing ip bgp neighbors",
55         "display routing ip bgp summary",
56         "display routing ip bgp ipv4 unicast",
57         "display routing ip bgp ipv4 unicast <ip/mask>",
58         "display routing bgp neighbors",
59         "display routing bgp neighbors <ip>",
60         "display routing bgp ipv4 summary",
61         "display routing bgp ipv4 vpn summary",
62         "display routing bgp ipv4 unicast <ip>",
63         "display routing bgp ipv4 unicast <ip/mask>",
64         "display routing bgp bfd neighbors details",
65         "display routing bgp bfd neighbors",
66         "display routing bgp bfd neighbors <ip> details",
67         "display routing bgp bfd global-config",
68         "display routing running-config"
69     };
70
71     @Override
72     protected Object doExecute() {
73         int handlerModule = 0;
74         cmd = cmd.trim();
75         if (cmd.equals("") || cmd.equals("help") || cmd.equals("-help") || cmd.equals("--help")) {
76             for (String help : validCommands) {
77                 session.getConsole().println(help);
78             }
79             return null;
80         }
81         String[] args = cmd.split(" ");
82         if (args.length == 0) {
83             return null;
84         }
85         String firstArg = args[0];
86         if (firstArg == null || firstArg.trim().equals("")) {
87             session.getConsole().println("Please provide a valid input.");
88             return null;
89         }
90         switch (firstArg) {
91             case "ip":
92             case "bgp":
93                 handlerModule = BGPD;
94                 break;
95             case "running-config":
96                 cmd = "running-config";
97                 handlerModule = BGPD;
98                 break;
99             default:
100                 session.getConsole().println("Unknown command");
101                 return null;
102         }
103
104         switch (handlerModule) {
105             case BGPD:
106                 try {
107                     handleCommand(cmd);
108                 } catch (IOException ioe) {
109                     session.getConsole().println("I/O error to" + serverName + ":"
110                             + SERVER_PORT + "occured " + ioe.getMessage());
111                 }
112                 break;
113             default:
114                 break;
115         }
116         return null;
117     }
118
119     public static void setHostAddr(String hostAddr) {
120         serverName = hostAddr;
121     }
122
123     public String getHostAddr() {
124         return serverName;
125     }
126
127     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
128     public void handleCommand(String command) throws IOException {
129         char[] cbuf = new char[10];
130         Socket socket;
131         PrintWriter outToSocket;
132         BufferedReader inFromSocket;
133         StringBuilder sb = new StringBuilder();
134         int ip;
135         int ret;
136         StringBuilder temp;
137         StringBuilder temp2;
138         char ch;
139         char gt = '>';
140         char hashChar = '#';
141
142         try {
143             socket = new Socket(serverName, SERVER_PORT);
144
145         } catch (UnknownHostException ioe) {
146             System.out.println("No host to " + serverName + ":"
147                                          + SERVER_PORT + "exists: " + ioe.getMessage());
148             return;
149         } catch (IOException ioe) {
150             System.out.println("I/O error to" + serverName + ":"
151                                          + SERVER_PORT + "occured " + ioe.getMessage());
152             return;
153         }
154         try {
155             socket.setSoTimeout(SOCK_TIMEOUT * 1000);
156             outToSocket = new PrintWriter(socket.getOutputStream(), true);
157             inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
158
159         } catch (IOException ioe) {
160             System.out.println("I/O error to" + serverName + ":"
161                     + SERVER_PORT + "occured " + ioe.getMessage());
162             socket.close();
163             return;
164         }
165         while (true) {
166             try {
167                 ret = inFromSocket.read(cbuf);
168
169             } catch (SocketTimeoutException ste) {
170                 System.out.println("Read from Socket timed Out while asking for password.");
171                 socket.close();
172                 return;
173             }
174             if (ret == -1) {
175                 System.out.println("Connection closed by BGPd.");
176                 socket.close();
177                 return;
178             } else {
179                 sb.append(cbuf);
180
181                 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
182                     break;
183                 }
184             }
185         }
186
187         sb.setLength(0);
188         outToSocket.println(VTY_PASSWORD);
189
190         while (true) {
191             try {
192                 ip = inFromSocket.read();
193             } catch (SocketTimeoutException ste) {
194                 System.out.println(sb.toString());
195                 System.out.println("Socket timeout while verifying the  password in" + serverName + ":"
196                         + SERVER_PORT + "occured " + ste.getMessage());
197                 socket.close();
198                 return;
199             }
200             if (ip == gt || ip == hashChar) {
201                 if (ip == gt) {
202                     sb.append(gt);
203                 } else {
204                     sb.append(hashChar);
205                 }
206                 break;
207             } else if (ip == -1) {
208                 System.out.println(sb.toString());
209                 System.out.println("Connection closed by BGPd.");
210                 socket.close();
211                 return;
212             } else {
213                 ch = (char) ip;
214                 sb.append(ch);
215             }
216         }
217
218         String promptStr = sb.toString();
219         String prompt = promptStr.trim();
220         sb.setLength(0);
221         outToSocket.println(NO_PAGINATION_CMD);
222         while (true) {
223             try {
224                 ip = inFromSocket.read();
225             } catch (SocketTimeoutException ste) {
226                 System.out.println(sb.toString());
227                 System.out.println("Read from socket timeout while sending term len command in" + serverName + ":"
228                         + SERVER_PORT + "occured " + ste.getMessage());
229                 socket.close();
230                 return;
231             }
232             if (ip == gt || ip == hashChar) {
233                 break;
234             } else if (ip == -1) {
235                 System.out.println(sb.toString());
236                 System.out.println("Connection closed by BGPd.");
237                 socket.close();
238                 return;
239             } else {
240                 ch = (char) ip;
241                 sb.append(ch);
242             }
243         }
244         sb.setLength(0);
245
246         String inputCmd = "show " + command;
247         outToSocket.println(inputCmd);
248         StringBuffer output = new StringBuffer();
249         String errorMsg = "";
250         while (true) {
251             char[] opBuf = new char[100];
252             temp = new StringBuilder();
253             temp2 = new StringBuilder();
254             try {
255                 ret = inFromSocket.read(opBuf);
256
257             } catch (SocketTimeoutException ste) {
258                 errorMsg = "Read from Socket timed Out while getting the data.";
259                 System.out.println("Socket timeout while getting the data in" + serverName + ":"
260                         + SERVER_PORT + "occured " + ste.getMessage());
261                 break;
262             }
263             if (ret == -1) {
264                 errorMsg = "Connection closed by BGPd";
265                 break;
266             }
267             temp2.append(opBuf);
268
269             if (temp2.toString().contains(inputCmd)) {
270
271                 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
272                 temp.append(replacedStr);
273                 temp2.setLength(0);
274
275             } else {
276                 temp.append(opBuf);
277                 temp2.setLength(0);
278
279             }
280
281             String outputStr = temp.toString();
282             output.append(outputStr);
283             if (output.toString().trim().endsWith(prompt)) {
284                 int index = output.toString().lastIndexOf(prompt);
285                 String newString = output.toString().substring(0, index);
286                 output.setLength(0);
287                 output.append(newString);
288                 break;
289             }
290             temp.setLength(0);
291         }
292         System.out.println(output.toString().trim());
293         if (errorMsg.length() > 0) {
294             System.out.println(errorMsg);
295         }
296         socket.close();
297     }
298 }