BUG 2245 - Fixed Singular Field
[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 implements OFClient {
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 ScenarioHandler scenarioHandler;
40     
41     /**
42      * Constructor of class
43      *
44      * @param host address of host
45      * @param port host listening port
46      */
47     public SimpleClient(String host, int port) {
48         this.host = host;
49         this.port = port;
50         init();
51     }
52
53     private void init() {
54         isOnlineFuture = SettableFuture.create();
55         scenarioDone = SettableFuture.create();
56     }
57     
58     /**
59      * Starting class of {@link SimpleClient}
60      */
61     @Override
62     public void run() {
63         group = new NioEventLoopGroup();
64         SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
65         clientInitializer.setScenario(scenarioHandler);
66         try {
67             Bootstrap b = new Bootstrap();
68             b.group(group)
69                 .channel(NioSocketChannel.class)
70                 .handler(clientInitializer);
71
72             b.connect(host, port).sync();
73
74             synchronized (scenarioHandler) {
75                 LOGGER.debug("WAITING FOR SCENARIO");
76                 scenarioHandler.wait();
77             }
78         } catch (Exception ex) {
79             LOGGER.error(ex.getMessage(), ex);
80         } finally {
81             LOGGER.debug("shutting down");
82             try {
83                 group.shutdownGracefully().get();
84                 LOGGER.debug("shutdown succesful");
85             } catch (InterruptedException | ExecutionException e) {
86                 LOGGER.error(e.getMessage(), e);
87             }
88         }
89         scenarioDone.set(true);
90     }
91
92     /**
93      * @return close future
94      */
95     public Future<?> disconnect() {
96         LOGGER.debug("disconnecting client");
97         return group.shutdownGracefully();
98     }
99
100     @Override
101     public void setSecuredClient(boolean securedClient) {
102         this.securedClient = securedClient;
103     }
104
105     /**
106      * Sets up {@link SimpleClient} and fires run()
107      *
108      * @param args
109      * @throws Exception
110      */
111     public static void main(String[] args) throws Exception {
112         String host;
113         int port;
114         SimpleClient sc;
115         if (args.length != 3) {
116             LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
117                     + " <host> <port> <secured>");
118             LOGGER.error("Trying to use default setting.");
119             InetAddress ia = InetAddress.getLocalHost();
120             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
121             host = all[0].getHostAddress();
122             port = 6633;
123             sc = new SimpleClient(host, port);
124             sc.setSecuredClient(true);
125         } else {
126             host = args[0];
127             port = Integer.parseInt(args[1]);
128             sc = new SimpleClient(host, port);
129             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
130         }
131         sc.run();
132     }
133
134     @Override
135     public SettableFuture<Boolean> getIsOnlineFuture() {
136         return isOnlineFuture;
137     }
138
139     @Override
140     public SettableFuture<Boolean> getScenarioDone() {
141         return scenarioDone;
142     }
143
144     @Override
145     public void setScenarioHandler(ScenarioHandler scenario) {
146         this.scenarioHandler = scenario;
147     }
148 }