Refactor FramingMechanismHandlerFactory
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / 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 package org.opendaylight.netconf.nettyutil;
9
10 import io.netty.channel.Channel;
11 import io.netty.util.concurrent.Promise;
12 import org.opendaylight.netconf.api.NetconfSession;
13 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
14 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
15 import org.opendaylight.netconf.nettyutil.handler.NetconfHelloMessageToXMLEncoder;
16 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
17
18 public abstract class AbstractChannelInitializer<S extends NetconfSession> {
19     public static final String NETCONF_MESSAGE_DECODER = "netconfMessageDecoder";
20     public static final String NETCONF_MESSAGE_AGGREGATOR = "aggregator";
21     public static final String NETCONF_MESSAGE_ENCODER = "netconfMessageEncoder";
22     public static final String NETCONF_MESSAGE_FRAME_ENCODER = "frameEncoder";
23     public static final String NETCONF_SESSION_NEGOTIATOR = "negotiator";
24
25     public void initialize(final Channel ch, final Promise<S> promise) {
26         ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
27         initializeMessageDecoder(ch);
28         ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, new EOMFramingMechanismEncoder());
29         initializeMessageEncoder(ch);
30
31         initializeSessionNegotiator(ch, promise);
32     }
33
34     protected void initializeMessageEncoder(final Channel ch) {
35         // Special encoding handler for hello message to include additional header if available,
36         // it is thrown away after successful negotiation
37         ch.pipeline().addLast(NETCONF_MESSAGE_ENCODER, new NetconfHelloMessageToXMLEncoder());
38     }
39
40     protected void initializeMessageDecoder(final Channel ch) {
41         // Special decoding handler for hello message to parse additional header if available,
42         // it is thrown away after successful negotiation
43         ch.pipeline().addLast(NETCONF_MESSAGE_DECODER, new NetconfXMLToHelloMessageDecoder());
44     }
45
46     /**
47      * Insert session negotiator into the pipeline. It must be inserted after message decoder
48      * identified by {@link AbstractChannelInitializer#NETCONF_MESSAGE_DECODER}, (or any other custom decoder processor)
49      */
50     protected abstract void initializeSessionNegotiator(Channel ch, Promise<S> promise);
51 }