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