OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / samples / 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 com.google.common.util.concurrent.SettableFuture;
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioServerSocketChannel;
16 import io.netty.util.concurrent.Future;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutionException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Listening client for testing purposes.
24  *
25  * @author martin.uhlir
26  */
27 public class ListeningSimpleClient implements OFClient {
28
29     private static final Logger LOG = LoggerFactory.getLogger(ListeningSimpleClient.class);
30     private int port;
31     private boolean securedClient = false;
32     private EventLoopGroup workerGroup;
33     private SettableFuture<Boolean> isOnlineFuture;
34     private SettableFuture<Boolean> scenarioDone;
35     private ScenarioHandler scenarioHandler;
36
37     /**
38      * Constructor of the class.
39      *
40      * @param port host listening port
41      */
42     public ListeningSimpleClient(int port) {
43         this.port = port;
44         init();
45     }
46
47     private void init() {
48         isOnlineFuture = SettableFuture.create();
49         scenarioDone = SettableFuture.create();
50     }
51
52     /**
53      * Starting class of {@link ListeningSimpleClient}.
54      */
55     @Override
56     public void run() {
57         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
58         workerGroup = new NioEventLoopGroup();
59         SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
60         clientInitializer.setScenario(scenarioHandler);
61         try {
62             ServerBootstrap bootstrap = new ServerBootstrap();
63             bootstrap.group(bossGroup, workerGroup)
64                 .channel(NioServerSocketChannel.class)
65                 .childHandler(clientInitializer);
66
67             ChannelFuture future = bootstrap.bind(port).sync();
68             // Update port, as it may have been specified as 0
69             this.port = ((InetSocketAddress) future.channel().localAddress()).getPort();
70             isOnlineFuture.set(true);
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("listening client shutting down");
82             try {
83                 workerGroup.shutdownGracefully().get();
84                 bossGroup.shutdownGracefully().get();
85                 LOG.debug("listening client shutdown succesful");
86             } catch (InterruptedException | ExecutionException e) {
87                 LOG.error("Error {}", e);
88             }
89         }
90         scenarioDone.set(true);
91     }
92
93     /**
94      * Disconnect.
95      *
96      * @return close future
97      */
98     public Future<?> disconnect() {
99         LOG.debug("disconnecting client");
100         return workerGroup.shutdownGracefully();
101     }
102
103     @Override
104     public void setSecuredClient(boolean securedClient) {
105         this.securedClient = securedClient;
106     }
107
108     @Override
109     public SettableFuture<Boolean> getIsOnlineFuture() {
110         return isOnlineFuture;
111     }
112
113     @Override
114     public SettableFuture<Boolean> getScenarioDone() {
115         return scenarioDone;
116     }
117
118     @Override
119     public void setScenarioHandler(ScenarioHandler scenario) {
120         this.scenarioHandler = scenario;
121     }
122
123     /**
124      * Returns the actual port number.
125      */
126     public int getPort() {
127         return this.port;
128     }
129 }