Copyright update
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClient.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.clients;
10
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import io.netty.util.concurrent.Future;
16
17 import java.net.InetAddress;
18 import java.util.concurrent.ExecutionException;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.util.concurrent.SettableFuture;
24
25 /**
26  * Simple client for testing purposes
27  *
28  * @author michal.polkorab
29  */
30 public class SimpleClient extends Thread {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
33     private final String host;
34     private final int port;
35     private boolean securedClient = false;
36     private EventLoopGroup group;
37     private SettableFuture<Boolean> isOnlineFuture;
38     private SettableFuture<Boolean> scenarioDone;
39     private SimpleClientInitializer clientInitializer;
40     private ScenarioHandler scenarioHandler;
41     
42     /**
43      * Constructor of class
44      *
45      * @param host address of host
46      * @param port host listening port
47      */
48     public SimpleClient(String host, int port) {
49         this.host = host;
50         this.port = port;
51         init();
52     }
53
54     private void init() {
55         isOnlineFuture = SettableFuture.create();
56         scenarioDone = SettableFuture.create();
57     }
58     
59     /**
60      * Starting class of {@link SimpleClient}
61      */
62     @Override
63     public void run() {
64         group = new NioEventLoopGroup();
65         clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
66         clientInitializer.setScenario(scenarioHandler);
67         try {
68             Bootstrap b = new Bootstrap();
69             b.group(group)
70                 .channel(NioSocketChannel.class)
71                 .handler(clientInitializer);
72
73             b.connect(host, port).sync();
74
75             synchronized (scenarioHandler) {
76                 LOGGER.debug("WAITING FOR SCENARIO");
77                 scenarioHandler.wait();
78             }
79         } catch (Exception ex) {
80             LOGGER.error(ex.getMessage(), ex);
81         } finally {
82             LOGGER.info("shutting down");
83             try {
84                 group.shutdownGracefully().get();
85                 LOGGER.info("shutdown succesful");
86             } catch (InterruptedException | ExecutionException e) {
87                 LOGGER.error(e.getMessage(), e);
88             }
89         }
90         scenarioDone.set(true);
91     }
92
93     /**
94      * @return close future
95      */
96     public Future<?> disconnect() {
97         LOGGER.debug("disconnecting client");
98         return group.shutdownGracefully();
99     }
100
101     /**
102      * @param securedClient
103      */
104     public void setSecuredClient(boolean securedClient) {
105         this.securedClient = securedClient;
106     }
107
108     /**
109      * Sets up {@link SimpleClient} and fires run()
110      *
111      * @param args
112      * @throws Exception
113      */
114     public static void main(String[] args) throws Exception {
115         String host;
116         int port;
117         SimpleClient sc;
118         if (args.length != 3) {
119             LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
120                     + " <host> <port> <secured>");
121             LOGGER.error("Trying to use default setting.");
122             InetAddress ia = InetAddress.getLocalHost();
123             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
124             host = all[0].getHostAddress();
125             port = 6633;
126             sc = new SimpleClient(host, port);
127             sc.setSecuredClient(true);
128         } else {
129             host = args[0];
130             port = Integer.parseInt(args[1]);
131             sc = new SimpleClient(host, port);
132             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
133         }
134         sc.start();
135         
136     }
137     
138     /**
139      * @return the isOnlineFuture
140      */
141     public SettableFuture<Boolean> getIsOnlineFuture() {
142         return isOnlineFuture;
143     }
144     
145     /**
146      * @return the scenarioDone
147      */
148     public SettableFuture<Boolean> getScenarioDone() {
149         return scenarioDone;
150     }
151     
152     /**
153      * @param scenario list of wanted actions
154      */
155     public void setScenarioHandler(ScenarioHandler scenario) {
156         this.scenarioHandler = scenario;
157     }
158 }