Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.routing.fpm; |
| 18 | |
| 19 | import org.jboss.netty.channel.Channel; |
| 20 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 21 | import org.jboss.netty.channel.ChannelStateEvent; |
| 22 | import org.jboss.netty.channel.ExceptionEvent; |
| 23 | import org.jboss.netty.channel.MessageEvent; |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 24 | import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; |
| 25 | import org.jboss.netty.handler.timeout.IdleStateEvent; |
| 26 | import org.jboss.netty.handler.timeout.ReadTimeoutException; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 27 | import org.onosproject.routing.fpm.protocol.FpmHeader; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame] | 31 | import java.net.InetSocketAddress; |
| 32 | import java.net.SocketAddress; |
| 33 | |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * Session handler for FPM protocol. |
| 38 | */ |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 39 | public class FpmSessionHandler extends IdleStateAwareChannelHandler { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 40 | |
| 41 | private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class); |
| 42 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 43 | private final FpmListener fpmListener; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 44 | |
| 45 | private Channel channel; |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame] | 46 | private FpmPeer us; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 47 | |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 48 | private boolean useKeepalives; |
| 49 | private boolean initialized; |
| 50 | |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Class constructor. |
| 53 | * |
| 54 | * @param fpmListener listener for FPM messages |
| 55 | */ |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 56 | public FpmSessionHandler(FpmListener fpmListener) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 57 | this.fpmListener = checkNotNull(fpmListener); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) |
| 62 | throws Exception { |
| 63 | FpmHeader fpmMessage = (FpmHeader) e.getMessage(); |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 64 | |
| 65 | initConnection(ctx, fpmMessage); |
| 66 | |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame] | 67 | fpmListener.fpmMessage(us, fpmMessage); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 70 | private void initConnection(ChannelHandlerContext ctx, FpmHeader message) { |
| 71 | if (!initialized) { |
| 72 | useKeepalives = message.version() >= FpmHeader.FPM_VERSION_ONOS_EXT; |
| 73 | if (useKeepalives) { |
| 74 | log.info("Using keepalives"); |
| 75 | } else { |
| 76 | log.info("Not using keepalives"); |
| 77 | // Remove the idle channel handler if using a protocol version |
| 78 | // with no keepalive messages |
| 79 | ctx.getPipeline().remove("idle"); |
| 80 | } |
| 81 | initialized = true; |
| 82 | } |
| 83 | } |
| 84 | |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) |
| 87 | throws Exception { |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 88 | if (e.getCause() instanceof ReadTimeoutException) { |
| 89 | log.warn("Haven't heard from FPM client for a while"); |
| 90 | } else { |
| 91 | log.error("Exception thrown while handling FPM message", e.getCause()); |
| 92 | } |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 93 | if (channel != null) { |
| 94 | channel.close(); |
| 95 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 96 | handleDisconnect(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 101 | throws Exception { |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame] | 102 | SocketAddress socketAddress = ctx.getChannel().getRemoteAddress(); |
| 103 | |
| 104 | if (!(socketAddress instanceof InetSocketAddress)) { |
| 105 | throw new IllegalStateException("Address type is not InetSocketAddress"); |
| 106 | } |
| 107 | |
| 108 | us = FpmPeer.fromSocketAddress((InetSocketAddress) socketAddress); |
| 109 | |
| 110 | if (!fpmListener.peerConnected(us)) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 111 | log.error("Received new FPM connection while already connected"); |
| 112 | ctx.getChannel().close(); |
| 113 | return; |
| 114 | } |
| 115 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 116 | channel = ctx.getChannel(); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 121 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 126 | throws Exception { |
| 127 | handleDisconnect(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 132 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private void handleDisconnect() { |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame] | 136 | if (us != null) { |
| 137 | fpmListener.peerDisconnected(us); |
| 138 | } |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 139 | channel = null; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 140 | } |
Jonathan Hart | 72bbf88 | 2017-04-14 08:42:51 -0700 | [diff] [blame^] | 141 | |
| 142 | @Override |
| 143 | public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) |
| 144 | throws Exception { |
| 145 | log.warn("FPM channel idle"); |
| 146 | if (useKeepalives) { |
| 147 | ctx.getChannel().close(); |
| 148 | } |
| 149 | super.channelIdle(ctx, e); |
| 150 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 151 | } |