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 public void handleCommand(String command) throws IOException {
119 char[] cbuf = new char[10];
121 PrintWriter outToSocket;
122 BufferedReader inFromSocket;
123 StringBuilder sb = new StringBuilder();
133 socket = new Socket(serverName, SERVER_PORT);
135 } catch (UnknownHostException ioe) {
136 session.getConsole().println("No host exists: " + ioe.getMessage());
138 } catch (IOException ioe) {
139 session.getConsole().println("I/O error occured " + ioe.getMessage());
143 socket.setSoTimeout(SOCK_TIMEOUT * 1000);
144 outToSocket = new PrintWriter(socket.getOutputStream(), true);
145 inFromSocket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
147 } catch (IOException ioe) {
148 session.getConsole().println("IOException thrown.");
154 ret = inFromSocket.read(cbuf);
156 } catch (SocketTimeoutException ste) {
157 session.getConsole().println("Read from Socket timed Out while asking for password.");
162 session.getConsole().println("Connection closed by BGPd.");
168 if (sb.toString().contains(PASSWORD_CHECK_STR)) {
175 outToSocket.println(VTY_PASSWORD);
179 ip = inFromSocket.read();
180 } catch (SocketTimeoutException ste) {
181 session.getConsole().println(sb.toString());
182 session.getConsole().println("Read from Socket timed Out while verifying the password.");
186 if (ip == gt || ip == hashChar) {
193 } else if (ip == -1) {
194 session.getConsole().println(sb.toString());
195 session.getConsole().println("Connection closed by BGPd.");
204 String promptStr = sb.toString();
205 String prompt = promptStr.trim();
207 outToSocket.println(NO_PAGINATION_CMD);
210 ip = inFromSocket.read();
211 } catch (SocketTimeoutException ste) {
212 session.getConsole().println(sb.toString());
213 session.getConsole().println("Read from Socket timed Out while sending the term len command..");
217 if (ip == gt || ip == hashChar) {
219 } else if (ip == -1) {
220 session.getConsole().println(sb.toString());
221 session.getConsole().println("Connection closed by BGPd.");
231 String inputCmd = "show " + command;
232 outToSocket.println(inputCmd);
233 StringBuffer output = new StringBuffer();
234 String errorMsg = "";
236 char[] opBuf = new char[100];
237 temp = new StringBuilder();
238 temp2 = new StringBuilder();
240 ret = inFromSocket.read(opBuf);
242 } catch (SocketTimeoutException ste) {
243 errorMsg = "Read from Socket timed Out while getting the data.";
247 errorMsg = "Connection closed by BGPd";
252 if (temp2.toString().contains(inputCmd)) {
254 String replacedStr = temp2.toString().replaceAll(inputCmd, "");
255 temp.append(replacedStr);
264 String outputStr = temp.toString().replaceAll("^\\s", "");
265 output.append(outputStr);
266 if (output.toString().trim().endsWith(prompt)) {
267 int index = output.toString().lastIndexOf(prompt);
268 String newString = output.toString().substring(0, index);
270 output.append(newString);
275 session.getConsole().println(output.toString().trim());
276 if (errorMsg.length() > 0) {
277 session.getConsole().println(errorMsg);