blob: b5e0105f3a7400c174f53fc9eb37f7d4692e0255 [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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Li451cea32016-10-04 15:27:50 +090017
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 Li451cea32016-10-04 15:27:50 +090024import org.onosproject.lisp.msg.protocols.LispMapNotify;
25import org.onosproject.lisp.msg.protocols.LispMapRegister;
Jian Li5e505c62016-12-05 02:44:24 +090026import org.onosproject.lisp.msg.protocols.LispInfoReply;
27import org.onosproject.lisp.msg.protocols.LispInfoRequest;
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
Jian Licdbc0872016-12-05 17:23:53 +090033import java.util.List;
34
Jian Li451cea32016-10-04 15:27:50 +090035/**
36 * Channel handler deals with the xTR connection and dispatches xTR messages
37 * to the appropriate locations.
38 */
39public class LispChannelHandler extends ChannelInboundHandlerAdapter {
40
41 private final Logger log = LoggerFactory.getLogger(getClass());
42
43 @Override
44 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
45
yoonseondb268672016-11-11 14:05:49 -080046 try {
Jian Licdbc0872016-12-05 17:23:53 +090047
48 // process map-request message that is encapsulated in ECM
yoonseondb268672016-11-11 14:05:49 -080049 if (msg instanceof LispEncapsulatedControl) {
Jian Li29986d82016-12-01 03:25:12 +090050 LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg);
yoonseondb268672016-11-11 14:05:49 -080051 if (innerMsg instanceof LispMapRequest) {
Jian Li29986d82016-12-01 03:25:12 +090052 LispMapResolver mapResolver = LispMapResolver.getInstance();
Jian Licdbc0872016-12-05 17:23:53 +090053 List<LispMessage> lispMessages =
Jian Li29986d82016-12-01 03:25:12 +090054 mapResolver.processMapRequest((LispEncapsulatedControl) msg);
Jian Li1118c122016-11-01 21:58:15 +090055
Jian Licdbc0872016-12-05 17:23:53 +090056 if (lispMessages != null) {
57 lispMessages.forEach(ctx::writeAndFlush);
58 }
yoonseondb268672016-11-11 14:05:49 -080059 }
Jian Li1118c122016-11-01 21:58:15 +090060 }
Jian Lif11594a2016-10-31 18:16:02 +090061
Jian Licdbc0872016-12-05 17:23:53 +090062 // process map-register message
yoonseondb268672016-11-11 14:05:49 -080063 if (msg instanceof LispMapRegister) {
Jian Li712ec052016-11-22 03:23:54 +090064 LispMapServer mapServer = LispMapServer.getInstance();
yoonseondb268672016-11-11 14:05:49 -080065 LispMapNotify mapNotify =
66 mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090067
Jian Li2c8a2a42016-11-24 02:51:03 +090068 if (mapNotify != null) {
69 ctx.writeAndFlush(mapNotify);
70 }
yoonseondb268672016-11-11 14:05:49 -080071 }
yoonseon980cd7c2016-11-18 14:18:46 -080072
Jian Licdbc0872016-12-05 17:23:53 +090073 // process info-request message
yoonseon980cd7c2016-11-18 14:18:46 -080074 if (msg instanceof LispInfoRequest) {
Jian Li712ec052016-11-22 03:23:54 +090075 LispMapServer mapServer = LispMapServer.getInstance();
yoonseon980cd7c2016-11-18 14:18:46 -080076 LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg);
77
Jian Licdbc0872016-12-05 17:23:53 +090078 if (infoReply != null) {
79 ctx.writeAndFlush(infoReply);
80 }
yoonseon980cd7c2016-11-18 14:18:46 -080081 }
yoonseondb268672016-11-11 14:05:49 -080082 } finally {
yoonseon980cd7c2016-11-18 14:18:46 -080083 // try to remove the received message form the buffer
Jian Liafe2d3f2016-11-01 02:49:07 +090084 ReferenceCountUtil.release(msg);
Jian Li451cea32016-10-04 15:27:50 +090085 }
Jian Li451cea32016-10-04 15:27:50 +090086 }
87
88 @Override
89 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
90 throws Exception {
91
92 if (evt instanceof IdleStateEvent) {
93 IdleStateEvent event = (IdleStateEvent) evt;
94 if (event.state() == IdleState.READER_IDLE) {
95 log.info("READER_IDLE read timeout");
96 ctx.disconnect();
97 } else if (event.state() == IdleState.WRITER_IDLE) {
98 log.info("WRITER_IDLE write timeout");
99 ctx.disconnect();
100 } else if (event.state() == IdleState.ALL_IDLE) {
101 log.info("ALL_IDLE total timeout");
102 ctx.disconnect();
103 }
104 }
105 }
106
107 @Override
108 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
109 throws Exception {
110 log.warn(cause.getMessage());
yoonseondb268672016-11-11 14:05:49 -0800111
112 //TODO: add error handle mechanisms for each cases
Jian Li451cea32016-10-04 15:27:50 +0900113 }
Jian Lif11594a2016-10-31 18:16:02 +0900114
115 /**
116 * Extracts LISP message from encapsulated control message.
117 *
118 * @param ecm Encapsulated Control Message
119 * @return extracted LISP message
120 */
Jian Li1118c122016-11-01 21:58:15 +0900121 private LispMessage extractLispMessage(LispEncapsulatedControl ecm) {
122 LispMessage message = ecm.getControlMessage();
123 message.configSender(ecm.getSender());
124 return message;
Jian Lif11594a2016-10-31 18:16:02 +0900125 }
Jian Li451cea32016-10-04 15:27:50 +0900126}