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.netconf.nettyutil.handler;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.CompositeByteBuf;
13 import io.netty.buffer.Unpooled;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.util.List;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class NetconfChunkAggregator extends ByteToMessageDecoder {
21 private static final Logger LOG = LoggerFactory.getLogger(NetconfChunkAggregator.class);
22 private static final String GOT_PARAM_WHILE_WAITING_FOR_PARAM = "Got byte {} while waiting for {}";
23 private static final String GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM = "Got byte {} while waiting for {}-{}";
24 public static final int DEFAULT_MAXIMUM_CHUNK_SIZE = 16 * 1024 * 1024;
29 HEADER_LENGTH_FIRST, // [1-9]
30 HEADER_LENGTH_OTHER, // [0-9]*\n
38 private final int maxChunkSize = DEFAULT_MAXIMUM_CHUNK_SIZE;
39 private State state = State.HEADER_ONE;
40 private long chunkSize;
41 private CompositeByteBuf chunk;
43 private static void checkNewLine(final byte byteToCheck, final String errorMessage) {
44 if (byteToCheck != '\n') {
45 LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM, byteToCheck, (byte)'\n');
46 throw new IllegalStateException(errorMessage);
50 private static void checkHash(final byte byteToCheck, final String errorMessage) {
51 if (byteToCheck != '#') {
52 LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM, byteToCheck, (byte)'#');
53 throw new IllegalStateException(errorMessage);
57 private void checkChunkSize() {
58 if (chunkSize > maxChunkSize) {
59 LOG.debug("Parsed chunk size {}, maximum allowed is {}", chunkSize, maxChunkSize);
60 throw new IllegalStateException("Maximum chunk size exceeded");
65 protected void decode(final ChannelHandlerContext ctx,
66 final ByteBuf in, final List<Object> out) throws IllegalStateException {
67 while (in.isReadable()) {
71 final byte b = in.readByte();
72 checkNewLine(b, "Malformed chunk header encountered (byte 0)");
73 state = State.HEADER_TWO;
79 final byte b = in.readByte();
80 checkHash(b, "Malformed chunk header encountered (byte 1)");
81 state = State.HEADER_LENGTH_FIRST;
84 case HEADER_LENGTH_FIRST:
86 final byte b = in.readByte();
87 chunkSize = processHeaderLengthFirst(b);
88 state = State.HEADER_LENGTH_OTHER;
91 case HEADER_LENGTH_OTHER:
93 final byte b = in.readByte();
98 if (b < '0' || b > '9') {
99 LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM, b, (byte)'0', (byte)'9');
100 throw new IllegalStateException("Invalid chunk size encountered");
103 chunkSize += b - '0';
109 * FIXME: this gathers all data into one big chunk before passing
110 * it on. Make sure the pipeline can work with partial data
111 * and then change this piece to pass the data on as it
114 if (in.readableBytes() < chunkSize) {
115 LOG.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize);
116 in.discardReadBytes();
119 aggregateChunks(in.readBytes((int) chunkSize));
120 state = State.FOOTER_ONE;
124 final byte b = in.readByte();
125 checkNewLine(b,"Malformed chunk footer encountered (byte 0)");
126 state = State.FOOTER_TWO;
132 final byte b = in.readByte();
133 checkHash(b,"Malformed chunk footer encountered (byte 1)");
134 state = State.FOOTER_THREE;
139 final byte b = in.readByte();
140 // In this state, either header-of-new-chunk or message-end is expected
141 // Depends on the next character
142 extractNewChunkOrMessageEnd(b);
147 final byte b = in.readByte();
148 checkNewLine(b,"Malformed chunk footer encountered (byte 3)");
149 state = State.HEADER_ONE;
156 LOG.info("Unknown state.");
161 in.discardReadBytes();
164 private void extractNewChunkOrMessageEnd(final byte byteToCheck) {
165 if (isHeaderLengthFirst(byteToCheck)) {
166 // Extract header length#1 from new chunk
167 chunkSize = processHeaderLengthFirst(byteToCheck);
168 // Proceed with next chunk processing
169 state = State.HEADER_LENGTH_OTHER;
170 } else if (byteToCheck == '#') {
171 state = State.FOOTER_FOUR;
173 LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM, byteToCheck, (byte) '#', (byte) '1', (byte) '9');
174 throw new IllegalStateException("Malformed chunk footer encountered (byte 2)");
178 private void initChunk() {
179 chunk = Unpooled.compositeBuffer();
182 private void aggregateChunks(final ByteBuf newChunk) {
183 chunk.addComponent(chunk.numComponents(), newChunk);
185 // Update writer index, addComponent does not update it
186 chunk.writerIndex(chunk.writerIndex() + newChunk.readableBytes());
189 private static int processHeaderLengthFirst(final byte byteToCheck) {
190 if (!isHeaderLengthFirst(byteToCheck)) {
191 LOG.debug(GOT_PARAM_WHILE_WAITING_FOR_PARAM_PARAM, byteToCheck, (byte)'1', (byte)'9');
192 throw new IllegalStateException("Invalid chunk size encountered (byte 0)");
195 return byteToCheck - '0';
198 private static boolean isHeaderLengthFirst(final byte byteToCheck) {
199 return byteToCheck >= '1' && byteToCheck <= '9';