Replaced slice() with readSlice() where appropriate.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPByteToMessageDecoder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufUtil;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPMessageConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * A PCEP message parser which also does validation.
26  */
27 public final class PCEPByteToMessageDecoder extends ByteToMessageDecoder {
28     private static final Logger LOG = LoggerFactory.getLogger(PCEPByteToMessageDecoder.class);
29
30     private final MessageRegistry registry;
31
32     public PCEPByteToMessageDecoder(final MessageRegistry registry) {
33         this.registry = Preconditions.checkNotNull(registry);
34     }
35
36     @Override
37     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
38         if (!in.isReadable()) {
39             LOG.debug("No more content in incoming buffer.");
40             return;
41         }
42
43         in.markReaderIndex();
44         LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
45
46         final List<Message> errors = new ArrayList<>();
47
48         try {
49             out.add(parse(in, errors));
50         } catch (final PCEPDeserializerException e) {
51             LOG.debug("Failed to decode protocol message", e);
52         }
53         in.discardReadBytes();
54
55         if (!errors.isEmpty()) {
56             // We have a bunch of messages, send them out
57             for (final Object e : errors) {
58                 ctx.write(e);
59             }
60             ctx.flush();
61         }
62     }
63
64     private Message parse(final ByteBuf buffer, final List<Message> errors) throws PCEPDeserializerException {
65         buffer.readerIndex(buffer.readerIndex() + 1);
66         final int type = buffer.readUnsignedByte();
67         final int msgLength = buffer.readUnsignedShort();
68         final int actualLength = buffer.readableBytes();
69         final ByteBuf msgBody = buffer.slice();
70         if (actualLength != msgLength - PCEPMessageConstants.COMMON_HEADER_LENGTH) {
71             throw new PCEPDeserializerException("Body size " + actualLength + " does not match header size "
72                     + (msgLength - PCEPMessageConstants.COMMON_HEADER_LENGTH));
73         }
74         buffer.readerIndex(buffer.readerIndex() + actualLength);
75         return this.registry.parseMessage(type, msgBody, errors);
76     }
77 }