blob: 33f845ec5659c393416be7d91a00275b061b5fd7 [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
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Session handler for FPM protocol.
33 */
34public class FpmSessionHandler extends SimpleChannelHandler {
35
36 private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class);
37
Jonathan Hart6b045582016-02-03 10:00:08 -080038 private final FpmListener fpmListener;
Jonathan Hart3930f632015-10-19 12:12:51 -070039
40 private Channel channel;
41
42 /**
43 * Class constructor.
44 *
45 * @param fpmListener listener for FPM messages
46 */
Jonathan Hart6b045582016-02-03 10:00:08 -080047 public FpmSessionHandler(FpmListener fpmListener) {
Jonathan Hart3930f632015-10-19 12:12:51 -070048 this.fpmListener = checkNotNull(fpmListener);
49 }
50
51 @Override
52 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
53 throws Exception {
54 FpmHeader fpmMessage = (FpmHeader) e.getMessage();
55 fpmListener.fpmMessage(fpmMessage);
56 }
57
58 @Override
59 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
60 throws Exception {
61 log.error("Exception thrown while handling FPM message", e.getCause());
Jonathan Hart6b045582016-02-03 10:00:08 -080062 if (channel != null) {
63 channel.close();
64 }
Jonathan Hart3930f632015-10-19 12:12:51 -070065 handleDisconnect();
66 }
67
68 @Override
69 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
70 throws Exception {
Jonathan Hart6b045582016-02-03 10:00:08 -080071 if (!fpmListener.peerConnected(ctx.getChannel().getRemoteAddress())) {
Jonathan Hart3930f632015-10-19 12:12:51 -070072 log.error("Received new FPM connection while already connected");
73 ctx.getChannel().close();
74 return;
75 }
76
Jonathan Hart6b045582016-02-03 10:00:08 -080077 channel = ctx.getChannel();
Jonathan Hart3930f632015-10-19 12:12:51 -070078 }
79
80 @Override
81 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
82 throws Exception {
Jonathan Hart3930f632015-10-19 12:12:51 -070083 }
84
85 @Override
86 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e)
87 throws Exception {
88 handleDisconnect();
89 }
90
91 @Override
92 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
93 throws Exception {
Jonathan Hart3930f632015-10-19 12:12:51 -070094 }
95
96 private void handleDisconnect() {
Jonathan Hart6b045582016-02-03 10:00:08 -080097 fpmListener.peerDisconnected(channel.getRemoteAddress());
98 channel = null;
Jonathan Hart3930f632015-10-19 12:12:51 -070099 }
100}