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