Ignore unknown subobjects while parsing RRO/ERO objects in PCEP messages
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.object;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufUtil;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
22 import org.opendaylight.protocol.util.Values;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public abstract class AbstractXROWithSubobjectsParser extends CommonObjectParser implements 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         final int objectClass, final int objectType) {
37         super(objectClass, objectType);
38         this.subobjReg = requireNonNull(subobjReg);
39     }
40
41     protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
42         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
43             "Array of bytes is mandatory. Can't be null or empty.");
44         final List<Subobject> subs = new ArrayList<>();
45         while (buffer.isReadable()) {
46             final boolean mandatory =
47                 ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false;
48             final int type = UnsignedBytes.checkedCast((buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES)
49                 & ~(1 << Values.FIRST_BIT_OFFSET));
50             final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
51             if (length > buffer.readableBytes()) {
52                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
53                         + buffer.readableBytes());
54             }
55             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
56             final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), mandatory);
57             if (sub == null) {
58                 LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type);
59             } else {
60                 LOG.debug("Subobject was parsed. {}", sub);
61                 subs.add(sub);
62             }
63         }
64         return subs;
65     }
66
67     protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
68         Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(),
69             "XRO must contain at least one subobject.");
70         for (final Subobject subobject : subobjects) {
71             this.subobjReg.serializeSubobject(subobject, buffer);
72         }
73     }
74 }