Merge "Fixing OF Multipart messages 1) So we have a MultipartRequestDesc message...
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientInitializer.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2
3 package org.opendaylight.openflowjava.protocol.impl.clients;
4
5 import io.netty.channel.ChannelInitializer;
6 import io.netty.channel.ChannelPipeline;
7 import io.netty.channel.socket.SocketChannel;
8 import io.netty.handler.ssl.SslHandler;
9
10 import javax.net.ssl.SSLEngine;
11
12 import org.opendaylight.openflowjava.protocol.impl.core.SslContextFactory;
13
14 import com.google.common.util.concurrent.SettableFuture;
15
16 /** Initializes secured {@link SimpleClient} pipeline
17  * 
18  * @author michal.polkorab
19  */
20 public class SimpleClientInitializer extends ChannelInitializer<SocketChannel> {
21     
22     private SettableFuture<Boolean> isOnlineFuture;
23     private boolean secured;
24     private ScenarioHandler scenarioHandler;
25
26     /**
27      * @param isOnlineFuture future notifier of connected channel
28      * @param secured true if {@link SimpleClient} should use encrypted communication
29      */
30     public SimpleClientInitializer(SettableFuture<Boolean> isOnlineFuture, boolean secured) {
31         this.isOnlineFuture = isOnlineFuture;
32         this.secured = secured;
33     }
34
35     @Override
36     public void initChannel(SocketChannel ch) throws Exception {
37         ChannelPipeline pipeline = ch.pipeline();
38         if (secured) {
39             SSLEngine engine = SslContextFactory.getClientContext()
40                     .createSSLEngine();
41             engine.setUseClientMode(true);
42             pipeline.addLast("ssl", new SslHandler(engine));
43             pipeline.addLast("framer", new SimpleClientFramer());
44         }
45         SimpleClientHandler simpleClientHandler = new SimpleClientHandler(isOnlineFuture, scenarioHandler);
46         simpleClientHandler.setScenario(scenarioHandler);
47         pipeline.addLast("handler", simpleClientHandler);
48         isOnlineFuture = null;
49
50     }
51
52     /**
53      * @param scenarioHandler handler of scenario events
54      */
55     public void setScenario(ScenarioHandler scenarioHandler) {
56         this.scenarioHandler = scenarioHandler;
57     }
58 }