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 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 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 | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
| 25 | import org.onosproject.lisp.msg.protocols.LispMapRegister; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispInfoReply; |
| 27 | import org.onosproject.lisp.msg.protocols.LispInfoRequest; |
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 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 33 | import java.util.List; |
| 34 | |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 35 | /** |
| 36 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 37 | * to the appropriate locations. |
| 38 | */ |
| 39 | public class LispChannelHandler extends ChannelInboundHandlerAdapter { |
| 40 | |
| 41 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 42 | |
| 43 | @Override |
| 44 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 45 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 46 | try { |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 47 | |
| 48 | // process map-request message that is encapsulated in ECM |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 49 | if (msg instanceof LispEncapsulatedControl) { |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 50 | LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 51 | if (innerMsg instanceof LispMapRequest) { |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 52 | LispMapResolver mapResolver = LispMapResolver.getInstance(); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 53 | List<LispMessage> lispMessages = |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 54 | mapResolver.processMapRequest((LispEncapsulatedControl) msg); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 55 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 56 | if (lispMessages != null) { |
| 57 | lispMessages.forEach(ctx::writeAndFlush); |
| 58 | } |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 59 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 60 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 61 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 62 | // process map-register message |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 63 | if (msg instanceof LispMapRegister) { |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 64 | LispMapServer mapServer = LispMapServer.getInstance(); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 65 | LispMapNotify mapNotify = |
| 66 | mapServer.processMapRegister((LispMapRegister) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 67 | |
Jian Li | 2c8a2a4 | 2016-11-24 02:51:03 +0900 | [diff] [blame] | 68 | if (mapNotify != null) { |
| 69 | ctx.writeAndFlush(mapNotify); |
| 70 | } |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 71 | } |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 72 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 73 | // process info-request message |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 74 | if (msg instanceof LispInfoRequest) { |
Jian Li | 712ec05 | 2016-11-22 03:23:54 +0900 | [diff] [blame] | 75 | LispMapServer mapServer = LispMapServer.getInstance(); |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 76 | LispInfoReply infoReply = mapServer.processInfoRequest((LispInfoRequest) msg); |
| 77 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 78 | if (infoReply != null) { |
| 79 | ctx.writeAndFlush(infoReply); |
| 80 | } |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 81 | } |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 82 | } finally { |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 83 | // try to remove the received message form the buffer |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 84 | ReferenceCountUtil.release(msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 85 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) |
| 90 | throws Exception { |
| 91 | |
| 92 | if (evt instanceof IdleStateEvent) { |
| 93 | IdleStateEvent event = (IdleStateEvent) evt; |
| 94 | if (event.state() == IdleState.READER_IDLE) { |
| 95 | log.info("READER_IDLE read timeout"); |
| 96 | ctx.disconnect(); |
| 97 | } else if (event.state() == IdleState.WRITER_IDLE) { |
| 98 | log.info("WRITER_IDLE write timeout"); |
| 99 | ctx.disconnect(); |
| 100 | } else if (event.state() == IdleState.ALL_IDLE) { |
| 101 | log.info("ALL_IDLE total timeout"); |
| 102 | ctx.disconnect(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
| 109 | throws Exception { |
| 110 | log.warn(cause.getMessage()); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 111 | |
| 112 | //TODO: add error handle mechanisms for each cases |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 113 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Extracts LISP message from encapsulated control message. |
| 117 | * |
| 118 | * @param ecm Encapsulated Control Message |
| 119 | * @return extracted LISP message |
| 120 | */ |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 121 | private LispMessage extractLispMessage(LispEncapsulatedControl ecm) { |
| 122 | LispMessage message = ecm.getControlMessage(); |
| 123 | message.configSender(ecm.getSender()); |
| 124 | return message; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 125 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 126 | } |