2 * Copyright (c) 2015 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.bgpmanager;
11 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.net.Socket;
16 import java.net.SocketTimeoutException;
17 import java.net.UnknownHostException;
18 import org.apache.karaf.shell.commands.Command;
19 import org.apache.karaf.shell.commands.Option;
20 import org.apache.karaf.shell.console.OsgiCommandSupport;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 @Command(scope = "odl", name = "show-bgp", description = "")
25 public class VtyshCli extends OsgiCommandSupport {
26 // public class DisplayBgpConfigCli {
28 @Option(name = "--cmd", description = "command to run", required = true, multiValued = false)
31 private static final Logger LOGGER = LoggerFactory.getLogger(VtyshCli.class);
33 private static int serverPort = 2605;
34 private static String serverName = "localhost";
35 private static int handlerModule = 0;
36 private static final int BGPd = 1;
37 public static String passwordCheckStr = "Password:";
38 public static String vtyPassword = "sdncbgpc";
39 public static String noPaginationCmd = "terminal length 0";
40 public static int sockTimeout = 5;
42 String[] validCommands = new String[]{
43 "display routing ip bgp vpnv4 all",
44 "display routing ip bgp vpnv4 rd <rd>",
45 "display routing ip bgp vpnv4 all neighbors",
46 "display routing ip bgp vpnv4 all neighbors <ip> routes",
47 "display routing ip bgp vpnv4 all <ip/mask>",
48 "display routing ip bgp vpnv4 all summary",
49 "display routing ip bgp vpnv4 all tags",
50 "display routing ip bgp vpnv4 rd <rd> tags",
51 "display routing ip bgp vpnv4 rd <rd> <ip>",
52 "display routing ip bgp vpnv4 rd <rd> <ip/mask>",
53 "display routing ip bgp neighbors",
54 "display routing ip bgp summary",
55 "display routing ip bgp ipv4 unicast",
56 "display routing ip bgp ipv4 unicast <ip/mask>",
57 "display routing bgp neighbors",
58 "display routing bgp neighbors <ip>",
59 "display routing bgp ipv4 unicast <ip>",
60 "display routing bgp ipv4 unicast <ip/mask>"
62 private static final Logger logger = LoggerFactory.getLogger(VtyshCli.class);
65 protected Object doExecute() throws Exception {
68 if (cmd.equals("") || cmd.equals("help") ||
69 cmd.equals("-help") || cmd.equals("--help")) {
70 for (String help : validCommands) {
71 System.out.println(help);
75 String args[] = cmd.split(" ");
76 if (args.length == 0) {
80 if (sArg == null || sArg.trim().equals("")) {
81 System.out.println("Please provide a valid input.");
90 System.out.println("Unknown command");
94 switch (handlerModule) {
97 handleCommand(sArg, cmd);
98 } catch (IOException ioe) {
99 System.out.println("IOException thrown.");
108 public static void setHostAddr(String hostAddr) {
109 serverName = hostAddr;
112 public String getHostAddr() {
116 public static void handleCommand(String arg, String cmd) throws IOException {
118 StringBuilder inputBgpCmd = new StringBuilder();
120 String str, prompt, replacedStr, inputCmd = null;
121 char cbuf[] = new char[10];
123 Socket socket = null;
124 PrintWriter out_to_socket = null;
125 BufferedReader in_from_socket = null;
126 StringBuilder sb = new StringBuilder();
128 StringBuilder temp, temp2;
131 inputBgpCmd.append("show " + cmd);
133 inputCmd = inputBgpCmd.toString();
136 socket = new Socket(serverName, serverPort);
138 } catch (UnknownHostException ioe) {
139 System.out.println("No host exists: " + ioe.getMessage());
141 } catch (IOException ioe) {
142 System.out.println("I/O error occured " + ioe.getMessage());
146 socket.setSoTimeout(sockTimeout * 1000);
147 out_to_socket = new PrintWriter(socket.getOutputStream(), true);
148 in_from_socket = new BufferedReader(new InputStreamReader(socket.getInputStream()));
150 } catch (IOException ioe) {
151 System.out.println("IOException thrown.");
157 ret = in_from_socket.read(cbuf);
159 } catch (SocketTimeoutException ste) {
160 System.out.println("Read from Socket timed Out while asking for password.");
165 System.out.println("Connection closed by BGPd.");
171 if (sb.toString().contains(passwordCheckStr)) {
179 out_to_socket.println(vtyPassword);
183 ip = in_from_socket.read();
184 } catch (SocketTimeoutException ste) {
185 System.out.println(sb.toString());
186 System.out.println("Read from Socket timed Out while verifying the password.");
190 if (ip == (int) gt) {
193 } else if (ip == -1) {
194 System.out.println(sb.toString());
195 System.out.println("Connection closed by BGPd.");
205 String promptStr = sb.toString();
206 prompt = promptStr.trim();
208 out_to_socket.println(noPaginationCmd);
211 ip = in_from_socket.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 == (int) gt) {
220 } else if (ip == -1) {
221 System.out.println(sb.toString());
222 System.out.println("Connection closed by BGPd.");
233 out_to_socket.println(inputCmd);
234 StringBuffer output = new StringBuffer();
235 String errorMsg = "";
237 op_buf = new char[100];
238 temp = new StringBuilder();
239 temp2 = new StringBuilder();
241 ret = in_from_socket.read(op_buf);
243 } catch (SocketTimeoutException ste) {
244 errorMsg = "Read from Socket timed Out while getting the data.";
248 errorMsg = "Connection closed by BGPd";
251 temp2.append(op_buf);
253 if (temp2.toString().contains(inputCmd)) {
255 replacedStr = temp2.toString().replaceAll(inputCmd, "");
256 temp.append(replacedStr);
265 String outputStr = temp.toString();
266 outputStr.replaceAll("^\\s+|\\s+$", "");
267 output.append(outputStr);
268 if (output.toString().trim().endsWith(prompt)) {
269 int index = output.toString().lastIndexOf(prompt);
270 String newString = output.toString().substring(0, index);
272 output.append(newString);
277 System.out.println(output.toString().trim());
278 if (errorMsg.length() > 0) {
279 System.out.println(errorMsg);