blob: 16e86626cc130a55f7cccc7db0efa0697474fe73 [file] [log] [blame]
Jian Li451cea32016-10-04 15:27:50 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.lisp.ctl;
17
18import io.netty.channel.ChannelHandlerContext;
19import io.netty.channel.ChannelInboundHandlerAdapter;
20import io.netty.handler.timeout.IdleState;
21import io.netty.handler.timeout.IdleStateEvent;
Jian Liafe2d3f2016-11-01 02:49:07 +090022import io.netty.util.ReferenceCountUtil;
Jian Lif11594a2016-10-31 18:16:02 +090023import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl;
Jian Li6ef1b3f2016-11-12 18:16:06 +090024import org.onosproject.lisp.msg.protocols.LispInfoReply;
25import org.onosproject.lisp.msg.protocols.LispInfoRequest;
Jian Li451cea32016-10-04 15:27:50 +090026import org.onosproject.lisp.msg.protocols.LispMapNotify;
27import org.onosproject.lisp.msg.protocols.LispMapRegister;
Jian Li451cea32016-10-04 15:27:50 +090028import org.onosproject.lisp.msg.protocols.LispMapRequest;
Jian Lif11594a2016-10-31 18:16:02 +090029import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li451cea32016-10-04 15:27:50 +090030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33/**
34 * Channel handler deals with the xTR connection and dispatches xTR messages
35 * to the appropriate locations.
36 */
37public class LispChannelHandler extends ChannelInboundHandlerAdapter {
38
39 private final Logger log = LoggerFactory.getLogger(getClass());
40
41 @Override
42 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
43
yoonseondb268672016-11-11 14:05:49 -080044 try {
45 // first we need to check whether this is an ECM
46 if (msg instanceof LispEncapsulatedControl) {
Jian Li29986d82016-12-01 03:25:12 +090047 LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg);
yoonseondb268672016-11-11 14:05:49 -080048 if (innerMsg instanceof LispMapRequest) {
Jian Li29986d82016-12-01 03:25:12 +090049 LispMapResolver mapResolver = LispMapResolver.getInstance();
yoonseondb268672016-11-11 14:05:49 -080050 LispMessage lispMessage =
Jian Li29986d82016-12-01 03:25:12 +090051 mapResolver.processMapRequest((LispEncapsulatedControl) msg);
Jian Li1118c122016-11-01 21:58:15 +090052
yoonseondb268672016-11-11 14:05:49 -080053 ctx.writeAndFlush(lispMessage);
54 }
Jian Li1118c122016-11-01 21:58:15 +090055 }
Jian Lif11594a2016-10-31 18:16:02 +090056
yoonseondb268672016-11-11 14:05:49 -080057 if (msg instanceof LispMapRegister) {
Jian Li712ec052016-11-22 03:23:54 +090058 LispMapServer mapServer = LispMapServer.getInstance();
yoonseondb268672016-11-11 14:05:49 -080059 LispMapNotify mapNotify =
60 mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090061
Jian Li2c8a2a42016-11-24 02:51:03 +090062 if (mapNotify != null) {
63 ctx.writeAndFlush(mapNotify);
64 }
yoonseondb268672016-11-11 14:05:49 -080065 }
yoonseon980cd7c2016-11-18 14:18:46 -080066
67 if (msg instanceof LispInfoRequest) {
Jian Li712ec052016-11-22 03:23:54 +090068 LispMapServer mapServer = LispMapServer.getInstance();
yoonseon980cd7c2016-11-18 14:18:46 -080069 LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg);
70
71 ctx.writeAndFlush(infoReply);
72 }
yoonseondb268672016-11-11 14:05:49 -080073 } finally {
yoonseon980cd7c2016-11-18 14:18:46 -080074 // try to remove the received message form the buffer
Jian Liafe2d3f2016-11-01 02:49:07 +090075 ReferenceCountUtil.release(msg);
Jian Li451cea32016-10-04 15:27:50 +090076 }
Jian Li451cea32016-10-04 15:27:50 +090077 }
78
79 @Override
80 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
81 throws Exception {
82
83 if (evt instanceof IdleStateEvent) {
84 IdleStateEvent event = (IdleStateEvent) evt;
85 if (event.state() == IdleState.READER_IDLE) {
86 log.info("READER_IDLE read timeout");
87 ctx.disconnect();
88 } else if (event.state() == IdleState.WRITER_IDLE) {
89 log.info("WRITER_IDLE write timeout");
90 ctx.disconnect();
91 } else if (event.state() == IdleState.ALL_IDLE) {
92 log.info("ALL_IDLE total timeout");
93 ctx.disconnect();
94 }
95 }
96 }
97
98 @Override
99 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
100 throws Exception {
101 log.warn(cause.getMessage());
yoonseondb268672016-11-11 14:05:49 -0800102
103 //TODO: add error handle mechanisms for each cases
Jian Li451cea32016-10-04 15:27:50 +0900104 }
Jian Lif11594a2016-10-31 18:16:02 +0900105
106 /**
107 * Extracts LISP message from encapsulated control message.
108 *
109 * @param ecm Encapsulated Control Message
110 * @return extracted LISP message
111 */
Jian Li1118c122016-11-01 21:58:15 +0900112 private LispMessage extractLispMessage(LispEncapsulatedControl ecm) {
113 LispMessage message = ecm.getControlMessage();
114 message.configSender(ecm.getSender());
115 return message;
Jian Lif11594a2016-10-31 18:16:02 +0900116 }
Jian Li451cea32016-10-04 15:27:50 +0900117}