2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.nemo.renderer.cli;
\r
11 import org.jboss.netty.bootstrap.ClientBootstrap;
\r
12 import org.jboss.netty.channel.ChannelFactory;
\r
13 import org.jboss.netty.channel.ChannelPipeline;
\r
14 import org.jboss.netty.channel.ChannelPipelineFactory;
\r
15 import org.jboss.netty.channel.Channels;
\r
16 import org.jboss.netty.buffer.ChannelBuffer;
\r
17 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
\r
18 import org.jboss.netty.handler.codec.string.StringDecoder;
\r
19 import org.jboss.netty.handler.codec.string.StringEncoder;
\r
20 import org.jboss.netty.channel.*;
\r
22 import java.net.InetSocketAddress;
\r
23 import java.util.concurrent.Executors;
\r
25 import org.slf4j.Logger;
\r
26 import org.slf4j.LoggerFactory;
\r
30 * @author Shixing Liu
\r
32 public class TelnetUtils implements AutoCloseable {
\r
34 private static final Logger LOG = LoggerFactory.getLogger(TelnetUtils.class);
\r
36 private static Boolean ClearOver = Boolean.TRUE;
\r
37 private static String instanceOfClearCliTemplate = null;
\r
38 private static String instanceOfNewCliTemplate = null;
\r
39 private static String currentDeviceName = null;
\r
40 private static Boolean ConfigOver = Boolean.FALSE;
\r
45 public TelnetUtils(){
\r
47 LOG.info("TelnetUtils constructor");
\r
54 public void close() throws Exception {
\r
62 public static Boolean isClearOver() {
\r
70 public static void setClearOver(Boolean clearOver) {
\r
71 ClearOver = clearOver;
\r
78 public static String getInstanceOfClearCliTemplate() {
\r
79 return instanceOfClearCliTemplate;
\r
84 * @param instanceOfClearCliTemplate
\r
86 public static void setInstanceOfClearCliTemplate(String instanceOfClearCliTemplate) {
\r
87 TelnetUtils.instanceOfClearCliTemplate = instanceOfClearCliTemplate;
\r
94 public static String getInstanceOfNewCliTemplate() {
\r
95 return instanceOfNewCliTemplate;
\r
100 * @param instanceOfNewCliTemplate
\r
102 public static void setInstanceOfNewCliTemplate(String instanceOfNewCliTemplate) {
\r
103 TelnetUtils.instanceOfNewCliTemplate = instanceOfNewCliTemplate;
\r
110 public static String getCurrentDeviceName() {
\r
111 return currentDeviceName;
\r
116 * @param currentDeviceName
\r
118 public static void setCurrentDeviceName(String currentDeviceName) {
\r
119 TelnetUtils.currentDeviceName = currentDeviceName;
\r
126 public static Boolean isConfigOver() {
\r
127 return ConfigOver.booleanValue();
\r
132 * @param configOver
\r
134 public static void setConfigOver(Boolean configOver) {
\r
135 ConfigOver = configOver;
\r
140 * @param simpleChannelUpstreamHandler
\r
142 private void startConnectAndConfig(SimpleChannelUpstreamHandler simpleChannelUpstreamHandler){
\r
143 ChannelFactory factory = new NioClientSocketChannelFactory(
\r
144 Executors.newCachedThreadPool(),
\r
145 Executors.newCachedThreadPool());
\r
146 ClientBootstrap bootstrap = new ClientBootstrap(factory);
\r
147 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
\r
148 public ChannelPipeline getPipeline() {
\r
149 ChannelPipeline p = Channels.pipeline();
\r
150 p.addLast("encode", new StringEncoder());
\r
151 p.addLast("decode", new StringDecoder());
\r
152 p.addLast("handler", new ClientHandlerForClearOldConfig());
\r
156 bootstrap.setOption("tcpNoDelay" , true);
\r
157 bootstrap.setOption("keepAlive", true);
\r
158 bootstrap.setOption("connectTimeoutMillis", 5000);
\r
160 String ipAddress = null;
\r
161 String deviceName = getCurrentDeviceName();
\r
162 if(deviceName.contains("PE1")) {
\r
163 ipAddress = "191.4.1.114";
\r
164 }else if(deviceName.contains("PE2")){
\r
165 ipAddress = "191.4.1.77";
\r
166 }else if(deviceName.contains("PE3")){
\r
167 ipAddress = "191.4.1.103";
\r
169 System.out.println("Try to clear old configuration for device "+ deviceName+"\n");
\r
170 bootstrap.connect(new InetSocketAddress(ipAddress, 23));
\r
177 private void clearOldConfig(){
\r
179 ChannelFactory factory = new NioClientSocketChannelFactory(
\r
180 Executors.newCachedThreadPool(),
\r
181 Executors.newCachedThreadPool());
\r
182 ClientBootstrap bootstrap = new ClientBootstrap(factory);
\r
183 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
\r
184 public ChannelPipeline getPipeline() {
\r
185 ChannelPipeline p = Channels.pipeline();
\r
186 p.addLast("encode", new StringEncoder());
\r
187 p.addLast("decode", new StringDecoder());
\r
188 p.addLast("handler", new ClientHandlerForClearOldConfig());
\r
192 bootstrap.setOption("tcpNoDelay" , true);
\r
193 bootstrap.setOption("keepAlive", true);
\r
194 bootstrap.setOption("connectTimeoutMillis", 5000);
\r
196 String ipAddress = null;
\r
197 String deviceName = getCurrentDeviceName();
\r
198 if(deviceName.contains("PE1")) {
\r
199 ipAddress = "191.4.1.114";
\r
200 }else if(deviceName.contains("PE2")){
\r
201 ipAddress = "191.4.1.77";
\r
202 }else if(deviceName.contains("PE3")){
\r
203 ipAddress = "191.4.1.103";
\r
205 System.out.println("Try to clear old configuration for device "+ deviceName+"\n");
\r
206 bootstrap.connect(new InetSocketAddress(ipAddress, 23));
\r
212 private static class ClientHandlerForClearOldConfig extends SimpleChannelUpstreamHandler {
\r
214 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
\r
218 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)throws Exception {
\r
220 String str = (String)(e.getMessage());
\r
221 System.out.println(e.getMessage());
\r
222 if(str.contains("Username")){
\r
223 e.getChannel().write("lfk\n");
\r
225 else if(str.contains("Password")){
\r
226 e.getChannel().write("123!@#\n");
\r
228 else if(str.contains("The current login time is")){
\r
229 //System.out.println("------------b1----------");
\r
230 setClearOver(Boolean.FALSE);
\r
231 e.getChannel().write("system-view\n");
\r
232 e.getChannel().write("undo ip vpn-instance vpn1\n");
\r
234 }else if(str.contains("Error: The VPN instance does not exist.")){
\r
235 //System.out.println("------------b2----------");
\r
236 setClearOver(Boolean.TRUE);
\r
237 e.getChannel().close();
\r
238 System.out.println("Clear is completed for device " + getCurrentDeviceName());
\r
239 //start send down new config cli
\r
241 }else if(str.contains("Error: The VPN instance is in stale state.")){
\r
242 //System.out.println("------------b3----------");
\r
243 e.getChannel().write("undo ip vpn-instance vpn1\n");
\r
245 else if((isClearOver() == Boolean.FALSE)
\r
246 &&(!str.contains("Error: The VPN instance does not exist."))
\r
247 &&(!str.contains("Error: The VPN instance is in stale state."))){
\r
248 //System.out.println("------------b4----------");
\r
249 e.getChannel().write("undo ip vpn-instance vpn1\n");
\r
251 super.messageReceived(ctx, e);
\r
255 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
\r
256 e.getCause().printStackTrace();
\r
257 e.getChannel().close();
\r
264 private static void sendNewConfig(){
\r
266 ChannelFactory factory = new NioClientSocketChannelFactory(
\r
267 Executors.newCachedThreadPool(),
\r
268 Executors.newCachedThreadPool());
\r
269 ClientBootstrap bootstrap = new ClientBootstrap(factory);
\r
270 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
\r
271 public ChannelPipeline getPipeline() {
\r
272 ChannelPipeline p = Channels.pipeline();
\r
273 p.addLast("encode",new StringEncoder());
\r
274 p.addLast("decode",new StringDecoder());
\r
275 p.addLast("handler",new ClientHandlerForSendNewConfig());
\r
279 bootstrap.setOption("connectTimeoutMillis", 6000);
\r
280 bootstrap.setOption("tcpNoDelay" , true);
\r
281 bootstrap.setOption("keepAlive", true);
\r
283 String ipAddress = null;
\r
284 String deviceName = getCurrentDeviceName();
\r
285 if(deviceName.contains("PE1")) {
\r
286 ipAddress = "191.4.1.114";
\r
287 }else if(deviceName.contains("PE2")){
\r
288 ipAddress = "191.4.1.77";
\r
289 }else if(deviceName.contains("PE3")){
\r
290 ipAddress = "191.4.1.103";
\r
292 System.out.println("Start to configure device " + deviceName + " with CLI.");
\r
293 bootstrap.connect(new InetSocketAddress(ipAddress, 23));
\r
299 * @param deviceName
\r
300 * @param clearOldConfigString
\r
301 * @param cliExecutionSequences
\r
303 public void configL3vpnOnDeviceByCli(String deviceName,
\r
304 String clearOldConfigString,
\r
305 String cliExecutionSequences){
\r
307 setConfigOver(Boolean.FALSE);
\r
308 setInstanceOfClearCliTemplate(clearOldConfigString);
\r
309 setInstanceOfNewCliTemplate(cliExecutionSequences);
\r
310 setCurrentDeviceName(deviceName);
\r
317 private static class ClientHandlerForSendNewConfig extends SimpleChannelUpstreamHandler {
\r
319 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
\r
323 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
\r
325 String str = (String)(e.getMessage());
\r
326 System.out.println(e.getMessage());
\r
327 if(str.contains("Username")){
\r
328 e.getChannel().write("lfk\n");
\r
330 else if(str.contains("Password")){
\r
331 e.getChannel().write("123!@#\n");
\r
333 if (str.contains("The current login time is")) {
\r
334 System.out.println("ClientHandlerForConfig ");
\r
335 e.getChannel().write("system-view\n");
\r
336 e.getChannel().write(getInstanceOfClearCliTemplate());
\r
337 e.getChannel().write(getInstanceOfNewCliTemplate());
\r
338 setConfigOver(Boolean.TRUE);
\r
339 System.out.println("Configuration is completed for device "+ getCurrentDeviceName());
\r
342 super.messageReceived(ctx, e);
\r
346 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
\r
347 e.getCause().printStackTrace();
\r
348 e.getChannel().close();
\r