blob: f6e982f906386c2398e827b5aa4194ee727776cc [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-present Open Networking Laboratory
Jonathan Hart3930f632015-10-19 12:12:51 -07003 *
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
17package org.onosproject.routing.fpm;
18
19import org.jboss.netty.channel.Channel;
20import org.jboss.netty.channel.ChannelHandlerContext;
21import org.jboss.netty.channel.ChannelStateEvent;
22import org.jboss.netty.channel.ExceptionEvent;
23import org.jboss.netty.channel.MessageEvent;
24import org.jboss.netty.channel.SimpleChannelHandler;
25import org.onosproject.routing.fpm.protocol.FpmHeader;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Jonathan Hartb10f1e72017-05-02 16:36:26 -070029import java.net.InetSocketAddress;
30import java.net.SocketAddress;
31
Jonathan Hart3930f632015-10-19 12:12:51 -070032import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Session handler for FPM protocol.
36 */
37public class FpmSessionHandler extends SimpleChannelHandler {
38
39 private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class);
40
Jonathan Hart6b045582016-02-03 10:00:08 -080041 private final FpmListener fpmListener;
Jonathan Hart3930f632015-10-19 12:12:51 -070042
43 private Channel channel;
Jonathan Hartb10f1e72017-05-02 16:36:26 -070044 private FpmPeer us;
Jonathan Hart3930f632015-10-19 12:12:51 -070045
46 /**
47 * Class constructor.
48 *
49 * @param fpmListener listener for FPM messages
50 */
Jonathan Hart6b045582016-02-03 10:00:08 -080051 public FpmSessionHandler(FpmListener fpmListener) {
Jonathan Hart3930f632015-10-19 12:12:51 -070052 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 Hartb10f1e72017-05-02 16:36:26 -070059 fpmListener.fpmMessage(us, fpmMessage);
Jonathan Hart3930f632015-10-19 12:12:51 -070060 }
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 Hart6b045582016-02-03 10:00:08 -080066 if (channel != null) {
67 channel.close();
68 }
Jonathan Hart3930f632015-10-19 12:12:51 -070069 handleDisconnect();
70 }
71
72 @Override
73 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
74 throws Exception {
Jonathan Hartb10f1e72017-05-02 16:36:26 -070075 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 Hart3930f632015-10-19 12:12:51 -070084 log.error("Received new FPM connection while already connected");
85 ctx.getChannel().close();
86 return;
87 }
88
Jonathan Hart6b045582016-02-03 10:00:08 -080089 channel = ctx.getChannel();
Jonathan Hart3930f632015-10-19 12:12:51 -070090 }
91
92 @Override
93 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
94 throws Exception {
Jonathan Hart3930f632015-10-19 12:12:51 -070095 }
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 Hart3930f632015-10-19 12:12:51 -0700106 }
107
108 private void handleDisconnect() {
Jonathan Hartb10f1e72017-05-02 16:36:26 -0700109 if (us != null) {
110 fpmListener.peerDisconnected(us);
111 }
Jonathan Hart6b045582016-02-03 10:00:08 -0800112 channel = null;
Jonathan Hart3930f632015-10-19 12:12:51 -0700113 }
114}