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 running-config"
68 protected Object doExecute() {
69 int handlerModule = 0;
71 if (cmd.equals("") || cmd.equals("help") || cmd.equals("-help") || cmd.equals("--help")) {
72 for (String help : validCommands) {
73 session.getConsole().println(help);
77 String[] args = cmd.split(" ");
78 if (args.length == 0) {
81 String firstArg = args[0];
82 if (firstArg == null || firstArg.trim().equals("")) {
83 session.getConsole().println("Please provide a valid input.");
91 case "running-config":
92 cmd = "running-config";
96 session.getConsole().println("Unknown command");
100 switch (handlerModule) {
104 } catch (IOException ioe) {
105 session.getConsole().println("IOException thrown.");
114 public static void setHostAddr(String hostAddr) {
115 serverName = hostAddr;
118 public String getHostAddr() {
122 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
123 public void handleCommand(String command) throws IOException {
124 char[] cbuf = new char[10];
126 PrintWriter outToSocket;
127 BufferedReader inFromSocket;
128 StringBuilder sb = new StringBuilder();
138 socket = new Socket(serverName, SERVER_PORT);
140 } catch (UnknownHostException ioe) {
141 System.out.println("No host exists: " + ioe.getMessage());
143 } catch (IOException ioe) {
144 System.out.println("I/O error occured " + ioe.getMessage());
148 socket.setSoTimeout(SOCK_TIMEOUT * 1000);
149 outToSocket = new PrintWriter(socket.getOutputStream(), true);
150 inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
152 } catch (IOException ioe) {
153 System.out.println("IOException thrown.");
159 ret = inFromSocket.read(cbuf);
161 } catch (SocketTimeoutException ste) {
162 System.out.println("Read from Socket timed Out while asking for password.");
167 System.out.println("Connection closed by BGPd.");
173 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
180 outToSocket.println(VTY_PASSWORD);
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.");
191 if (ip == gt || ip == hashChar) {
198 } else if (ip == -1) {
199 System.out.println(sb.toString());
200 System.out.println("Connection closed by BGPd.");
209 String promptStr = sb.toString();
210 String prompt = promptStr.trim();
212 outToSocket.println(NO_PAGINATION_CMD);
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..");
222 if (ip == gt || ip == hashChar) {
224 } else if (ip == -1) {
225 System.out.println(sb.toString());
226 System.out.println("Connection closed by BGPd.");
236 String inputCmd = "show " + command;
237 outToSocket.println(inputCmd);
238 StringBuilder output = new StringBuilder();
239 String errorMsg = "";
241 char[] opBuf = new char[100];
242 temp = new StringBuilder();
243 temp2 = new StringBuilder();
245 ret = inFromSocket.read(opBuf);
247 } catch (SocketTimeoutException ste) {
248 errorMsg = "Read from Socket timed Out while getting the data.";
252 errorMsg = "Connection closed by BGPd";
257 if (temp2.toString().contains(inputCmd)) {
259 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
260 temp.append(replacedStr);
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);
275 output.append(newString);
280 System.out.println(output.toString().trim());
281 if (errorMsg.length() > 0) {
282 System.out.println(errorMsg);