blob: d4296022c3f85bbb881162c48cefca00323ea57a [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
slowr577f3222017-08-28 10:49:08 -07002 * Copyright 2016-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
17package org.onosproject.xran.controller;
18
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;
slowr60d4d102017-08-16 18:33:58 -070026import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream;
slowrc86750e2017-08-22 17:26:47 -070027import org.onosproject.xran.codecs.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
47 private final Controller controller;
48
49 XranChannelHandler(Controller controller) {
50 this.controller = controller;
51 }
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 */
slowr13fa5b02017-08-08 16:32:31 -070060 public static SctpMessage getSctpMessage(XrancPdu pdu) throws IOException {
61 BerByteArrayOutputStream os = new BerByteArrayOutputStream(4096);
62
63 pdu.encode(os);
64
65 log.debug("Sending message: {}", pdu);
66 final ByteBuf buf = Unpooled.buffer(os.getArray().length);
67 for (int i = 0; i < buf.capacity(); i++) {
68 buf.writeByte(os.getArray()[i]);
69 }
70 return new SctpMessage(0, 0, buf);
71 }
72
73 @Override
74 public void channelActive(ChannelHandlerContext ctx) throws IOException, URISyntaxException {
75 final SocketAddress address = ctx.channel().remoteAddress();
76 if (!(address instanceof InetSocketAddress)) {
77 log.warn("Invalid client connection. Xran Cell is indentifed based on IP");
78 ctx.close();
79 return;
80 }
81
82 final InetSocketAddress inetAddress = (InetSocketAddress) address;
83 final String host = inetAddress.getHostString();
84 log.info("New client connected to the Server: {}", host);
85
86 log.info("Adding device...");
87
88 controller.deviceAgent.addConnectedCell(host, ctx);
89 }
90
91 @Override
slowrc86750e2017-08-22 17:26:47 -070092 public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException, InterruptedException {
slowr13fa5b02017-08-08 16:32:31 -070093 SctpMessage sctpMessage = (SctpMessage) msg;
94 ByteBuf byteBuf = sctpMessage.content();
95
96 byte[] bytes = new byte[byteBuf.readableBytes()];
97 byteBuf.readBytes(bytes);
98
slowr577f3222017-08-28 10:49:08 -070099 XrancPdu recvPdu = new XrancPdu();
slowr13fa5b02017-08-08 16:32:31 -0700100
101 InputStream inputStream = new ByteArrayInputStream(bytes);
102
slowr577f3222017-08-28 10:49:08 -0700103 recvPdu.decode(inputStream);
slowr13fa5b02017-08-08 16:32:31 -0700104
slowr577f3222017-08-28 10:49:08 -0700105 controller.packetAgent.handlePacket(recvPdu, ctx);
slowr13fa5b02017-08-08 16:32:31 -0700106 }
107
108 @Override
109 public void channelInactive(ChannelHandlerContext ctx) {
110 log.info("Client dropped the connection");
111
112 final SocketAddress address = ctx.channel().remoteAddress();
113 if (!(address instanceof InetSocketAddress)) {
114 log.warn("Invalid client connection. Xran Cell is indentifed based on IP");
115 ctx.close();
116 return;
117 }
118
119 final InetSocketAddress inetAddress = (InetSocketAddress) address;
120 final String host = inetAddress.getHostString();
121
122 controller.deviceAgent.removeConnectedCell(host);
123
124 ctx.close();
125 }
126
127 @Override
128 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
129 // Close the connection when an exception is raised.
130 log.warn("exceptionCaught: {}", ExceptionUtils.getStackTrace(cause));
131 cause.printStackTrace();
132 ctx.close();
133 }
134}
135