Integration test with hadshake
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClient.java
1  /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.clients;
3
4 import io.netty.bootstrap.Bootstrap;
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.Channel;
7 import io.netty.channel.EventLoopGroup;
8 import io.netty.channel.nio.NioEventLoopGroup;
9 import io.netty.channel.socket.nio.NioSocketChannel;
10 import io.netty.util.concurrent.Future;
11
12 import java.io.BufferedReader;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.net.InetAddress;
19 import java.nio.charset.Charset;
20
21 import org.opendaylight.openflowjava.protocol.impl.integration.IntegrationTest;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.util.concurrent.SettableFuture;
26
27 /**
28  * Simple client for testing purposes
29  *
30  * @author michal.polkorab
31  */
32 public class SimpleClient extends Thread {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
35     private final String host;
36     private final int port;
37     private boolean securedClient = false;
38     private InputStream fis;
39     private EventLoopGroup group;
40     private SettableFuture<Boolean> isOnlineFuture;
41     private SettableFuture<Boolean> automatedPartDone;
42     
43     /**
44      * Constructor of class
45      *
46      * @param host address of host
47      * @param port host listening port
48      * @param filename name of input file containing binary data to be send
49      */
50     public SimpleClient(String host, int port, String filename) {
51         this.host = host;
52         this.port = port;
53         if (filename != null) {
54             try {
55                 fis = new FileInputStream(filename);
56             } catch (FileNotFoundException ex) {
57                 LOGGER.error(ex.getMessage(), ex);
58             }
59         }
60         init();
61     }
62
63     /**
64      * @param host
65      * @param port
66      * @param filename
67      */
68     public SimpleClient(String host, int port, InputStream filename) {
69         this.host = host;
70         this.port = port;
71         this.fis = filename;
72         init();
73     }
74
75     private void init() {
76         isOnlineFuture = SettableFuture.create();
77         automatedPartDone = SettableFuture.create();
78     }
79     
80     /**
81      * Starting class of {@link SimpleClient}
82      */
83     @Override
84     public void run() {
85         group = new NioEventLoopGroup();
86         try {
87             Bootstrap b = new Bootstrap();
88             b.group(group)
89                 .channel(NioSocketChannel.class)
90                 .handler(new SimpleClientInitializer(isOnlineFuture, securedClient));
91
92             Channel ch = b.connect(host, port).sync().channel();
93             
94             byte[] bytearray = new byte[64];
95             ByteBuf buffy = ch.alloc().buffer(128);
96
97             LOGGER.debug("Before fis != null - fis == " + fis);
98             if (fis != null) {
99                 try {
100                     LOGGER.debug("Size to read (in bytes) : " + fis.available());
101                     int lenght;
102                     while ((lenght = fis.read(bytearray)) != -1) {
103                         buffy.writeBytes(bytearray, 0, lenght);
104                     }
105                     ch.writeAndFlush(buffy);
106                     fis.close();
107                 } catch (IOException e) {
108                     LOGGER.error(e.getMessage(), e);
109                     automatedPartDone.setException(e);
110                 }
111             }
112             automatedPartDone.set(true);
113             
114             BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
115             for (;;) {
116                 String line = in.readLine();
117                 if (line == null) {
118                     break;
119                 }
120                 buffy = ch.alloc().buffer(128);
121                 buffy.writeBytes(line.getBytes(Charset.defaultCharset()));
122                 ch.writeAndFlush(buffy);
123
124                 if ("bye".equals(line.toLowerCase())) {
125                     LOGGER.info("Bye");
126                     in.close();
127                     break;
128                 }
129             }
130             LOGGER.debug("after stdin reading done");
131         } catch (Exception ex) {
132             LOGGER.error(ex.getMessage(), ex);
133         } finally {
134             group.shutdownGracefully();
135         }
136     }
137
138     /**
139      * @return close future
140      */
141     public Future<?> disconnect() {
142         LOGGER.debug("disconnecting client");
143         return group.shutdownGracefully();
144     }
145
146     /**
147      * @param securedClient
148      */
149     public void setSecuredClient(boolean securedClient) {
150         this.securedClient = securedClient;
151     }
152
153     /**
154      * Sets up {@link SimpleClient} and fires run()
155      *
156      * @param args
157      * @throws Exception
158      */
159     public static void main(String[] args) throws Exception {
160         String host;
161         int port;
162         SimpleClient sc;
163         if (args.length != 4) {
164             LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
165                     + " <host> <port> <secured> <filename>");
166             LOGGER.error("Trying to use default setting.");
167             InetAddress ia = InetAddress.getLocalHost();
168             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
169             host = all[0].getHostAddress();
170             port = 6633;
171             InputStream filenamearg = IntegrationTest.class.getResourceAsStream(
172                     IntegrationTest.OF_BINARY_MESSAGE_INPUT_TXT);
173             sc = new SimpleClient(host, port, filenamearg);
174             sc.setSecuredClient(true);
175         } else {
176             host = args[0];
177             port = Integer.parseInt(args[1]);
178             String filenamearg = args[3];
179             sc = new SimpleClient(host, port, filenamearg);
180             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
181         }
182         sc.start();
183         
184     }
185     
186     /**
187      * @return the isOnlineFuture
188      */
189     public SettableFuture<Boolean> getIsOnlineFuture() {
190         return isOnlineFuture;
191     }
192     
193     /**
194      * @return the automatedPartDone
195      */
196     public SettableFuture<Boolean> getAutomatedPartDone() {
197         return automatedPartDone;
198     }
199 }