blob: 764f55c6f1d3ce2c6dd57f5110ef701aee2504f4 [file] [log] [blame]
slowr13fa5b02017-08-08 16:32:31 -07001/*
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 */
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/**
39 * Created by dimitris on 7/20/17.
40 */
41
42@Sharable
43public class XranChannelHandler extends ChannelInboundHandlerAdapter {
44
45 private static final Logger log =
46 LoggerFactory.getLogger(XranChannelHandler.class);
47
48 private final Controller controller;
49
50 XranChannelHandler(Controller controller) {
51 this.controller = controller;
52 }
53
54 public static SctpMessage getSctpMessage(XrancPdu pdu) throws IOException {
55 BerByteArrayOutputStream os = new BerByteArrayOutputStream(4096);
56
57 pdu.encode(os);
58
59 log.debug("Sending message: {}", pdu);
60 final ByteBuf buf = Unpooled.buffer(os.getArray().length);
61 for (int i = 0; i < buf.capacity(); i++) {
62 buf.writeByte(os.getArray()[i]);
63 }
64 return new SctpMessage(0, 0, buf);
65 }
66
67 @Override
68 public void channelActive(ChannelHandlerContext ctx) throws IOException, URISyntaxException {
69 final SocketAddress address = ctx.channel().remoteAddress();
70 if (!(address instanceof InetSocketAddress)) {
71 log.warn("Invalid client connection. Xran Cell is indentifed based on IP");
72 ctx.close();
73 return;
74 }
75
76 final InetSocketAddress inetAddress = (InetSocketAddress) address;
77 final String host = inetAddress.getHostString();
78 log.info("New client connected to the Server: {}", host);
79
80 log.info("Adding device...");
81
82 controller.deviceAgent.addConnectedCell(host, ctx);
83 }
84
85 @Override
slowrc86750e2017-08-22 17:26:47 -070086 public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException, InterruptedException {
slowr13fa5b02017-08-08 16:32:31 -070087 SctpMessage sctpMessage = (SctpMessage) msg;
88 ByteBuf byteBuf = sctpMessage.content();
89
90 byte[] bytes = new byte[byteBuf.readableBytes()];
91 byteBuf.readBytes(bytes);
92
93 XrancPdu recv_pdu = new XrancPdu();
94
95 InputStream inputStream = new ByteArrayInputStream(bytes);
96
97 recv_pdu.decode(inputStream);
98
99 controller.packetAgent.handlePacket(recv_pdu, ctx);
100 }
101
102 @Override
103 public void channelInactive(ChannelHandlerContext ctx) {
104 log.info("Client dropped the connection");
105
106 final SocketAddress address = ctx.channel().remoteAddress();
107 if (!(address instanceof InetSocketAddress)) {
108 log.warn("Invalid client connection. Xran Cell is indentifed based on IP");
109 ctx.close();
110 return;
111 }
112
113 final InetSocketAddress inetAddress = (InetSocketAddress) address;
114 final String host = inetAddress.getHostString();
115
116 controller.deviceAgent.removeConnectedCell(host);
117
118 ctx.close();
119 }
120
121 @Override
122 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
123 // Close the connection when an exception is raised.
124 log.warn("exceptionCaught: {}", ExceptionUtils.getStackTrace(cause));
125 cause.printStackTrace();
126 ctx.close();
127 }
128}
129