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