clean pcep/impl
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / AbstractSessionNegotiator.java
1 /*
2  * Copyright (c) 2015 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.pcep.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFutureListener;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.util.concurrent.Promise;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.protocol.pcep.SessionNegotiator;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public abstract class AbstractSessionNegotiator extends ChannelInboundHandlerAdapter implements SessionNegotiator {
24     private static final Logger LOG = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
25     protected final Channel channel;
26     protected final Promise<PCEPSessionImpl> promise;
27
28     protected AbstractSessionNegotiator(final Promise<PCEPSessionImpl> promise, final Channel channel) {
29         this.promise = requireNonNull(promise);
30         this.channel = requireNonNull(channel);
31     }
32
33     protected final void negotiationSuccessful(final PCEPSessionImpl session) {
34         LOG.debug("Negotiation on channel {} successful with session {}", this.channel, session);
35         this.channel.pipeline().replace(this, "session", session);
36         this.promise.setSuccess(session);
37     }
38
39     protected void negotiationFailed(final Throwable cause) {
40         LOG.debug("Negotiation on channel {} failed", this.channel, cause);
41         this.channel.close();
42         this.promise.setFailure(cause);
43     }
44
45     protected final void sendMessage(final Message msg) {
46         this.channel.writeAndFlush(msg).addListener((ChannelFutureListener) f -> {
47             if (!f.isSuccess()) {
48                 LOG.info("Failed to send message {}", msg, f.cause());
49                 negotiationFailed(f.cause());
50             } else {
51                 LOG.trace("Message {} sent to socket", msg);
52             }
53
54         });
55     }
56
57     @Override
58     @SuppressWarnings("checkstyle:IllegalCatch")
59     public final void channelActive(final ChannelHandlerContext ctx) {
60         LOG.debug("Starting session negotiation on channel {}", this.channel);
61
62         try {
63             this.startNegotiation();
64         } catch (final Exception e) {
65             LOG.warn("Unexpected negotiation failure", e);
66             this.negotiationFailed(e);
67         }
68
69     }
70
71     @Override
72     @SuppressWarnings("checkstyle:IllegalCatch")
73     public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
74         LOG.debug("Negotiation read invoked on channel {}", this.channel);
75
76         try {
77             this.handleMessage((Message) msg);
78         } catch (Exception e) {
79             LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
80             this.negotiationFailed(e);
81         }
82
83     }
84
85     @Override
86     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
87         LOG.info("Unexpected error during negotiation", cause);
88         this.negotiationFailed(cause);
89     }
90
91     protected abstract void startNegotiation() throws ExecutionException;
92
93     protected abstract void handleMessage(Message msg);
94 }