blob: 9d8c93a65f54e4c326c0241ff5fcb3ea694006fb [file] [log] [blame]
karthik19776806c982024-02-17 18:46:43 +05301/*
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 */
16package org.onosproject.sflow.impl;
17
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.channel.Channel;
21import org.jboss.netty.channel.ChannelHandlerContext;
22import org.jboss.netty.handler.codec.frame.FrameDecoder;
23
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27/**
28 * Decode an sFlow message from a Channel, for use in a netty pipeline.
29 */
30public 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);
42 return null;
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