blob: 6a6a0ab4d5dd5faae3411bd8711fe88a294ae40a [file] [log] [blame]
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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.artemis.impl.moas;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.google.common.annotations.Beta;
21import io.netty.buffer.ByteBuf;
22import io.netty.channel.ChannelHandler.Sharable;
23import io.netty.channel.ChannelHandlerContext;
24import io.netty.channel.ChannelInboundHandlerAdapter;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070025import org.onlab.packet.IpAddress;
26import org.onosproject.artemis.impl.objects.ArtemisMessage;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.io.IOException;
31import java.net.InetSocketAddress;
32import java.net.SocketAddress;
33
34/**
35 * MOAS Server channel handler.
36 */
37@Sharable
38@Beta
39public class MoasServerHandler extends ChannelInboundHandlerAdapter {
40
41 private static final Logger log =
42 LoggerFactory.getLogger(MoasServerHandler.class);
43
44 private MoasServerController controller;
45
46 MoasServerHandler(MoasServerController controller) {
47 this.controller = controller;
48 }
49
50 @Override
51 public void channelActive(ChannelHandlerContext ctx) throws Exception {
52 final SocketAddress address = ctx.channel().remoteAddress();
53 if (!(address instanceof InetSocketAddress)) {
54 log.warn("Invalid client connection. MOAS is identified based on IP");
55 ctx.close();
56 return;
57 }
58
59 final InetSocketAddress inetAddress = (InetSocketAddress) address;
60 final String host = inetAddress.getHostString();
61 log.info("New client connected to the Server: {}", host);
62
63 controller.deviceAgent.addMoas(IpAddress.valueOf(host), ctx);
64 }
65
66 @Override
67 public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException {
68 ByteBuf in = (ByteBuf) msg;
69 String strMsg = in.toString(io.netty.util.CharsetUtil.US_ASCII);
70
71 ObjectMapper mapper = new ObjectMapper();
72 ArtemisMessage actObj = mapper.readValue(strMsg, ArtemisMessage.class);
73
74 controller.packetAgent.processMoasPacket(actObj, ctx);
75 }
76
77 @Override
78 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Ray Milkeyba547f92018-02-01 15:22:31 -080079 log.error("exceptionCaught()", cause);
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070080 ctx.close();
81 }
82
83}