X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=pcep%2Fspi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fpcep%2Fspi%2FObjectUtil.java;h=096481123d2b500fb449cd2d1fdffd95aec67dd6;hb=95355fd16e82f4c4e23c5c825a7649e1cace8413;hp=e92a3e24b121c855502d3c6a7be3418f6b37d55a;hpb=02da050ff4ec011ecb54b7e58870fdb4ae0bd196;p=bgpcep.git diff --git a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectUtil.java b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectUtil.java index e92a3e24b1..096481123d 100644 --- a/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectUtil.java +++ b/pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/ObjectUtil.java @@ -7,53 +7,33 @@ */ package org.opendaylight.protocol.pcep.spi; -import org.opendaylight.protocol.util.ByteArray; - -import com.google.common.primitives.UnsignedBytes; +import io.netty.buffer.ByteBuf; +import org.opendaylight.protocol.util.BitArray; public final class ObjectUtil { - private static final int HEADER_SIZE = 4; - - private static final int OC_F_LENGTH = 1; - private static final int OT_FLAGS_MF_LENGTH = 1; - private static final int OBJ_LENGTH_F_LENGTH = 2; - - private static final int OC_F_OFFSET = 0; - private static final int OT_FLAGS_MF_OFFSET = OC_F_OFFSET + OC_F_LENGTH; - private static final int OBJ_LENGTH_F_OFFSET = OT_FLAGS_MF_OFFSET + OT_FLAGS_MF_LENGTH; - - private static final int OT_SF_LENGTH = 4; - /* - * flags offsets inside multi-filed - */ - private static final int P_FLAG_OFFSET = 6; - private static final int I_FLAG_OFFSET = 7; - - private ObjectUtil() { - } - - public static byte[] formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore, - final byte[] valueBytes) { - - final byte[] bytes = new byte[HEADER_SIZE + valueBytes.length]; - - // objClass - bytes[0] = UnsignedBytes.checkedCast(objectClass); - - // objType_flags multi-field - bytes[OT_FLAGS_MF_OFFSET] = UnsignedBytes.checkedCast(objectType << (Byte.SIZE - OT_SF_LENGTH)); - if (processingRule != null && processingRule) { - bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (P_FLAG_OFFSET) - 1; - } - if (ignore != null && ignore) { - bytes[OT_FLAGS_MF_OFFSET] |= 1 << Byte.SIZE - (I_FLAG_OFFSET) - 1; - } - - // objLength - System.arraycopy(ByteArray.intToBytes(valueBytes.length + HEADER_SIZE, OBJ_LENGTH_F_LENGTH), 0, bytes, OBJ_LENGTH_F_OFFSET, - OBJ_LENGTH_F_LENGTH); - System.arraycopy(valueBytes, 0, bytes, HEADER_SIZE, valueBytes.length); - return bytes; - } + private static final int HEADER_SIZE = 4; + + private static final int FLAGS_SIZE = 4; + /* + * flags offsets inside multi-field + */ + private static final int PROCESSED = 2; + private static final int IGNORED = 3; + + private ObjectUtil() { + } + + public static void formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, + final Boolean ignore, final ByteBuf body, final ByteBuf out) { + out.writeByte(objectClass); + final BitArray flags = new BitArray(FLAGS_SIZE); + flags.set(IGNORED, ignore); + flags.set(PROCESSED, processingRule); + final byte flagB = flags.toByte(); + final int typeByte = objectType << FLAGS_SIZE | flagB & 0xff; + out.writeByte(typeByte); + out.writeShort(body.writerIndex() + HEADER_SIZE); + out.writeBytes(body); + } }