blob: e0285c3bb5a31b7761e1e6f0462ffd4ea89c4441 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08002 * Copyright 2017-present Open Networking Foundation
slowr13fa5b02017-08-08 16:32:31 -07003 *
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 Mavrommatis96b255a2017-12-06 13:09:25 -080017package org.onosproject.xran.impl.controller;
slowr13fa5b02017-08-08 16:32:31 -070018
19import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
21import io.netty.channel.ChannelHandler.Sharable;
22import io.netty.channel.ChannelHandlerContext;
23import io.netty.channel.ChannelInboundHandlerAdapter;
24import io.netty.channel.sctp.SctpMessage;
25import org.apache.commons.lang.exception.ExceptionUtils;
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080026import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
27import org.onosproject.xran.asn1lib.pdu.XrancPdu;
slowr13fa5b02017-08-08 16:32:31 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
slowr13fa5b02017-08-08 16:32:31 -070031import java.io.ByteArrayInputStream;
32import java.io.IOException;
33import java.io.InputStream;
34import java.net.InetSocketAddress;
35import java.net.SocketAddress;
slowr13fa5b02017-08-08 16:32:31 -070036import java.net.URISyntaxException;
slowr13fa5b02017-08-08 16:32:31 -070037
38/**
slowr577f3222017-08-28 10:49:08 -070039 * Xran channel handler.
slowr13fa5b02017-08-08 16:32:31 -070040 */
slowr13fa5b02017-08-08 16:32:31 -070041@Sharable
42public class XranChannelHandler extends ChannelInboundHandlerAdapter {
43
44 private static final Logger log =
45 LoggerFactory.getLogger(XranChannelHandler.class);
46
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080047 private final XranServer xranServer;
slowr13fa5b02017-08-08 16:32:31 -070048
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080049 XranChannelHandler(XranServer xranServer) {
50 this.xranServer = xranServer;
slowr13fa5b02017-08-08 16:32:31 -070051 }
52
slowr577f3222017-08-28 10:49:08 -070053 /**
54 * Given PDU construct an SCTP message.
55 *
56 * @param pdu PDU packet
57 * @return SCTP message
58 * @throws IOException IO exception
59 */
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080060 public static SctpMessage getSctpMessage(XrancPdu pdu) {
slowr13fa5b02017-08-08 16:32:31 -070061 BerByteArrayOutputStream os = new BerByteArrayOutputStream(4096);
62
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080063 try {
64 pdu.encode(os);
65 } catch (IOException e) {
66 log.warn(ExceptionUtils.getFullStackTrace(e));
67 }
slowr13fa5b02017-08-08 16:32:31 -070068
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 Mavrommatis96b255a2017-12-06 13:09:25 -080092 xranServer.deviceAgent.addConnectedCell(host, ctx);
slowr13fa5b02017-08-08 16:32:31 -070093 }
94
95 @Override
slowrc86750e2017-08-22 17:26:47 -070096 public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException, InterruptedException {
slowr13fa5b02017-08-08 16:32:31 -070097 SctpMessage sctpMessage = (SctpMessage) msg;
98 ByteBuf byteBuf = sctpMessage.content();
99
100 byte[] bytes = new byte[byteBuf.readableBytes()];
101 byteBuf.readBytes(bytes);
102
slowr577f3222017-08-28 10:49:08 -0700103 XrancPdu recvPdu = new XrancPdu();
slowr13fa5b02017-08-08 16:32:31 -0700104
105 InputStream inputStream = new ByteArrayInputStream(bytes);
106
slowr577f3222017-08-28 10:49:08 -0700107 recvPdu.decode(inputStream);
slowr13fa5b02017-08-08 16:32:31 -0700108
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -0800109 xranServer.packetAgent.handlePacket(recvPdu, ctx);
slowr13fa5b02017-08-08 16:32:31 -0700110 }
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 Mavrommatis96b255a2017-12-06 13:09:25 -0800126 xranServer.deviceAgent.removeConnectedCell(host);
slowr13fa5b02017-08-08 16:32:31 -0700127
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