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;
129 char ch, gt = '>', hashChar = '#';
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) || (ip == (int)hashChar)) {
191 if (ip == (int) gt) {
197 } else if (ip == -1) {
198 System.out.println(sb.toString());
199 System.out.println("Connection closed by BGPd.");
209 String promptStr = sb.toString();
210 prompt = promptStr.trim();
212 out_to_socket.println(noPaginationCmd);
215 ip = in_from_socket.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 == (int) gt) || (ip == (int) hashChar)) {
224 } else if (ip == -1) {
225 System.out.println(sb.toString());
226 System.out.println("Connection closed by BGPd.");
237 out_to_socket.println(inputCmd);
238 StringBuffer output = new StringBuffer();
239 String errorMsg = "";
241 op_buf = new char[100];
242 temp = new StringBuilder();
243 temp2 = new StringBuilder();
245 ret = in_from_socket.read(op_buf);
247 } catch (SocketTimeoutException ste) {
248 errorMsg = "Read from Socket timed Out while getting the data.";
252 errorMsg = "Connection closed by BGPd";
255 temp2.append(op_buf);
257 if (temp2.toString().contains(inputCmd)) {
259 replacedStr = temp2.toString().replaceAll(inputCmd, "");
260 temp.append(replacedStr);
269 String outputStr = temp.toString();
270 outputStr.replaceAll("^\\s+|\\s+$", "");
271 output.append(outputStr);
272 if (output.toString().trim().endsWith(prompt)) {
273 int index = output.toString().lastIndexOf(prompt);
274 String newString = output.toString().substring(0, index);
276 output.append(newString);
281 System.out.println(output.toString().trim());
282 if (errorMsg.length() > 0) {
283 System.out.println(errorMsg);