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