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.pcep.impl;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.opendaylight.protocol.framework.DeserializerException;
19 import org.opendaylight.protocol.pcep.spi.MessageHandlerRegistry;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Preconditions;
26 import com.google.common.primitives.UnsignedBytes;
29 * A PCEP message parser which also does validation.
31 public final class PCEPByteToMessageDecoder extends ByteToMessageDecoder {
32 private final static Logger LOG = LoggerFactory.getLogger(PCEPByteToMessageDecoder.class);
34 private final static int TYPE_SIZE = 1; // bytes
36 private final static int LENGTH_SIZE = 2; // bytes
38 private final MessageHandlerRegistry registry;
40 public PCEPByteToMessageDecoder(final MessageHandlerRegistry registry) {
41 this.registry = Preconditions.checkNotNull(registry);
45 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
46 if (in.readableBytes() == 0) {
47 LOG.debug("No more content in incoming buffer.");
52 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
53 final byte[] bytes = new byte[in.readableBytes()];
56 final List<Message> errors = new ArrayList<>();
59 out.add(parse(bytes, errors));
60 } catch (DeserializerException e) {
61 LOG.debug("Failed to decode protocol message", e);
62 this.exceptionCaught(ctx, e);
64 in.discardReadBytes();
66 if (!errors.isEmpty()) {
67 // We have a bunch of messages, send them out
68 for (final Object e : errors) {
69 ctx.channel().write(e);
71 ctx.channel().flush();
75 private Message parse(final byte[] bytes, final List<Message> errors) throws DeserializerException {
76 final int type = UnsignedBytes.toInt(bytes[1]);
77 final int msgLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, TYPE_SIZE + 1, LENGTH_SIZE));
79 final byte[] msgBody = ByteArray.cutBytes(bytes, TYPE_SIZE + 1 + LENGTH_SIZE);
80 if (msgBody.length != msgLength - PCEPMessageConstants.COMMON_HEADER_LENGTH) {
81 throw new DeserializerException("Body size " + msgBody.length + " does not match header size "
82 + (msgLength - PCEPMessageConstants.COMMON_HEADER_LENGTH));
85 final Message msg = this.registry.getMessageParser(type).parseMessage(msgBody, errors);
86 LOG.debug("Message was parsed. {}", msg);