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
8 package org.opendaylight.protocol.framework;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.handler.codec.ByteToMessageDecoder;
14 import java.util.Arrays;
15 import java.util.List;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 final class ProtocolMessageDecoder extends ByteToMessageDecoder {
22 private final static Logger logger = LoggerFactory.getLogger(ProtocolMessageDecoder.class);
24 private final ProtocolMessageFactory factory;
26 public ProtocolMessageDecoder(final ProtocolMessageFactory factory) {
27 this.factory = factory;
31 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
33 ProtocolMessage msg = null;
35 final byte[] bytes = new byte[in.readableBytes()];
36 logger.debug("Received to decode: {}", Arrays.toString(bytes));
38 msg = this.factory.parse(bytes);
39 } catch (DeserializerException | DocumentedException e) {
40 this.exceptionCaught(ctx, e);
42 in.discardReadBytes();