Deprecate protocol-framework tests
[netconf.git] / protocol-framework / src / main / java / org / opendaylight / protocol / framework / AbstractSessionNegotiator.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.protocol.framework;
9
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;
17
18 /**
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
22  *
23  * @param <M> Protocol message type
24  * @param <S> Protocol session type, has to extend {@code ProtocolSession<M>}
25  */
26 @Deprecated
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;
32
33     public AbstractSessionNegotiator(final Promise<S> promise, final Channel channel) {
34         this.promise = Preconditions.checkNotNull(promise);
35         this.channel = Preconditions.checkNotNull(channel);
36     }
37
38     protected abstract void startNegotiation() throws Exception;
39     protected abstract void handleMessage(M msg) throws Exception;
40
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);
45     }
46
47     protected void negotiationFailed(final Throwable cause) {
48         LOG.debug("Negotiation on channel {} failed", channel, cause);
49         channel.close();
50         promise.setFailure(cause);
51     }
52
53     /**
54      * Send a message to peer and fail negotiation if it does not reach
55      * the peer.
56      *
57      * @param msg Message which should be sent.
58      */
59     protected final void sendMessage(final M msg) {
60         this.channel.writeAndFlush(msg).addListener(f -> {
61             if (!f.isSuccess()) {
62                 LOG.info("Failed to send message {}", msg, f.cause());
63                 negotiationFailed(f.cause());
64             } else {
65                 LOG.trace("Message {} sent to socket", msg);
66             }
67         });
68     }
69
70     @Override
71     public final void channelActive(final ChannelHandlerContext ctx) {
72         LOG.debug("Starting session negotiation on channel {}", channel);
73         try {
74             startNegotiation();
75         } catch (final Exception e) {
76             LOG.warn("Unexpected negotiation failure", e);
77             negotiationFailed(e);
78         }
79     }
80
81     @Override
82     @SuppressWarnings("unchecked")
83     public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
84         LOG.debug("Negotiation read invoked on channel {}", channel);
85         try {
86             handleMessage((M)msg);
87         } catch (final Exception e) {
88             LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
89             negotiationFailed(e);
90         }
91     }
92
93     @Override
94     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
95         LOG.info("Unexpected error during negotiation", cause);
96         negotiationFailed(cause);
97     }
98 }