Bug-612: Added check - before subobjects are serialized.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / AbstractXROWithSubobjectsParser.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.object;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.ObjectParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
25
26     private static final Logger LOG = LoggerFactory.getLogger(AbstractXROWithSubobjectsParser.class);
27
28     private static final int HEADER_LENGTH = 2;
29
30     private final XROSubobjectRegistry subobjReg;
31
32     protected AbstractXROWithSubobjectsParser(final XROSubobjectRegistry subobjReg) {
33         this.subobjReg = Preconditions.checkNotNull(subobjReg);
34     }
35
36     protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
37         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
38         final List<Subobject> subs = new ArrayList<>();
39         while (buffer.isReadable()) {
40             final boolean mandatory = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
41             int type = UnsignedBytes.checkedCast((buffer.readByte() & 0xff) & ~(1 << 7));
42             int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
43             if (length > buffer.readableBytes()) {
44                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
45                         + buffer.readableBytes());
46             }
47             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
48             final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), mandatory);
49             if (sub == null) {
50                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
51             } else {
52                 LOG.debug("Subobject was parsed. {}", sub);
53                 subs.add(sub);
54             }
55             buffer.readerIndex(buffer.readerIndex() + length);
56         }
57         return subs;
58     }
59
60     protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
61         Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(), "XRO must contain at least one subobject.");
62         for (final Subobject subobject : subobjects) {
63             this.subobjReg.serializeSubobject(subobject, buffer);
64         }
65     }
66 }