/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.pcep.parser.object; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; import java.util.ArrayList; import java.util.List; import org.opendaylight.protocol.pcep.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.CommonObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.RROSubobjectRegistry; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractRROWithSubobjectsParser extends CommonObjectParser implements ObjectSerializer { private static final Logger LOG = LoggerFactory.getLogger(AbstractRROWithSubobjectsParser.class); private final RROSubobjectRegistry subobjReg; private static final int HEADER_LENGTH = 2; protected AbstractRROWithSubobjectsParser(final RROSubobjectRegistry subobjReg, final int objectClass, final int objectType) { super(objectClass, objectType); this.subobjReg = requireNonNull(subobjReg); } protected List parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException { checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final List subs = new ArrayList<>(); while (buffer.isReadable()) { final int type = buffer.readUnsignedByte(); final int length = buffer.readUnsignedByte() - HEADER_LENGTH; if (length > buffer.readableBytes()) { throw new PCEPDeserializerException( "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes()); } LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer)); final Subobject sub = subobjReg.parseSubobject(type, buffer.readSlice(length)); if (sub == null) { LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } } return subs; } protected final void serializeSubobject(final List subobjects, final ByteBuf buffer) { checkArgument(subobjects != null && !subobjects.isEmpty(), "RRO must contain at least one subobject."); for (final Subobject subobject : subobjects) { subobjReg.serializeSubobject(subobject, buffer); } } }