/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.protocol.framework;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.Promise;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

/**
 * Abstract base class for session negotiators. It implements the basic
 * substrate to implement SessionNegotiator API specification, with subclasses
 * needing to provide only
 *
 * @param <M> Protocol message type
 * @param <S> Protocol session type, has to extend ProtocolSession<M>
 */
public abstract class AbstractSessionNegotiator<M, S extends AbstractProtocolSession<?>> extends ChannelInboundHandlerAdapter implements SessionNegotiator<S> {
    private final Logger logger = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
    private final Promise<S> promise;
    protected final Channel channel;

    public AbstractSessionNegotiator(final Promise<S> promise, final Channel channel) {
        this.promise = Preconditions.checkNotNull(promise);
        this.channel = Preconditions.checkNotNull(channel);
    }

    protected abstract void startNegotiation() throws Exception;
    protected abstract void handleMessage(M msg) throws Exception;

    protected final void negotiationSuccessful(final S session) {
        logger.debug("Negotiation on channel {} successful with session {}", channel, session);
        channel.pipeline().replace(this, "session", session);
        promise.setSuccess(session);
    }

    protected final void negotiationFailed(final Throwable cause) {
        logger.debug("Negotiation on channel {} failed", channel, cause);
        channel.close();
        promise.setFailure(cause);
    }

    @Override
    public final void channelActive(final ChannelHandlerContext ctx) {
        logger.debug("Starting session negotiation on channel {}", channel);
        try {
            startNegotiation();
        } catch (Exception e) {
            logger.warn("Unexpected negotiation failure", e);
            negotiationFailed(e);
        }
    }

    @Override
    public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
        logger.debug("Negotiation read invoked on channel {}", channel);
        try {
            handleMessage((M)msg);
        } catch (Exception e) {
            logger.debug("Unexpected error while handling negotiation message {}", msg, e);
            negotiationFailed(e);
        }
    }

    @Override
    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
        logger.info("Unexpected error during negotiation", cause);
        negotiationFailed(cause);
    }
}

@@ -380,16 +380,102 @@
 hannel;%0A
+import io.netty.channel.ChannelFuture;%0Aimport io.netty.channel.ChannelFutureListener;%0A
 import i
@@ -1193,22 +1193,19 @@
  Logger 
-logger
+LOG
  = Logge
@@ -1747,38 +1747,35 @@
 sion) %7B%0A        
-logger
+LOG
 .debug(%22Negotiat
@@ -2014,38 +2014,35 @@
 ause) %7B%0A        
-logger
+LOG
 .debug(%22Negotiat
@@ -2144,32 +2144,816 @@
 (cause);%0A    %7D%0A%0A
+    /**%0A     * Send a message to peer and fail negotiation if it does not reach%0A     * the peer.%0A     *%0A     * @param msg Message which should be sent.%0A     */%0A    protected final void sendMessage(final M msg) %7B%0A        this.channel.writeAndFlush(msg).addListener(%0A                new ChannelFutureListener() %7B%0A                    @Override%0A                    public void operationComplete(final ChannelFuture f) %7B%0A                        if (!f.isSuccess()) %7B%0A                            LOG.info(%22Failed to send message %7B%7D%22, msg, f.cause());%0A                            negotiationFailed(f.cause());%0A                        %7D else %7B%0A                            LOG.trace(%22Message %7B%7D sent to socket%22, msg);%0A                        %7D%0A                    %7D%0A                %7D);%0A    %7D%0A%0A
     @Override%0A  
@@ -3021,38 +3021,35 @@
  ctx) %7B%0A        
-logger
+LOG
 .debug(%22Starting
@@ -3185,22 +3185,19 @@
         
-logger
+LOG
 .warn(%22U
@@ -3384,38 +3384,35 @@
  msg) %7B%0A        
-logger
+LOG
 .debug(%22Negotiat
@@ -3547,22 +3547,19 @@
         
-logger
+LOG
 .debug(%22
@@ -3791,14 +3791,11 @@
     
-logger
+LOG
 .inf

Introduce checked sendMessage() method