2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class AbstractEROWithSubobjectsParser extends CommonObjectParser implements ObjectSerializer {
28 private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
30 private static final int HEADER_LENGTH = 2;
32 private final EROSubobjectRegistry subobjReg;
34 protected AbstractEROWithSubobjectsParser(final EROSubobjectRegistry subobjReg, final int objectClass,
35 final int objectType) {
36 super(objectClass, objectType);
37 this.subobjReg = requireNonNull(subobjReg);
40 protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
41 // Explicit approval of empty ERO
42 Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
43 final List<Subobject> subs = new ArrayList<>();
44 while (buffer.isReadable()) {
45 final boolean loose = ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0)
48 final int type = (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(
53 "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
55 LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
56 final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), loose);
58 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
60 LOG.debug("Subobject was parsed. {}", sub);
67 protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
68 if (subobjects != null) {
69 for (final Subobject subobject : subobjects) {
70 this.subobjReg.serializeSubobject(subobject, buffer);