2 * Copyright (c) 2015 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.pcep.impl;
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFutureListener;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
15 import io.netty.util.concurrent.Promise;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.protocol.pcep.SessionNegotiator;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public abstract class AbstractSessionNegotiator extends ChannelInboundHandlerAdapter implements SessionNegotiator {
23 private static final Logger LOG = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
24 protected final Channel channel;
25 protected final Promise<PCEPSessionImpl> promise;
27 protected AbstractSessionNegotiator(final Promise<PCEPSessionImpl> promise, final Channel channel) {
28 this.promise = Preconditions.checkNotNull(promise);
29 this.channel = Preconditions.checkNotNull(channel);
32 protected final void negotiationSuccessful(final PCEPSessionImpl session) {
33 LOG.debug("Negotiation on channel {} successful with session {}", this.channel, session);
34 this.channel.pipeline().replace(this, "session", session);
35 this.promise.setSuccess(session);
38 protected void negotiationFailed(final Throwable cause) {
39 LOG.debug("Negotiation on channel {} failed", this.channel, cause);
41 this.promise.setFailure(cause);
44 protected final void sendMessage(final Message msg) {
45 this.channel.writeAndFlush(msg).addListener((ChannelFutureListener) f -> {
47 LOG.info("Failed to send message {}", msg, f.cause());
48 negotiationFailed(f.cause());
50 LOG.trace("Message {} sent to socket", msg);
57 public final void channelActive(final ChannelHandlerContext ctx) {
58 LOG.debug("Starting session negotiation on channel {}", this.channel);
61 this.startNegotiation();
62 } catch (final Exception e) {
63 LOG.warn("Unexpected negotiation failure", e);
64 this.negotiationFailed(e);
70 public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
71 LOG.debug("Negotiation read invoked on channel {}", this.channel);
74 this.handleMessage((Message) msg);
75 } catch (Exception e) {
76 LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
77 this.negotiationFailed(e);
83 public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
84 LOG.info("Unexpected error during negotiation", cause);
85 this.negotiationFailed(cause);
88 protected abstract void startNegotiation() throws ExecutionException;
90 protected abstract void handleMessage(final Message msg);