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