89854c7eff0a1a9a3ed5327203d3c8e21b403066
[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                 while (! scenarioHandler.isScenarioFinished()) {
77                     scenarioHandler.wait();
78                 }
79             }
80         } catch (Exception ex) {
81             LOGGER.error(ex.getMessage(), ex);
82         } finally {
83             LOGGER.debug("shutting down");
84             try {
85                 group.shutdownGracefully().get();
86                 LOGGER.debug("shutdown succesful");
87             } catch (InterruptedException | ExecutionException e) {
88                 LOGGER.error(e.getMessage(), e);
89             }
90         }
91         scenarioDone.set(true);
92     }
93
94     /**
95      * @return close future
96      */
97     public Future<?> disconnect() {
98         LOGGER.debug("disconnecting client");
99         return group.shutdownGracefully();
100     }
101
102     @Override
103     public void setSecuredClient(boolean securedClient) {
104         this.securedClient = securedClient;
105     }
106
107     /**
108      * Sets up {@link SimpleClient} and fires run()
109      *
110      * @param args
111      * @throws Exception
112      */
113     public static void main(String[] args) throws Exception {
114         String host;
115         int port;
116         SimpleClient sc;
117         if (args.length != 3) {
118             LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
119                     + " <host> <port> <secured>");
120             LOGGER.error("Trying to use default setting.");
121             InetAddress ia = InetAddress.getLocalHost();
122             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
123             host = all[0].getHostAddress();
124             port = 6633;
125             sc = new SimpleClient(host, port);
126             sc.setSecuredClient(true);
127         } else {
128             host = args[0];
129             port = Integer.parseInt(args[1]);
130             sc = new SimpleClient(host, port);
131             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
132         }
133         sc.run();
134     }
135
136     @Override
137     public SettableFuture<Boolean> getIsOnlineFuture() {
138         return isOnlineFuture;
139     }
140
141     @Override
142     public SettableFuture<Boolean> getScenarioDone() {
143         return scenarioDone;
144     }
145
146     @Override
147     public void setScenarioHandler(ScenarioHandler scenario) {
148         this.scenarioHandler = scenario;
149     }
150 }