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; |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 37 | import java.util.concurrent.ExecutorService; |
| 38 | import java.util.concurrent.Executors; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 39 | |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 40 | import static org.onlab.packet.IpAddress.valueOf; |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 41 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 42 | |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 43 | /** |
| 44 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 45 | * to the appropriate locations. |
| 46 | */ |
| 47 | public class LispChannelHandler extends ChannelInboundHandlerAdapter { |
| 48 | |
| 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 51 | private final LispRouterFactory routerFactory = LispRouterFactory.getInstance(); |
| 52 | |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 53 | protected ExecutorService executorMessages = |
| 54 | Executors.newFixedThreadPool(32, groupedThreads("onos/lisp", "message-stats-%d", log)); |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 55 | |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 56 | @Override |
| 57 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 58 | |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 59 | executorMessages.execute(new LispMessageHandler(ctx, (LispMessage) msg)); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) |
| 64 | throws Exception { |
| 65 | |
| 66 | if (evt instanceof IdleStateEvent) { |
| 67 | IdleStateEvent event = (IdleStateEvent) evt; |
| 68 | if (event.state() == IdleState.READER_IDLE) { |
| 69 | log.info("READER_IDLE read timeout"); |
| 70 | ctx.disconnect(); |
| 71 | } else if (event.state() == IdleState.WRITER_IDLE) { |
| 72 | log.info("WRITER_IDLE write timeout"); |
| 73 | ctx.disconnect(); |
| 74 | } else if (event.state() == IdleState.ALL_IDLE) { |
| 75 | log.info("ALL_IDLE total timeout"); |
| 76 | ctx.disconnect(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
| 83 | throws Exception { |
| 84 | log.warn(cause.getMessage()); |
yoonseon | db26867 | 2016-11-11 14:05:49 -0800 | [diff] [blame] | 85 | |
| 86 | //TODO: add error handle mechanisms for each cases |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 87 | } |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Extracts LISP message from encapsulated control message. |
| 91 | * |
| 92 | * @param ecm Encapsulated Control Message |
| 93 | * @return extracted LISP message |
| 94 | */ |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 95 | private LispMessage extractLispMessage(LispEncapsulatedControl ecm) { |
| 96 | LispMessage message = ecm.getControlMessage(); |
| 97 | message.configSender(ecm.getSender()); |
| 98 | return message; |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 99 | } |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * LISP message handler. |
| 103 | */ |
| 104 | private final class LispMessageHandler implements Runnable { |
| 105 | |
| 106 | protected final ChannelHandlerContext ctx; |
| 107 | protected final LispMessage msg; |
| 108 | private LispRouter router; |
| 109 | |
| 110 | public LispMessageHandler(ChannelHandlerContext ctx, LispMessage msg) { |
| 111 | this.ctx = ctx; |
| 112 | this.msg = msg; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void run() { |
| 117 | |
| 118 | try { |
| 119 | // process map-request message that is encapsulated in ECM |
| 120 | if (msg instanceof LispEncapsulatedControl) { |
| 121 | LispMessage innerMsg = extractLispMessage((LispEncapsulatedControl) msg); |
| 122 | if (innerMsg instanceof LispMapRequest) { |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 123 | |
| 124 | IpAddress xtrAddress = valueOf(innerMsg.getSender().getAddress()); |
| 125 | |
| 126 | router = routerFactory.getRouterInstance(xtrAddress); |
| 127 | |
| 128 | if (!router.isConnected()) { |
| 129 | router.setChannel(ctx.channel()); |
| 130 | router.connectRouter(); |
| 131 | } |
| 132 | |
| 133 | router.setEidRecords(((LispMapRequest) innerMsg).getEids()); |
| 134 | router.setSubscribed(true); |
| 135 | router.handleMessage(innerMsg); |
| 136 | |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 137 | LispMapResolver mapResolver = LispMapResolver.getInstance(); |
| 138 | List<LispMessage> lispMessages = |
| 139 | mapResolver.processMapRequest(msg); |
| 140 | |
| 141 | if (lispMessages != null) { |
| 142 | lispMessages.forEach(ctx::writeAndFlush); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // process map-register message |
| 148 | if (msg instanceof LispMapRegister) { |
| 149 | |
| 150 | LispMapRegister register = (LispMapRegister) msg; |
| 151 | IpAddress xtrAddress = valueOf(register.getSender().getAddress()); |
| 152 | router = routerFactory.getRouterInstance(xtrAddress); |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 153 | |
| 154 | if (!router.isConnected()) { |
| 155 | router.setChannel(ctx.channel()); |
| 156 | router.connectRouter(); |
| 157 | } |
| 158 | |
Jian Li | f987cb1 | 2017-04-28 00:41:46 +0900 | [diff] [blame] | 159 | router.handleMessage(register); |
| 160 | |
| 161 | LispMapServer mapServer = LispMapServer.getInstance(); |
| 162 | LispMapNotify mapNotify = mapServer.processMapRegister(register); |
| 163 | |
| 164 | if (mapNotify != null) { |
| 165 | ctx.writeAndFlush(mapNotify); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // process info-request message |
| 170 | if (msg instanceof LispInfoRequest) { |
| 171 | LispMapServer mapServer = LispMapServer.getInstance(); |
| 172 | LispInfoReply infoReply = mapServer.processInfoRequest(msg); |
| 173 | |
| 174 | if (infoReply != null) { |
| 175 | ctx.writeAndFlush(infoReply); |
| 176 | } |
| 177 | } |
| 178 | } finally { |
| 179 | // try to remove the received message form the buffer |
| 180 | ReferenceCountUtil.release(msg); |
| 181 | } |
| 182 | } |
| 183 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 184 | } |