2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.framework;
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelInboundHandlerAdapter;
14 import io.netty.util.concurrent.Promise;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Abstract base class for session negotiators. It implements the basic
20 * substrate to implement SessionNegotiator API specification, with subclasses
21 * needing to provide only
23 * @param <M> Protocol message type
24 * @param <S> Protocol session type, has to extend {@code ProtocolSession<M>}
27 public abstract class AbstractSessionNegotiator<M, S extends AbstractProtocolSession<?>>
28 extends ChannelInboundHandlerAdapter implements SessionNegotiator<S> {
29 private final Logger LOG = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
30 private final Promise<S> promise;
31 protected final Channel channel;
33 public AbstractSessionNegotiator(final Promise<S> promise, final Channel channel) {
34 this.promise = Preconditions.checkNotNull(promise);
35 this.channel = Preconditions.checkNotNull(channel);
38 protected abstract void startNegotiation() throws Exception;
39 protected abstract void handleMessage(M msg) throws Exception;
41 protected final void negotiationSuccessful(final S session) {
42 LOG.debug("Negotiation on channel {} successful with session {}", channel, session);
43 channel.pipeline().replace(this, "session", session);
44 promise.setSuccess(session);
47 protected void negotiationFailed(final Throwable cause) {
48 LOG.debug("Negotiation on channel {} failed", channel, cause);
50 promise.setFailure(cause);
54 * Send a message to peer and fail negotiation if it does not reach
57 * @param msg Message which should be sent.
59 protected final void sendMessage(final M msg) {
60 this.channel.writeAndFlush(msg).addListener(f -> {
62 LOG.info("Failed to send message {}", msg, f.cause());
63 negotiationFailed(f.cause());
65 LOG.trace("Message {} sent to socket", msg);
71 public final void channelActive(final ChannelHandlerContext ctx) {
72 LOG.debug("Starting session negotiation on channel {}", channel);
75 } catch (final Exception e) {
76 LOG.warn("Unexpected negotiation failure", e);
82 @SuppressWarnings("unchecked")
83 public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
84 LOG.debug("Negotiation read invoked on channel {}", channel);
86 handleMessage((M)msg);
87 } catch (final Exception e) {
88 LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
94 public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
95 LOG.info("Unexpected error during negotiation", cause);
96 negotiationFailed(cause);