blob: 1a759140789c244066bbcae3ba06ba00a5631371 [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;
Jonathan Hart72bbf882017-04-14 08:42:51 -070024import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
25import org.jboss.netty.handler.timeout.IdleStateEvent;
26import org.jboss.netty.handler.timeout.ReadTimeoutException;
Jonathan Hart3930f632015-10-19 12:12:51 -070027import org.onosproject.routing.fpm.protocol.FpmHeader;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Jonathan Hartb10f1e72017-05-02 16:36:26 -070031import java.net.InetSocketAddress;
32import java.net.SocketAddress;
33
Jonathan Hart3930f632015-10-19 12:12:51 -070034import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Session handler for FPM protocol.
38 */
Jonathan Hart72bbf882017-04-14 08:42:51 -070039public class FpmSessionHandler extends IdleStateAwareChannelHandler {
Jonathan Hart3930f632015-10-19 12:12:51 -070040
41 private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class);
42
Jonathan Hart6b045582016-02-03 10:00:08 -080043 private final FpmListener fpmListener;
Jonathan Hart3930f632015-10-19 12:12:51 -070044
45 private Channel channel;
Jonathan Hartb10f1e72017-05-02 16:36:26 -070046 private FpmPeer us;
Jonathan Hart3930f632015-10-19 12:12:51 -070047
Jonathan Hart72bbf882017-04-14 08:42:51 -070048 private boolean useKeepalives;
49 private boolean initialized;
50
Jonathan Hart3930f632015-10-19 12:12:51 -070051 /**
52 * Class constructor.
53 *
54 * @param fpmListener listener for FPM messages
55 */
Jonathan Hart6b045582016-02-03 10:00:08 -080056 public FpmSessionHandler(FpmListener fpmListener) {
Jonathan Hart3930f632015-10-19 12:12:51 -070057 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 Hart72bbf882017-04-14 08:42:51 -070064
65 initConnection(ctx, fpmMessage);
66
Jonathan Hartb10f1e72017-05-02 16:36:26 -070067 fpmListener.fpmMessage(us, fpmMessage);
Jonathan Hart3930f632015-10-19 12:12:51 -070068 }
69
Jonathan Hart72bbf882017-04-14 08:42:51 -070070 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 Hart3930f632015-10-19 12:12:51 -070085 @Override
86 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
87 throws Exception {
Jonathan Hart72bbf882017-04-14 08:42:51 -070088 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 Hart6b045582016-02-03 10:00:08 -080093 if (channel != null) {
94 channel.close();
95 }
Jonathan Hart3930f632015-10-19 12:12:51 -070096 handleDisconnect();
97 }
98
99 @Override
100 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
101 throws Exception {
Jonathan Hartb10f1e72017-05-02 16:36:26 -0700102 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 Hart3930f632015-10-19 12:12:51 -0700111 log.error("Received new FPM connection while already connected");
112 ctx.getChannel().close();
113 return;
114 }
115
Jonathan Hart6b045582016-02-03 10:00:08 -0800116 channel = ctx.getChannel();
Jonathan Hart3930f632015-10-19 12:12:51 -0700117 }
118
119 @Override
120 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
121 throws Exception {
Jonathan Hart3930f632015-10-19 12:12:51 -0700122 }
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 Hart3930f632015-10-19 12:12:51 -0700133 }
134
135 private void handleDisconnect() {
Jonathan Hartb10f1e72017-05-02 16:36:26 -0700136 if (us != null) {
137 fpmListener.peerDisconnected(us);
138 }
Jonathan Hart6b045582016-02-03 10:00:08 -0800139 channel = null;
Jonathan Hart3930f632015-10-19 12:12:51 -0700140 }
Jonathan Hart72bbf882017-04-14 08:42:51 -0700141
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 Hart3930f632015-10-19 12:12:51 -0700151}