Merge "Refactor Additional header for netconf hello message."
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / AbstractChannelInitializer.java
index caee5421525515a2b9cef2b136cf00d2f5944fd5..0910d9403ad98af67427d9dc818e1ddcff216878 100644 (file)
@@ -10,12 +10,47 @@ package org.opendaylight.controller.netconf.util;
 
 import io.netty.channel.socket.SocketChannel;
 import io.netty.util.concurrent.Promise;
+
 import org.opendaylight.controller.netconf.api.NetconfSession;
+import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.controller.netconf.util.handler.NetconfHelloMessageToXMLEncoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
+import org.opendaylight.controller.netconf.util.handler.NetconfXMLToHelloMessageDecoder;
+import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
+
+public abstract class AbstractChannelInitializer<S extends NetconfSession> {
+
+    public static final String NETCONF_MESSAGE_DECODER = "netconfMessageDecoder";
+    public static final String NETCONF_MESSAGE_AGGREGATOR = "aggregator";
+    public static final String NETCONF_MESSAGE_ENCODER = "netconfMessageEncoder";
+    public static final String NETCONF_MESSAGE_FRAME_ENCODER = "frameEncoder";
+    public static final String NETCONF_SESSION_NEGOTIATOR = "negotiator";
+
+    public void initialize(SocketChannel ch, Promise<S> promise) {
+        ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfMessageAggregator(FramingMechanism.EOM));
+        initializeMessageDecoder(ch);
+        ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
+        initializeMessageEncoder(ch);
+
+        initializeSessionNegotiator(ch, promise);
+    }
 
-public abstract class AbstractChannelInitializer {
+    protected void initializeMessageEncoder(SocketChannel ch) {
+        // Special encoding handler for hello message to include additional header if available,
+        // it is thrown away after successful negotiation
+        ch.pipeline().addLast(NETCONF_MESSAGE_ENCODER, new NetconfHelloMessageToXMLEncoder());
+    }
 
-    public abstract void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise);
+    protected void initializeMessageDecoder(SocketChannel ch) {
+        // Special decoding handler for hello message to parse additional header if available,
+        // it is thrown away after successful negotiation
+        ch.pipeline().addLast(NETCONF_MESSAGE_DECODER, new NetconfXMLToHelloMessageDecoder());
+    }
 
-    protected abstract void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise);
+    /**
+     * Insert session negotiator into the pipeline. It must be inserted after message decoder
+     * identified by {@link AbstractChannelInitializer#NETCONF_MESSAGE_DECODER}, (or any other custom decoder processor)
+     */
+    protected abstract void initializeSessionNegotiator(SocketChannel ch, Promise<S> promise);
 
 }