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; |
| 24 | import org.jboss.netty.channel.SimpleChannelHandler; |
| 25 | import org.onosproject.routing.fpm.protocol.FpmHeader; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 29 | import java.net.InetSocketAddress; |
| 30 | import java.net.SocketAddress; |
| 31 | |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Session handler for FPM protocol. |
| 36 | */ |
| 37 | public class FpmSessionHandler extends SimpleChannelHandler { |
| 38 | |
| 39 | private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class); |
| 40 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 41 | private final FpmListener fpmListener; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 42 | |
| 43 | private Channel channel; |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 44 | private FpmPeer us; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Class constructor. |
| 48 | * |
| 49 | * @param fpmListener listener for FPM messages |
| 50 | */ |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 51 | public FpmSessionHandler(FpmListener fpmListener) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 52 | this.fpmListener = checkNotNull(fpmListener); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) |
| 57 | throws Exception { |
| 58 | FpmHeader fpmMessage = (FpmHeader) e.getMessage(); |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 59 | fpmListener.fpmMessage(us, fpmMessage); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) |
| 64 | throws Exception { |
| 65 | log.error("Exception thrown while handling FPM message", e.getCause()); |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 66 | if (channel != null) { |
| 67 | channel.close(); |
| 68 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 69 | handleDisconnect(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 74 | throws Exception { |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 75 | SocketAddress socketAddress = ctx.getChannel().getRemoteAddress(); |
| 76 | |
| 77 | if (!(socketAddress instanceof InetSocketAddress)) { |
| 78 | throw new IllegalStateException("Address type is not InetSocketAddress"); |
| 79 | } |
| 80 | |
| 81 | us = FpmPeer.fromSocketAddress((InetSocketAddress) socketAddress); |
| 82 | |
| 83 | if (!fpmListener.peerConnected(us)) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 84 | log.error("Received new FPM connection while already connected"); |
| 85 | ctx.getChannel().close(); |
| 86 | return; |
| 87 | } |
| 88 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 89 | channel = ctx.getChannel(); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 94 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 99 | throws Exception { |
| 100 | handleDisconnect(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 105 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void handleDisconnect() { |
Jonathan Hart | b10f1e7 | 2017-05-02 16:36:26 -0700 | [diff] [blame^] | 109 | if (us != null) { |
| 110 | fpmListener.peerDisconnected(us); |
| 111 | } |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 112 | channel = null; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 113 | } |
| 114 | } |