Bring some reliability in the eclipse and maven mixed builds
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / AbstractChannelInitializer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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 package org.opendaylight.controller.netconf.util;
10
11 import javax.net.ssl.SSLContext;
12 import javax.net.ssl.SSLEngine;
13
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfSession;
16 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
17 import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
18 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
19 import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory;
20 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
21 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
22 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
23
24 import com.google.common.base.Optional;
25
26 import io.netty.channel.ChannelHandler;
27 import io.netty.channel.socket.SocketChannel;
28 import io.netty.handler.ssl.SslHandler;
29 import io.netty.util.concurrent.Promise;
30
31 public abstract class AbstractChannelInitializer {
32
33     private final Optional<SSLContext> maybeContext;
34     private final NetconfHandlerFactory handlerFactory;
35
36     public AbstractChannelInitializer(Optional<SSLContext> maybeContext) {
37         this.maybeContext = maybeContext;
38         this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
39     }
40
41     public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
42         if (maybeContext.isPresent()) {
43             initSsl(ch);
44         }
45
46         ch.pipeline().addLast("aggregator", new NetconfMessageAggregator(FramingMechanism.EOM));
47         ch.pipeline().addLast(handlerFactory.getDecoders());
48         initializeAfterDecoder(ch, promise);
49         ch.pipeline().addLast("frameEncoder", FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
50         ch.pipeline().addLast(handlerFactory.getEncoders());
51     }
52
53     protected abstract void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise);
54
55     private void initSsl(SocketChannel ch) {
56         SSLEngine sslEngine = maybeContext.get().createSSLEngine();
57         initSslEngine(sslEngine);
58         final SslHandler handler = new SslHandler(sslEngine);
59         ch.pipeline().addLast("ssl", handler);
60     }
61
62     protected abstract void initSslEngine(SSLEngine sslEngine);
63
64     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
65
66         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
67             super(msgFactory);
68         }
69
70         @Override
71         public ChannelHandler[] getEncoders() {
72             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
73         }
74
75         @Override
76         public ChannelHandler[] getDecoders() {
77             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
78         }
79     }
80 }