blob: 1b14a2fce5cfb0648a993078221506b21e4a4483 [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 Li451cea32016-10-04 15:27:50 +090024import org.onosproject.lisp.msg.protocols.LispMapNotify;
25import org.onosproject.lisp.msg.protocols.LispMapRegister;
Jian Li451cea32016-10-04 15:27:50 +090026import org.onosproject.lisp.msg.protocols.LispMapRequest;
Jian Lif11594a2016-10-31 18:16:02 +090027import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li451cea32016-10-04 15:27:50 +090028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * Channel handler deals with the xTR connection and dispatches xTR messages
33 * to the appropriate locations.
34 */
35public class LispChannelHandler extends ChannelInboundHandlerAdapter {
36
37 private final Logger log = LoggerFactory.getLogger(getClass());
38
39 @Override
40 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
41
yoonseondb268672016-11-11 14:05:49 -080042 try {
43 // first we need to check whether this is an ECM
44 if (msg instanceof LispEncapsulatedControl) {
45 LispMessage innerMsg =
46 extractLispMessage((LispEncapsulatedControl) msg);
47 if (innerMsg instanceof LispMapRequest) {
48 LispMapResolver mapResolver = new LispMapResolver();
49 LispMessage lispMessage =
50 mapResolver.processMapRequest(
51 (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) {
58 LispMapServer mapServer = new LispMapServer();
59 LispMapNotify mapNotify =
60 mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090061
yoonseondb268672016-11-11 14:05:49 -080062 ctx.writeAndFlush(mapNotify);
63 }
64 } finally {
Jian Liafe2d3f2016-11-01 02:49:07 +090065 ReferenceCountUtil.release(msg);
Jian Li451cea32016-10-04 15:27:50 +090066 }
Jian Li451cea32016-10-04 15:27:50 +090067 }
68
69 @Override
70 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
71 throws Exception {
72
73 if (evt instanceof IdleStateEvent) {
74 IdleStateEvent event = (IdleStateEvent) evt;
75 if (event.state() == IdleState.READER_IDLE) {
76 log.info("READER_IDLE read timeout");
77 ctx.disconnect();
78 } else if (event.state() == IdleState.WRITER_IDLE) {
79 log.info("WRITER_IDLE write timeout");
80 ctx.disconnect();
81 } else if (event.state() == IdleState.ALL_IDLE) {
82 log.info("ALL_IDLE total timeout");
83 ctx.disconnect();
84 }
85 }
86 }
87
88 @Override
89 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
90 throws Exception {
91 log.warn(cause.getMessage());
yoonseondb268672016-11-11 14:05:49 -080092
93 //TODO: add error handle mechanisms for each cases
Jian Li451cea32016-10-04 15:27:50 +090094 }
Jian Lif11594a2016-10-31 18:16:02 +090095
96 /**
97 * Extracts LISP message from encapsulated control message.
98 *
99 * @param ecm Encapsulated Control Message
100 * @return extracted LISP message
101 */
Jian Li1118c122016-11-01 21:58:15 +0900102 private LispMessage extractLispMessage(LispEncapsulatedControl ecm) {
103 LispMessage message = ecm.getControlMessage();
104 message.configSender(ecm.getSender());
105 return message;
Jian Lif11594a2016-10-31 18:16:02 +0900106 }
Jian Li451cea32016-10-04 15:27:50 +0900107}