blob: 15c1e0b2f4cea3b8b1e70c96eff0c024e521054d [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;
26import org.onosproject.net.DeviceId;
27import org.onosproject.xran.codecs.pdu.XrancPdu;
28import org.openmuc.jasn1.ber.BerByteArrayOutputStream;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import javax.xml.bind.DatatypeConverter;
33import java.io.ByteArrayInputStream;
34import java.io.IOException;
35import java.io.InputStream;
36import java.net.InetSocketAddress;
37import java.net.SocketAddress;
38import java.net.URI;
39import java.net.URISyntaxException;
40import java.nio.charset.Charset;
41
42import static org.onosproject.net.DeviceId.deviceId;
43
44/**
45 * Created by dimitris on 7/20/17.
46 */
47
48@Sharable
49public class XranChannelHandler extends ChannelInboundHandlerAdapter {
50
51 private static final Logger log =
52 LoggerFactory.getLogger(XranChannelHandler.class);
53
54 private final Controller controller;
55
56 XranChannelHandler(Controller controller) {
57 this.controller = controller;
58 }
59
60 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
92 public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException {
93 SctpMessage sctpMessage = (SctpMessage) msg;
94 ByteBuf byteBuf = sctpMessage.content();
95
96 byte[] bytes = new byte[byteBuf.readableBytes()];
97 byteBuf.readBytes(bytes);
98
99 XrancPdu recv_pdu = new XrancPdu();
100
101 InputStream inputStream = new ByteArrayInputStream(bytes);
102
103 recv_pdu.decode(inputStream);
104
105 controller.packetAgent.handlePacket(recv_pdu, ctx);
106 }
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