Initial code drop of netconf protocol implementation
[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.messages.NetconfMessageFactory;
17 import org.opendaylight.protocol.framework.ProtocolHandlerFactory;
18 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
19 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
20
21 import com.google.common.base.Optional;
22
23 import io.netty.channel.ChannelHandler;
24 import io.netty.channel.socket.SocketChannel;
25 import io.netty.handler.ssl.SslHandler;
26 import io.netty.util.concurrent.Promise;
27
28 public abstract class AbstractChannelInitializer {
29
30     private final Optional<SSLContext> maybeContext;
31     private final NetconfHandlerFactory handlerFactory;
32
33     public AbstractChannelInitializer(Optional<SSLContext> maybeContext) {
34         this.maybeContext = maybeContext;
35         this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory());
36     }
37
38     public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
39         if (maybeContext.isPresent()) {
40             initSsl(ch);
41         }
42
43         ch.pipeline().addLast("frameDecoder", NetconfMessageFactory.getDelimiterFrameDecoder());
44         ch.pipeline().addLast(handlerFactory.getDecoders());
45         initializeAfterDecoder(ch, promise);
46         ch.pipeline().addLast(handlerFactory.getEncoders());
47     }
48
49     protected abstract void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise);
50
51     private void initSsl(SocketChannel ch) {
52         SSLEngine sslEngine = maybeContext.get().createSSLEngine();
53         initSslEngine(sslEngine);
54         final SslHandler handler = new SslHandler(sslEngine);
55         ch.pipeline().addLast("ssl", handler);
56     }
57
58     protected abstract void initSslEngine(SSLEngine sslEngine);
59
60     private static final class NetconfHandlerFactory extends ProtocolHandlerFactory<NetconfMessage> {
61
62         public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) {
63             super(msgFactory);
64         }
65
66         @Override
67         public ChannelHandler[] getEncoders() {
68             return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) };
69         }
70
71         @Override
72         public ChannelHandler[] getDecoders() {
73             return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) };
74         }
75     }
76 }