Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / commands / ClearBgpCli.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.bgpmanager.commands;
10
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;
18
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;
25
26 @Command(scope = "odl", name = "clear-bgp-neighbor", description = "")
27 public class ClearBgpCli extends OsgiCommandSupport {
28
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 = '#';
36     static char GT = '>';
37     
38     static final Logger LOGGER = LoggerFactory.getLogger(ClearBgpCli.class);
39
40     @Argument(name="neighbor-ip", description = "neighbor ip to be cleared", required = false, multiValued = false)
41     String nbr = "";
42
43     Socket socket = null;
44     PrintWriter out = null;
45     BufferedReader in = null;
46
47     public static void setHostAddr(String hostAddr) {
48         serverName = hostAddr;
49     }
50
51     protected Object doExecute() throws Exception {
52         if (nbr.isEmpty()) {
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>");
56             return null;
57         } else if ("all".equals(nbr)) {
58             nbr = "*";
59         } else {
60             try {
61                 InetAddress.getByName(nbr);
62             } catch (Exception e) {
63                 System.out.println("Invalid neighbor ip");
64                 return null;
65             }
66         }
67         clearBgp("clear ip bgp "+nbr);
68         return null;
69     }
70     
71     public static void main(String args[]) throws IOException{
72         ClearBgpCli test = new ClearBgpCli();
73         test.clearBgp("clear ip bgp");
74     }
75
76     public void clearBgp(String clearCommand) throws IOException {
77         try {
78             socket = new Socket(serverName, serverPort);
79         } catch (Exception ioe) {
80             System.out.println("failed to connect to bgpd " + ioe.getMessage());
81             return;
82         }
83         intializeSocketOptions();
84         try {
85             readPassword(in);
86             
87             out.println(vtyPassword);
88             out.println("enable");
89             LOGGER.trace("reading until HASH sign");
90             readUntilPrompt(in, HASH_PROMPT);
91             
92             out.println(clearCommand);
93             readUntilPrompt(in, HASH_PROMPT);
94         } catch (Exception e) {
95             System.out.println(e.getMessage());
96         } finally {
97             socket.close();
98         }
99         return;
100
101     }
102
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()));
107     }
108
109     private static boolean readPassword(BufferedReader in)
110             throws IOException {
111         return readUntilPrompt(in, GT, passwordCheckStr);
112     }
113
114     private static boolean readUntilPrompt(BufferedReader in, char promptChar)
115             throws IOException {
116         return readUntilPrompt(in, promptChar, null);
117     }
118     
119     private static boolean readUntilPrompt(
120             BufferedReader in, char promptChar, String passwordCheckStr)
121             throws IOException {
122         StringBuilder sb = new StringBuilder();
123         int ret = 0;
124         while (true) {
125             ret = in.read();
126             if (ret == -1) {
127                 throw new IOException("connection closed by bgpd");
128             } else if (ret == promptChar) {
129                 return true;
130             }
131             
132             sb.append((char)ret);
133             if (passwordCheckStr != null) {
134                 if (sb.toString().contains(passwordCheckStr)) {
135                     break;
136                 }
137             }
138         }
139         sb.setLength(0);
140         return true;
141     }
142
143 }