2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.util.handler;
11 import com.google.common.base.Charsets;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
16 import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.util.List;
22 public class NetconfMessageAggregator extends ByteToMessageDecoder {
24 private final static Logger logger = LoggerFactory.getLogger(NetconfMessageAggregator.class);
26 private byte[] eom = NetconfMessageConstants.endOfMessage;
28 public NetconfMessageAggregator(FramingMechanism framingMechanism) {
29 if (framingMechanism == FramingMechanism.CHUNK) {
30 eom = NetconfMessageConstants.endOfChunk;
35 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
36 int index = indexOfSequence(in, eom);
38 logger.debug("Message is not complete, read again.");
39 if (logger.isTraceEnabled()) {
40 String str = in.toString(Charsets.UTF_8);
41 logger.trace("Message read so far: {}", str);
45 ByteBuf msg = in.readBytes(index);
46 in.readBytes(eom.length);
47 in.discardReadBytes();
48 logger.debug("Message is complete.");
53 private int indexOfSequence(ByteBuf in, byte[] sequence) {
55 for (int i = 0; i < in.readableBytes() - sequence.length + 1; i++) {
56 if (in.getByte(i) == sequence[0]) {
58 for (int j = 1; j < sequence.length; j++) {
59 if (in.getByte(i + j) != sequence[j]) {