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