blob: d1ad800d83ca74454647579eec7b158d532ea690 [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
Jian Lif11594a2016-10-31 18:16:02 +090042 // first we need to check whether this is an ECM
43 if (msg instanceof LispEncapsulatedControl) {
Jian Li1118c122016-11-01 21:58:15 +090044 LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg);
45 if (innerMsg instanceof LispMapRequest) {
46 LispMapResolver mapResolver = new LispMapResolver();
47 LispMessage lispMessage = mapResolver.processMapRequest((LispEncapsulatedControl) msg);
48
49 // try to remove the received map-request message from buffer
50 ReferenceCountUtil.release(msg);
51
52 ctx.writeAndFlush(lispMessage);
53 }
Jian Lif11594a2016-10-31 18:16:02 +090054 }
55
Jian Li451cea32016-10-04 15:27:50 +090056 if (msg instanceof LispMapRegister) {
57 LispMapServer mapServer = new LispMapServer();
Jian Liafe2d3f2016-11-01 02:49:07 +090058 LispMapNotify mapNotify = mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090059
Jian Liafe2d3f2016-11-01 02:49:07 +090060 // try to remove the received map-register message from buffer
61 ReferenceCountUtil.release(msg);
62
63 ctx.writeAndFlush(mapNotify);
Jian Li451cea32016-10-04 15:27:50 +090064 }
Jian Li451cea32016-10-04 15:27:50 +090065 }
66
67 @Override
68 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
69 throws Exception {
70
71 if (evt instanceof IdleStateEvent) {
72 IdleStateEvent event = (IdleStateEvent) evt;
73 if (event.state() == IdleState.READER_IDLE) {
74 log.info("READER_IDLE read timeout");
75 ctx.disconnect();
76 } else if (event.state() == IdleState.WRITER_IDLE) {
77 log.info("WRITER_IDLE write timeout");
78 ctx.disconnect();
79 } else if (event.state() == IdleState.ALL_IDLE) {
80 log.info("ALL_IDLE total timeout");
81 ctx.disconnect();
82 }
83 }
84 }
85
86 @Override
87 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
88 throws Exception {
89 log.warn(cause.getMessage());
90 ctx.close();
91 }
Jian Lif11594a2016-10-31 18:16:02 +090092
93 /**
94 * Extracts LISP message from encapsulated control message.
95 *
96 * @param ecm Encapsulated Control Message
97 * @return extracted LISP message
98 */
Jian Li1118c122016-11-01 21:58:15 +090099 private LispMessage extractLispMessage(LispEncapsulatedControl ecm) {
100 LispMessage message = ecm.getControlMessage();
101 message.configSender(ecm.getSender());
102 return message;
Jian Lif11594a2016-10-31 18:16:02 +0900103 }
Jian Li451cea32016-10-04 15:27:50 +0900104}