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