Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
| 18 | import io.netty.channel.ChannelHandlerContext; |
| 19 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 20 | import io.netty.handler.timeout.IdleState; |
| 21 | import io.netty.handler.timeout.IdleStateEvent; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 22 | import io.netty.util.ReferenceCountUtil; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl; |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispInfoReply; |
| 25 | import org.onosproject.lisp.msg.protocols.LispInfoRequest; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
| 27 | import org.onosproject.lisp.msg.protocols.LispMapRegister; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 29 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | /** |
| 34 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 35 | * to the appropriate locations. |
| 36 | */ |
| 37 | public 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 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 44 | 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 Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 54 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 55 | ctx.writeAndFlush(lispMessage); |
| 56 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 57 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 58 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 59 | if (msg instanceof LispMapRegister) { |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 60 | LispMapServer mapServer = LispMapServer.getInstance(); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 61 | LispMapNotify mapNotify = |
| 62 | mapServer.processMapRegister((LispMapRegister) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 63 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 64 | if (mapNotify != null) { |
| 65 | ctx.writeAndFlush(mapNotify); |
| 66 | } |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 67 | } |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 68 | |
| 69 | if (msg instanceof LispInfoRequest) { |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 70 | LispMapServer mapServer = LispMapServer.getInstance(); |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 71 | LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg); |
| 72 | |
| 73 | ctx.writeAndFlush(infoReply); |
| 74 | } |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 75 | } finally { |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 76 | // try to remove the received message form the buffer |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 77 | ReferenceCountUtil.release(msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 78 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) |
| 83 | throws Exception { |
| 84 | |
| 85 | if (evt instanceof IdleStateEvent) { |
| 86 | IdleStateEvent event = (IdleStateEvent) evt; |
| 87 | if (event.state() == IdleState.READER_IDLE) { |
| 88 | log.info("READER_IDLE read timeout"); |
| 89 | ctx.disconnect(); |
| 90 | } else if (event.state() == IdleState.WRITER_IDLE) { |
| 91 | log.info("WRITER_IDLE write timeout"); |
| 92 | ctx.disconnect(); |
| 93 | } else if (event.state() == IdleState.ALL_IDLE) { |
| 94 | log.info("ALL_IDLE total timeout"); |
| 95 | ctx.disconnect(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
| 102 | throws Exception { |
| 103 | log.warn(cause.getMessage()); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 104 | |
| 105 | //TODO: add error handle mechanisms for each cases |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 106 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Extracts LISP message from encapsulated control message. |
| 110 | * |
| 111 | * @param ecm Encapsulated Control Message |
| 112 | * @return extracted LISP message |
| 113 | */ |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 114 | private LispMessage extractLispMessage(LispEncapsulatedControl ecm) { |
| 115 | LispMessage message = ecm.getControlMessage(); |
| 116 | message.configSender(ecm.getSender()); |
| 117 | return message; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 118 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 119 | } |