/* * 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.impl.object; import java.util.List; import org.opendaylight.protocol.pcep.spi.ObjectParser; import org.opendaylight.protocol.pcep.spi.ObjectSerializer; import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.RROSubobjectHandlerRegistry; import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer; import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobjects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; public abstract class AbstractRROWithSubobjectsParser implements ObjectParser, ObjectSerializer { private static final Logger LOG = LoggerFactory.getLogger(AbstractRROWithSubobjectsParser.class); private static final int SUB_TYPE_FLAG_F_LENGTH = 1; private static final int SUB_LENGTH_F_LENGTH = 1; private static final int TYPE_FLAG_F_OFFSET = 0; private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH; private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH; private final RROSubobjectHandlerRegistry subobjReg; protected AbstractRROWithSubobjectsParser(final RROSubobjectHandlerRegistry subobjReg) { this.subobjReg = Preconditions.checkNotNull(subobjReg); } protected List parseSubobjects(final byte[] bytes) throws PCEPDeserializerException { if (bytes == null) { throw new IllegalArgumentException("Byte array is mandatory."); } int type; byte[] soContentsBytes; int length; int offset = 0; final List subs = Lists.newArrayList(); while (offset < bytes.length) { length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH)); type = bytes[offset + TYPE_FLAG_F_OFFSET]; if (length > bytes.length - offset) { throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= " + (bytes.length - offset)); } soContentsBytes = new byte[length - SO_CONTENTS_OFFSET]; System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET); LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes)); final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes); LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); offset += length; } return subs; } protected final byte[] serializeSubobject(final List subobjects) { final List result = Lists.newArrayList(); int finalLength = 0; for (final Subobjects subobject : subobjects) { final RROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType()); final byte[] bytes = serializer.serializeSubobject(subobject); finalLength += bytes.length; result.add(bytes); } final byte[] resultBytes = new byte[finalLength]; int byteOffset = 0; for (final byte[] b : result) { System.arraycopy(b, 0, resultBytes, byteOffset, b.length); byteOffset += b.length; } return resultBytes; } }