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 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"
64 protected Object doExecute() {
65 int handlerModule = 0;
67 if (cmd.equals("") || cmd.equals("help") || cmd.equals("-help") || cmd.equals("--help")) {
68 for (String help : validCommands) {
69 session.getConsole().println(help);
73 String[] args = cmd.split(" ");
74 if (args.length == 0) {
77 String firstArg = args[0];
78 if (firstArg == null || firstArg.trim().equals("")) {
79 session.getConsole().println("Please provide a valid input.");
87 case "running-config":
88 cmd = "running-config";
92 session.getConsole().println("Unknown command");
96 switch (handlerModule) {
100 } catch (IOException ioe) {
101 session.getConsole().println("IOException thrown.");
110 public static void setHostAddr(String hostAddr) {
111 serverName = hostAddr;
114 public String getHostAddr() {
118 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
119 public void handleCommand(String command) throws IOException {
120 char[] cbuf = new char[10];
122 PrintWriter outToSocket;
123 BufferedReader inFromSocket;
124 StringBuilder sb = new StringBuilder();
134 socket = new Socket(serverName, SERVER_PORT);
136 } catch (UnknownHostException ioe) {
137 System.out.println("No host exists: " + ioe.getMessage());
139 } catch (IOException ioe) {
140 System.out.println("I/O error occured " + ioe.getMessage());
144 socket.setSoTimeout(SOCK_TIMEOUT * 1000);
145 outToSocket = new PrintWriter(socket.getOutputStream(), true);
146 inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
148 } catch (IOException ioe) {
149 System.out.println("IOException thrown.");
155 ret = inFromSocket.read(cbuf);
157 } catch (SocketTimeoutException ste) {
158 System.out.println("Read from Socket timed Out while asking for password.");
163 System.out.println("Connection closed by BGPd.");
169 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
176 outToSocket.println(VTY_PASSWORD);
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.");
187 if (ip == gt || ip == hashChar) {
194 } else if (ip == -1) {
195 System.out.println(sb.toString());
196 System.out.println("Connection closed by BGPd.");
205 String promptStr = sb.toString();
206 String prompt = promptStr.trim();
208 outToSocket.println(NO_PAGINATION_CMD);
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..");
218 if (ip == gt || ip == hashChar) {
220 } else if (ip == -1) {
221 System.out.println(sb.toString());
222 System.out.println("Connection closed by BGPd.");
232 String inputCmd = "show " + command;
233 outToSocket.println(inputCmd);
234 StringBuilder output = new StringBuilder();
235 String errorMsg = "";
237 char[] opBuf = new char[100];
238 temp = new StringBuilder();
239 temp2 = new StringBuilder();
241 ret = inFromSocket.read(opBuf);
243 } catch (SocketTimeoutException ste) {
244 errorMsg = "Read from Socket timed Out while getting the data.";
248 errorMsg = "Connection closed by BGPd";
253 if (temp2.toString().contains(inputCmd)) {
255 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
256 temp.append(replacedStr);
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);
271 output.append(newString);
276 System.out.println(output.toString().trim());
277 if (errorMsg.length() > 0) {
278 System.out.println(errorMsg);