Fix Java 8 javadoc compatibility
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ListeningSimpleClient.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.openflowjava.protocol.impl.clients;
9
10 import io.netty.bootstrap.ServerBootstrap;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.channel.socket.nio.NioServerSocketChannel;
15 import io.netty.util.concurrent.Future;
16
17 import java.net.InetSocketAddress;
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  * Listening client for testing purposes
27  * @author martin.uhlir
28  *
29  */
30 public class ListeningSimpleClient implements OFClient {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(ListeningSimpleClient.class);
33     private int port;
34     private boolean securedClient = false;
35     private EventLoopGroup workerGroup;
36     private SettableFuture<Boolean> isOnlineFuture;
37     private SettableFuture<Boolean> scenarioDone;
38     private ScenarioHandler scenarioHandler;
39
40     /**
41      * Constructor of the class
42      *
43      * @param port host listening port
44      */
45     public ListeningSimpleClient(int port) {
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 ListeningSimpleClient}
57      */
58     @Override
59     public void run() {
60         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
61         workerGroup = new NioEventLoopGroup();
62         SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
63         clientInitializer.setScenario(scenarioHandler);
64         try {
65             ServerBootstrap b = new ServerBootstrap();
66             b.group(bossGroup, workerGroup)
67                 .channel(NioServerSocketChannel.class)
68                 .childHandler(clientInitializer);
69
70             ChannelFuture f = b.bind(port).sync();
71             // Update port, as it may have been specified as 0
72             this.port = ((InetSocketAddress) f.channel().localAddress()).getPort();
73             isOnlineFuture.set(true);
74
75             synchronized (scenarioHandler) {
76                 LOGGER.debug("WAITING FOR SCENARIO");
77                 while (! scenarioHandler.isScenarioFinished()) {
78                     scenarioHandler.wait();
79                 }
80             }
81         } catch (Exception ex) {
82             LOGGER.error(ex.getMessage(), ex);
83         } finally {
84             LOGGER.debug("listening client shutting down");
85             try {
86                 workerGroup.shutdownGracefully().get();
87                 bossGroup.shutdownGracefully().get();
88                 LOGGER.debug("listening client shutdown succesful");
89             } catch (InterruptedException | ExecutionException e) {
90                 LOGGER.error(e.getMessage(), e);
91             }
92         }
93         scenarioDone.set(true);
94     }
95
96     /**
97      * @return close future
98      */
99     public Future<?> disconnect() {
100         LOGGER.debug("disconnecting client");
101         return workerGroup.shutdownGracefully();
102     }
103
104     @Override
105     public void setSecuredClient(boolean securedClient) {
106         this.securedClient = securedClient;
107     }
108
109     @Override
110     public SettableFuture<Boolean> getIsOnlineFuture() {
111         return isOnlineFuture;
112     }
113
114     @Override
115     public SettableFuture<Boolean> getScenarioDone() {
116         return scenarioDone;
117     }
118
119     @Override
120     public void setScenarioHandler(ScenarioHandler scenario) {
121         this.scenarioHandler = scenario;
122     }
123
124     /**
125      * @return actual port number
126      */
127     public int getPort() {
128         return this.port;
129     }
130 }