2 * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.bgpmanager;
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;
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 {
27 @Option(name = "--cmd", description = "command to run", required = true, multiValued = false)
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;
37 private static String serverName = "localhost";
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"
72 protected Object doExecute() {
73 int handlerModule = 0;
75 if (cmd.equals("") || cmd.equals("help") || cmd.equals("-help") || cmd.equals("--help")) {
76 for (String help : validCommands) {
77 session.getConsole().println(help);
81 String[] args = cmd.split(" ");
82 if (args.length == 0) {
85 String firstArg = args[0];
86 if (firstArg == null || firstArg.trim().equals("")) {
87 session.getConsole().println("Please provide a valid input.");
95 case "running-config":
96 cmd = "running-config";
100 session.getConsole().println("Unknown command");
104 switch (handlerModule) {
108 } catch (IOException ioe) {
109 session.getConsole().println("I/O error to" + serverName + ":"
110 + SERVER_PORT + "occured " + ioe.getMessage());
119 public static void setHostAddr(String hostAddr) {
120 serverName = hostAddr;
123 public String getHostAddr() {
127 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
128 public void handleCommand(String command) throws IOException {
129 char[] cbuf = new char[10];
131 PrintWriter outToSocket;
132 BufferedReader inFromSocket;
133 StringBuilder sb = new StringBuilder();
143 socket = new Socket(serverName, SERVER_PORT);
145 } catch (UnknownHostException ioe) {
146 System.out.println("No host to " + serverName + ":"
147 + SERVER_PORT + "exists: " + ioe.getMessage());
149 } catch (IOException ioe) {
150 System.out.println("I/O error to" + serverName + ":"
151 + SERVER_PORT + "occured " + ioe.getMessage());
155 socket.setSoTimeout(SOCK_TIMEOUT * 1000);
156 outToSocket = new PrintWriter(socket.getOutputStream(), true);
157 inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
159 } catch (IOException ioe) {
160 System.out.println("I/O error to" + serverName + ":"
161 + SERVER_PORT + "occured " + ioe.getMessage());
167 ret = inFromSocket.read(cbuf);
169 } catch (SocketTimeoutException ste) {
170 System.out.println("Read from Socket timed Out while asking for password.");
175 System.out.println("Connection closed by BGPd.");
181 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
188 outToSocket.println(VTY_PASSWORD);
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());
200 if (ip == gt || ip == hashChar) {
207 } else if (ip == -1) {
208 System.out.println(sb.toString());
209 System.out.println("Connection closed by BGPd.");
218 String promptStr = sb.toString();
219 String prompt = promptStr.trim();
221 outToSocket.println(NO_PAGINATION_CMD);
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());
232 if (ip == gt || ip == hashChar) {
234 } else if (ip == -1) {
235 System.out.println(sb.toString());
236 System.out.println("Connection closed by BGPd.");
246 String inputCmd = "show " + command;
247 outToSocket.println(inputCmd);
248 StringBuffer output = new StringBuffer();
249 String errorMsg = "";
251 char[] opBuf = new char[100];
252 temp = new StringBuilder();
253 temp2 = new StringBuilder();
255 ret = inFromSocket.read(opBuf);
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());
264 errorMsg = "Connection closed by BGPd";
269 if (temp2.toString().contains(inputCmd)) {
271 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
272 temp.append(replacedStr);
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);
287 output.append(newString);
292 System.out.println(output.toString().trim());
293 if (errorMsg.length() > 0) {
294 System.out.println(errorMsg);