Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame^] | 1 | /* |
| 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 | package org.onosproject.artemis.impl.moas; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.google.common.annotations.Beta; |
| 21 | import io.netty.buffer.ByteBuf; |
| 22 | import io.netty.buffer.Unpooled; |
| 23 | import io.netty.channel.ChannelHandler.Sharable; |
| 24 | import io.netty.channel.ChannelHandlerContext; |
| 25 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 26 | import io.netty.util.CharsetUtil; |
| 27 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onosproject.artemis.ArtemisPacketProcessor; |
| 31 | import org.onosproject.artemis.impl.objects.ArtemisMessage; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | /** |
| 38 | * MOAS Client channel handler. |
| 39 | */ |
| 40 | @Sharable |
| 41 | @Beta |
| 42 | public class MoasClientHandler extends ChannelInboundHandlerAdapter { |
| 43 | |
| 44 | private static final Logger log = |
| 45 | LoggerFactory.getLogger(MoasClientHandler.class); |
| 46 | |
| 47 | private IpAddress localIp; |
| 48 | private IpPrefix localPrefix; |
| 49 | private ArtemisPacketProcessor packetProcessor; |
| 50 | |
| 51 | MoasClientHandler(IpAddress localIp, IpPrefix localPrefix, ArtemisPacketProcessor packetProcessor) { |
| 52 | this.localIp = localIp; |
| 53 | this.packetProcessor = packetProcessor; |
| 54 | this.localPrefix = localPrefix; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| 59 | log.info("Connected to server {}", ctx.channel().remoteAddress()); |
| 60 | |
| 61 | ArtemisMessage message = new ArtemisMessage(); |
| 62 | message.setType(ArtemisMessage.Type.INITIATE_FROM_CLIENT); |
| 63 | message.setLocalIp(localIp.toString()); |
| 64 | message.setLocalPrefix(localPrefix.toString()); |
| 65 | |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | try { |
| 68 | String jsonInString = mapper.writeValueAsString(message); |
| 69 | ByteBuf buffer = Unpooled.copiedBuffer(jsonInString, CharsetUtil.UTF_8); |
| 70 | ctx.writeAndFlush(buffer); |
| 71 | } catch (JsonProcessingException e) { |
| 72 | e.printStackTrace(); |
| 73 | log.warn(ExceptionUtils.getFullStackTrace(e)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { |
| 79 | ByteBuf in = (ByteBuf) msg; |
| 80 | String strMsg = in.toString(io.netty.util.CharsetUtil.US_ASCII); |
| 81 | |
| 82 | ObjectMapper mapper = new ObjectMapper(); |
| 83 | ArtemisMessage actObj = mapper.readValue(strMsg, ArtemisMessage.class); |
| 84 | |
| 85 | packetProcessor.processMoasPacket(actObj, ctx); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 90 | log.error(ExceptionUtils.getFullStackTrace(cause)); |
| 91 | cause.printStackTrace(); |
| 92 | ctx.close(); |
| 93 | } |
| 94 | |
| 95 | } |