blob: 2a7b74063f18937cc830f0639ccd3c0e6d9b30d1 [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) {
47 LispMessage innerMsg =
48 extractLispMessage((LispEncapsulatedControl) msg);
49 if (innerMsg instanceof LispMapRequest) {
50 LispMapResolver mapResolver = new LispMapResolver();
51 LispMessage lispMessage =
52 mapResolver.processMapRequest(
53 (LispEncapsulatedControl) msg);
Jian Li1118c122016-11-01 21:58:15 +090054
yoonseondb268672016-11-11 14:05:49 -080055 ctx.writeAndFlush(lispMessage);
56 }
Jian Li1118c122016-11-01 21:58:15 +090057 }
Jian Lif11594a2016-10-31 18:16:02 +090058
yoonseondb268672016-11-11 14:05:49 -080059 if (msg instanceof LispMapRegister) {
Jian Li712ec052016-11-22 03:23:54 +090060 LispMapServer mapServer = LispMapServer.getInstance();
yoonseondb268672016-11-11 14:05:49 -080061 LispMapNotify mapNotify =
62 mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090063
yoonseondb268672016-11-11 14:05:49 -080064 ctx.writeAndFlush(mapNotify);
65 }
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}