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.commands;
11 import java.io.BufferedReader;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintWriter;
15 import java.net.InetAddress;
16 import java.net.Socket;
17 import java.net.SocketException;
19 import org.apache.karaf.shell.commands.Argument;
20 import org.apache.karaf.shell.commands.Command;
21 import org.apache.karaf.shell.commands.Option;
22 import org.apache.karaf.shell.console.OsgiCommandSupport;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 @Command(scope = "odl", name = "clear-bgp-neighbor", description = "")
27 public class ClearBgpCli extends OsgiCommandSupport {
29 private static int serverPort = 2605;
30 private static String serverName = "localhost";
31 public static String passwordCheckStr = "Password:";
32 public static String vtyPassword = "sdncbgpc";
33 public static String enableCmd = "enable";
34 public static int sockTimeout = 5;
35 static char HASH_PROMPT = '#';
38 static final Logger LOGGER = LoggerFactory.getLogger(ClearBgpCli.class);
40 @Argument(name="neighbor-ip", description = "neighbor ip to be cleared", required = false, multiValued = false)
44 PrintWriter out = null;
45 BufferedReader in = null;
47 public static void setHostAddr(String hostAddr) {
48 serverName = hostAddr;
51 protected Object doExecute() throws Exception {
53 System.out.println("enter neighbor ip to be cleared");
54 System.out.println("Usage:");
55 System.out.println("odl:clear-bgp-neighbor <neighbor|all>");
57 } else if ("all".equals(nbr)) {
61 InetAddress.getByName(nbr);
62 } catch (Exception e) {
63 System.out.println("Invalid neighbor ip");
67 clearBgp("clear ip bgp "+nbr);
71 public static void main(String args[]) throws IOException{
72 ClearBgpCli test = new ClearBgpCli();
73 test.clearBgp("clear ip bgp");
76 public void clearBgp(String clearCommand) throws IOException {
78 socket = new Socket(serverName, serverPort);
79 } catch (Exception ioe) {
80 System.out.println("failed to connect to bgpd " + ioe.getMessage());
83 intializeSocketOptions();
87 out.println(vtyPassword);
88 out.println("enable");
89 LOGGER.trace("reading until HASH sign");
90 readUntilPrompt(in, HASH_PROMPT);
92 out.println(clearCommand);
93 readUntilPrompt(in, HASH_PROMPT);
94 } catch (Exception e) {
95 System.out.println(e.getMessage());
103 private void intializeSocketOptions() throws SocketException, IOException {
104 socket.setSoTimeout(sockTimeout * 1000);
105 out = new PrintWriter(socket.getOutputStream(), true);
106 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
109 private static boolean readPassword(BufferedReader in)
111 return readUntilPrompt(in, GT, passwordCheckStr);
114 private static boolean readUntilPrompt(BufferedReader in, char promptChar)
116 return readUntilPrompt(in, promptChar, null);
119 private static boolean readUntilPrompt(
120 BufferedReader in, char promptChar, String passwordCheckStr)
122 StringBuilder sb = new StringBuilder();
127 throw new IOException("connection closed by bgpd");
128 } else if (ret == promptChar) {
132 sb.append((char)ret);
133 if (passwordCheckStr != null) {
134 if (sb.toString().contains(passwordCheckStr)) {