blob: 8efbff96718f04a54ad42ece935ddfba0bbd63e9 [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 Lib1a8fd02016-12-27 03:55:32 +090023import org.onlab.packet.IpAddress;
24import org.onosproject.lisp.ctl.LispRouter;
25import org.onosproject.lisp.ctl.LispRouterFactory;
Jian Lif11594a2016-10-31 18:16:02 +090026import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl;
Jian Li5e505c62016-12-05 02:44:24 +090027import org.onosproject.lisp.msg.protocols.LispInfoReply;
28import org.onosproject.lisp.msg.protocols.LispInfoRequest;
Jian Lib1a8fd02016-12-27 03:55:32 +090029import org.onosproject.lisp.msg.protocols.LispMapNotify;
30import org.onosproject.lisp.msg.protocols.LispMapRegister;
Jian Li451cea32016-10-04 15:27:50 +090031import org.onosproject.lisp.msg.protocols.LispMapRequest;
Jian Lif11594a2016-10-31 18:16:02 +090032import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li451cea32016-10-04 15:27:50 +090033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Jian Licdbc0872016-12-05 17:23:53 +090036import java.util.List;
37
Jian Lib1a8fd02016-12-27 03:55:32 +090038import static org.onlab.packet.IpAddress.valueOf;
39
Jian Li451cea32016-10-04 15:27:50 +090040/**
41 * Channel handler deals with the xTR connection and dispatches xTR messages
42 * to the appropriate locations.
43 */
44public class LispChannelHandler extends ChannelInboundHandlerAdapter {
45
46 private final Logger log = LoggerFactory.getLogger(getClass());
47
Jian Lib1a8fd02016-12-27 03:55:32 +090048 private final LispRouterFactory routerFactory = LispRouterFactory.getInstance();
49
50 private LispRouter router;
51
Jian Li451cea32016-10-04 15:27:50 +090052 @Override
53 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
54
yoonseondb268672016-11-11 14:05:49 -080055 try {
Jian Licdbc0872016-12-05 17:23:53 +090056
57 // process map-request message that is encapsulated in ECM
yoonseondb268672016-11-11 14:05:49 -080058 if (msg instanceof LispEncapsulatedControl) {
Jian Li29986d82016-12-01 03:25:12 +090059 LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg);
yoonseondb268672016-11-11 14:05:49 -080060 if (innerMsg instanceof LispMapRequest) {
Jian Li29986d82016-12-01 03:25:12 +090061 LispMapResolver mapResolver = LispMapResolver.getInstance();
Jian Licdbc0872016-12-05 17:23:53 +090062 List<LispMessage> lispMessages =
Jian Li29986d82016-12-01 03:25:12 +090063 mapResolver.processMapRequest((LispEncapsulatedControl) msg);
Jian Li1118c122016-11-01 21:58:15 +090064
Jian Licdbc0872016-12-05 17:23:53 +090065 if (lispMessages != null) {
66 lispMessages.forEach(ctx::writeAndFlush);
67 }
yoonseondb268672016-11-11 14:05:49 -080068 }
Jian Li1118c122016-11-01 21:58:15 +090069 }
Jian Lif11594a2016-10-31 18:16:02 +090070
Jian Licdbc0872016-12-05 17:23:53 +090071 // process map-register message
yoonseondb268672016-11-11 14:05:49 -080072 if (msg instanceof LispMapRegister) {
Jian Lib1a8fd02016-12-27 03:55:32 +090073
74 LispMapRegister register = (LispMapRegister) msg;
75 IpAddress xtrAddress = valueOf(register.getSender().getAddress());
76 router = routerFactory.getRouterInstance(xtrAddress);
77 router.setChannel(ctx.channel());
78 router.connectRouter();
79 router.handleMessage(register);
80
Jian Li712ec052016-11-22 03:23:54 +090081 LispMapServer mapServer = LispMapServer.getInstance();
Jian Lib1a8fd02016-12-27 03:55:32 +090082 LispMapNotify mapNotify = mapServer.processMapRegister(register);
Jian Li451cea32016-10-04 15:27:50 +090083
Jian Li2c8a2a42016-11-24 02:51:03 +090084 if (mapNotify != null) {
85 ctx.writeAndFlush(mapNotify);
86 }
yoonseondb268672016-11-11 14:05:49 -080087 }
yoonseon980cd7c2016-11-18 14:18:46 -080088
Jian Licdbc0872016-12-05 17:23:53 +090089 // process info-request message
yoonseon980cd7c2016-11-18 14:18:46 -080090 if (msg instanceof LispInfoRequest) {
Jian Li712ec052016-11-22 03:23:54 +090091 LispMapServer mapServer = LispMapServer.getInstance();
yoonseon980cd7c2016-11-18 14:18:46 -080092 LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg);
93
Jian Licdbc0872016-12-05 17:23:53 +090094 if (infoReply != null) {
95 ctx.writeAndFlush(infoReply);
96 }
yoonseon980cd7c2016-11-18 14:18:46 -080097 }
yoonseondb268672016-11-11 14:05:49 -080098 } finally {
yoonseon980cd7c2016-11-18 14:18:46 -080099 // try to remove the received message form the buffer
Jian Liafe2d3f2016-11-01 02:49:07 +0900100 ReferenceCountUtil.release(msg);
Jian Li451cea32016-10-04 15:27:50 +0900101 }
Jian Li451cea32016-10-04 15:27:50 +0900102 }
103
104 @Override
105 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
106 throws Exception {
107
108 if (evt instanceof IdleStateEvent) {
109 IdleStateEvent event = (IdleStateEvent) evt;
110 if (event.state() == IdleState.READER_IDLE) {
111 log.info("READER_IDLE read timeout");
112 ctx.disconnect();
113 } else if (event.state() == IdleState.WRITER_IDLE) {
114 log.info("WRITER_IDLE write timeout");
115 ctx.disconnect();
116 } else if (event.state() == IdleState.ALL_IDLE) {
117 log.info("ALL_IDLE total timeout");
118 ctx.disconnect();
119 }
120 }
121 }
122
123 @Override
124 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
125 throws Exception {
126 log.warn(cause.getMessage());
yoonseondb268672016-11-11 14:05:49 -0800127
128 //TODO: add error handle mechanisms for each cases
Jian Li451cea32016-10-04 15:27:50 +0900129 }
Jian Lif11594a2016-10-31 18:16:02 +0900130
131 /**
132 * Extracts LISP message from encapsulated control message.
133 *
134 * @param ecm Encapsulated Control Message
135 * @return extracted LISP message
136 */
Jian Li1118c122016-11-01 21:58:15 +0900137 private LispMessage extractLispMessage(LispEncapsulatedControl ecm) {
138 LispMessage message = ecm.getControlMessage();
139 message.configSender(ecm.getSender());
140 return message;
Jian Lif11594a2016-10-31 18:16:02 +0900141 }
Jian Li451cea32016-10-04 15:27:50 +0900142}