blob: 6c723a6d7abf09b2988c4b3d3cb09ef1df9b3ad8 [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.LispMapReply;
28import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li451cea32016-10-04 15:27:50 +090029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Channel handler deals with the xTR connection and dispatches xTR messages
34 * to the appropriate locations.
35 */
36public class LispChannelHandler extends ChannelInboundHandlerAdapter {
37
38 private final Logger log = LoggerFactory.getLogger(getClass());
39
40 @Override
41 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
42
Jian Lif11594a2016-10-31 18:16:02 +090043 // first we need to check whether this is an ECM
44 if (msg instanceof LispEncapsulatedControl) {
45 msg = extractMapRequest((LispEncapsulatedControl) msg);
46 }
47
Jian Li451cea32016-10-04 15:27:50 +090048 if (msg instanceof LispMapRegister) {
49 LispMapServer mapServer = new LispMapServer();
Jian Liafe2d3f2016-11-01 02:49:07 +090050 LispMapNotify mapNotify = mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090051
Jian Liafe2d3f2016-11-01 02:49:07 +090052 // try to remove the received map-register message from buffer
53 ReferenceCountUtil.release(msg);
54
55 ctx.writeAndFlush(mapNotify);
Jian Li451cea32016-10-04 15:27:50 +090056 }
57
58 if (msg instanceof LispMapRequest) {
59 LispMapResolver mapResolver = new LispMapResolver();
Jian Li6322a362016-10-31 00:57:19 +090060 LispMapReply mapReply =
61 (LispMapReply) mapResolver.processMapRequest((LispMapRequest) msg);
Jian Li451cea32016-10-04 15:27:50 +090062
Jian Liafe2d3f2016-11-01 02:49:07 +090063 // TODO: serialize mapReply message and write to channel
Jian Li451cea32016-10-04 15:27:50 +090064 }
65 }
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 */
99 private LispMessage extractMapRequest(LispEncapsulatedControl ecm) {
100 return ecm.getControlMessage();
101 }
Jian Li451cea32016-10-04 15:27:50 +0900102}