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.LispMapReply; |
| 28 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | /** |
| 33 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 34 | * to the appropriate locations. |
| 35 | */ |
| 36 | public 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 Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 43 | // first we need to check whether this is an ECM |
| 44 | if (msg instanceof LispEncapsulatedControl) { |
| 45 | msg = extractMapRequest((LispEncapsulatedControl) msg); |
| 46 | } |
| 47 | |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 48 | if (msg instanceof LispMapRegister) { |
| 49 | LispMapServer mapServer = new LispMapServer(); |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame^] | 50 | LispMapNotify mapNotify = mapServer.processMapRegister((LispMapRegister) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 51 | |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame^] | 52 | // try to remove the received map-register message from buffer |
| 53 | ReferenceCountUtil.release(msg); |
| 54 | |
| 55 | ctx.writeAndFlush(mapNotify); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (msg instanceof LispMapRequest) { |
| 59 | LispMapResolver mapResolver = new LispMapResolver(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 60 | LispMapReply mapReply = |
| 61 | (LispMapReply) mapResolver.processMapRequest((LispMapRequest) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 62 | |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame^] | 63 | // TODO: serialize mapReply message and write to channel |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 64 | } |
| 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 Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 92 | |
| 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 Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 102 | } |