c44a660cd08de933c82a95ac4b5cc647f1d80de0
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientInitializer.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
10 package org.opendaylight.openflowjava.protocol.impl.clients;
11
12 import io.netty.channel.ChannelInitializer;
13 import io.netty.channel.ChannelPipeline;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import io.netty.handler.ssl.SslHandler;
16
17 import javax.net.ssl.SSLEngine;
18
19 import com.google.common.util.concurrent.SettableFuture;
20
21 /** Initializes secured {@link SimpleClient} pipeline
22  * 
23  * @author michal.polkorab
24  */
25 public class SimpleClientInitializer extends ChannelInitializer<NioSocketChannel> {
26     
27     private SettableFuture<Boolean> isOnlineFuture;
28     private boolean secured;
29     private ScenarioHandler scenarioHandler;
30
31     /**
32      * @param isOnlineFuture future notifier of connected channel
33      * @param secured true if {@link SimpleClient} should use encrypted communication
34      */
35     public SimpleClientInitializer(SettableFuture<Boolean> isOnlineFuture, boolean secured) {
36         this.isOnlineFuture = isOnlineFuture;
37         this.secured = secured;
38     }
39
40     @Override
41     public void initChannel(NioSocketChannel ch) throws Exception {
42         ChannelPipeline pipeline = ch.pipeline();
43         if (secured) {
44             SSLEngine engine = ClientSslContextFactory.getClientContext()
45                     .createSSLEngine();
46             engine.setUseClientMode(true);
47             pipeline.addLast("ssl", new SslHandler(engine));
48         }
49         SimpleClientHandler simpleClientHandler = new SimpleClientHandler(isOnlineFuture, scenarioHandler);
50         simpleClientHandler.setScenario(scenarioHandler);
51         pipeline.addLast("framer", new SimpleClientFramer());
52         pipeline.addLast("handler", simpleClientHandler);
53         isOnlineFuture = null;
54
55     }
56
57     /**
58      * @param scenarioHandler handler of scenario events
59      */
60     public void setScenario(ScenarioHandler scenarioHandler) {
61         this.scenarioHandler = scenarioHandler;
62     }
63 }