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 | 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 | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | /** |
| 32 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 33 | * to the appropriate locations. |
| 34 | */ |
| 35 | public 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 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame^] | 42 | 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 Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 52 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame^] | 53 | ctx.writeAndFlush(lispMessage); |
| 54 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 55 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 56 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame^] | 57 | if (msg instanceof LispMapRegister) { |
| 58 | LispMapServer mapServer = new LispMapServer(); |
| 59 | LispMapNotify mapNotify = |
| 60 | mapServer.processMapRegister((LispMapRegister) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 61 | |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame^] | 62 | ctx.writeAndFlush(mapNotify); |
| 63 | } |
| 64 | } finally { |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 65 | ReferenceCountUtil.release(msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 66 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 67 | } |
| 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()); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame^] | 92 | |
| 93 | //TODO: add error handle mechanisms for each cases |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 94 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Extracts LISP message from encapsulated control message. |
| 98 | * |
| 99 | * @param ecm Encapsulated Control Message |
| 100 | * @return extracted LISP message |
| 101 | */ |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 102 | private LispMessage extractLispMessage(LispEncapsulatedControl ecm) { |
| 103 | LispMessage message = ecm.getControlMessage(); |
| 104 | message.configSender(ecm.getSender()); |
| 105 | return message; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 106 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 107 | } |