blob: d628838ba15a7c0cb01b2ce91ae14d55ef618d11 [file] [log] [blame]
Jian Li451cea32016-10-04 15:27:50 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
18import io.netty.channel.ChannelHandlerContext;
19import io.netty.channel.ChannelInboundHandlerAdapter;
20import io.netty.handler.timeout.IdleState;
21import io.netty.handler.timeout.IdleStateEvent;
Jian Lif11594a2016-10-31 18:16:02 +090022import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl;
Jian Li451cea32016-10-04 15:27:50 +090023import org.onosproject.lisp.msg.protocols.LispMapNotify;
24import org.onosproject.lisp.msg.protocols.LispMapRegister;
Jian Li451cea32016-10-04 15:27:50 +090025import org.onosproject.lisp.msg.protocols.LispMapRequest;
Jian Lif11594a2016-10-31 18:16:02 +090026import org.onosproject.lisp.msg.protocols.LispMapReply;
27import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li451cea32016-10-04 15:27:50 +090028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * Channel handler deals with the xTR connection and dispatches xTR messages
33 * to the appropriate locations.
34 */
35public 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
Jian Lif11594a2016-10-31 18:16:02 +090042 // first we need to check whether this is an ECM
43 if (msg instanceof LispEncapsulatedControl) {
44 msg = extractMapRequest((LispEncapsulatedControl) msg);
45 }
46
Jian Li451cea32016-10-04 15:27:50 +090047 if (msg instanceof LispMapRegister) {
48 LispMapServer mapServer = new LispMapServer();
Jian Li6322a362016-10-31 00:57:19 +090049 LispMapNotify mapNotify =
50 (LispMapNotify) mapServer.processMapRegister((LispMapRegister) msg);
Jian Li451cea32016-10-04 15:27:50 +090051
52 // TODO: deserialize mapNotify message and write to channel
53 }
54
55 if (msg instanceof LispMapRequest) {
56 LispMapResolver mapResolver = new LispMapResolver();
Jian Li6322a362016-10-31 00:57:19 +090057 LispMapReply mapReply =
58 (LispMapReply) mapResolver.processMapRequest((LispMapRequest) msg);
Jian Li451cea32016-10-04 15:27:50 +090059
60 // TODO: deserialize mapReply message and write to channel
61 }
62 }
63
64 @Override
65 public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
66 throws Exception {
67
68 if (evt instanceof IdleStateEvent) {
69 IdleStateEvent event = (IdleStateEvent) evt;
70 if (event.state() == IdleState.READER_IDLE) {
71 log.info("READER_IDLE read timeout");
72 ctx.disconnect();
73 } else if (event.state() == IdleState.WRITER_IDLE) {
74 log.info("WRITER_IDLE write timeout");
75 ctx.disconnect();
76 } else if (event.state() == IdleState.ALL_IDLE) {
77 log.info("ALL_IDLE total timeout");
78 ctx.disconnect();
79 }
80 }
81 }
82
83 @Override
84 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
85 throws Exception {
86 log.warn(cause.getMessage());
87 ctx.close();
88 }
Jian Lif11594a2016-10-31 18:16:02 +090089
90 /**
91 * Extracts LISP message from encapsulated control message.
92 *
93 * @param ecm Encapsulated Control Message
94 * @return extracted LISP message
95 */
96 private LispMessage extractMapRequest(LispEncapsulatedControl ecm) {
97 return ecm.getControlMessage();
98 }
Jian Li451cea32016-10-04 15:27:50 +090099}