slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 1 | /* |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 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 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 17 | package org.onosproject.xran.impl.controller; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 18 | |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
| 21 | import io.netty.channel.ChannelHandler.Sharable; |
| 22 | import io.netty.channel.ChannelHandlerContext; |
| 23 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 24 | import io.netty.channel.sctp.SctpMessage; |
| 25 | import org.apache.commons.lang.exception.ExceptionUtils; |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 26 | import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream; |
| 27 | import org.onosproject.xran.asn1lib.pdu.XrancPdu; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 31 | import java.io.ByteArrayInputStream; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.net.InetSocketAddress; |
| 35 | import java.net.SocketAddress; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 36 | import java.net.URISyntaxException; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 39 | * Xran channel handler. |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 40 | */ |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 41 | @Sharable |
| 42 | public class XranChannelHandler extends ChannelInboundHandlerAdapter { |
| 43 | |
| 44 | private static final Logger log = |
| 45 | LoggerFactory.getLogger(XranChannelHandler.class); |
| 46 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 47 | private final XranServer xranServer; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 48 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 49 | XranChannelHandler(XranServer xranServer) { |
| 50 | this.xranServer = xranServer; |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Given PDU construct an SCTP message. |
| 55 | * |
| 56 | * @param pdu PDU packet |
| 57 | * @return SCTP message |
| 58 | * @throws IOException IO exception |
| 59 | */ |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 60 | public static SctpMessage getSctpMessage(XrancPdu pdu) { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 61 | BerByteArrayOutputStream os = new BerByteArrayOutputStream(4096); |
| 62 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 63 | try { |
| 64 | pdu.encode(os); |
| 65 | } catch (IOException e) { |
| 66 | log.warn(ExceptionUtils.getFullStackTrace(e)); |
| 67 | } |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 68 | |
| 69 | log.debug("Sending message: {}", pdu); |
| 70 | final ByteBuf buf = Unpooled.buffer(os.getArray().length); |
| 71 | for (int i = 0; i < buf.capacity(); i++) { |
| 72 | buf.writeByte(os.getArray()[i]); |
| 73 | } |
| 74 | return new SctpMessage(0, 0, buf); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void channelActive(ChannelHandlerContext ctx) throws IOException, URISyntaxException { |
| 79 | final SocketAddress address = ctx.channel().remoteAddress(); |
| 80 | if (!(address instanceof InetSocketAddress)) { |
| 81 | log.warn("Invalid client connection. Xran Cell is indentifed based on IP"); |
| 82 | ctx.close(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 87 | final String host = inetAddress.getHostString(); |
| 88 | log.info("New client connected to the Server: {}", host); |
| 89 | |
| 90 | log.info("Adding device..."); |
| 91 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 92 | xranServer.deviceAgent.addConnectedCell(host, ctx); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
slowr | c86750e | 2017-08-22 17:26:47 -0700 | [diff] [blame] | 96 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException, InterruptedException { |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 97 | SctpMessage sctpMessage = (SctpMessage) msg; |
| 98 | ByteBuf byteBuf = sctpMessage.content(); |
| 99 | |
| 100 | byte[] bytes = new byte[byteBuf.readableBytes()]; |
| 101 | byteBuf.readBytes(bytes); |
| 102 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 103 | XrancPdu recvPdu = new XrancPdu(); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 104 | |
| 105 | InputStream inputStream = new ByteArrayInputStream(bytes); |
| 106 | |
slowr | 577f322 | 2017-08-28 10:49:08 -0700 | [diff] [blame] | 107 | recvPdu.decode(inputStream); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 108 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 109 | xranServer.packetAgent.handlePacket(recvPdu, ctx); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void channelInactive(ChannelHandlerContext ctx) { |
| 114 | log.info("Client dropped the connection"); |
| 115 | |
| 116 | final SocketAddress address = ctx.channel().remoteAddress(); |
| 117 | if (!(address instanceof InetSocketAddress)) { |
| 118 | log.warn("Invalid client connection. Xran Cell is indentifed based on IP"); |
| 119 | ctx.close(); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 124 | final String host = inetAddress.getHostString(); |
| 125 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 126 | xranServer.deviceAgent.removeConnectedCell(host); |
slowr | 13fa5b0 | 2017-08-08 16:32:31 -0700 | [diff] [blame] | 127 | |
| 128 | ctx.close(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 133 | // Close the connection when an exception is raised. |
| 134 | log.warn("exceptionCaught: {}", ExceptionUtils.getStackTrace(cause)); |
| 135 | cause.printStackTrace(); |
| 136 | ctx.close(); |
| 137 | } |
| 138 | } |
| 139 | |