karthik1977 | 6806c98 | 2024-02-17 18:46:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024-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.sflow.impl; |
| 17 | |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.channel.Channel; |
| 21 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 22 | import org.jboss.netty.handler.codec.frame.FrameDecoder; |
| 23 | |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Decode an sFlow message from a Channel, for use in a netty pipeline. |
| 29 | */ |
| 30 | public class SflowMessageDecoder extends FrameDecoder { |
| 31 | |
| 32 | private static final Logger log = LoggerFactory.getLogger(SflowMessageDecoder.class); |
| 33 | |
| 34 | @Override |
| 35 | protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { |
| 36 | |
| 37 | try { |
| 38 | if (buffer.readableBytes() > 0) { |
| 39 | byte[] bytes = new byte[buffer.readableBytes()]; |
| 40 | buffer.readBytes(bytes); |
| 41 | ctx.setAttachment(null); |
karthik1977 | 4bb1ec0 | 2024-02-20 22:26:15 +0530 | [diff] [blame^] | 42 | return SflowPacket.deserializer().deserialize(bytes, 0, bytes.length); |
karthik1977 | 6806c98 | 2024-02-17 18:46:43 +0530 | [diff] [blame] | 43 | } |
| 44 | } catch (Exception e) { |
| 45 | log.error("sFlow message decode error ", e); |
| 46 | buffer.resetReaderIndex(); |
| 47 | buffer.discardReadBytes(); |
| 48 | } |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |