/** * Returns a new BitArray with given byte array as backing * array. * * @param bytes byte array * @return new BitArray */ public static BitArray valueOf(final byte[] bytes) { return new BitArray(bytes, bytes.length); }
/** * Returns new BitArray with given byte as backing * array. * * @param b unsigned byte * @return new BitArray */ public static BitArray valueOf(final byte b) { return new BitArray(new byte[] {b}, Byte.SIZE); }
/** * Returns a new BitArray created from bytes from given ByteBuf. * * @param buffer ByteBuf, whose readerIndex will be moved by * minimum number of bytes required for the bit size. * @param size Number of bits to be allocated in BitArray * @return new BitArray */ public static BitArray valueOf(final ByteBuf buffer, final int size) { Preconditions.checkArgument(size >= 1, "Minimum size is 1 bit."); Preconditions.checkNotNull(buffer, "Byte Array cannot be null"); final byte[] b = new byte[calculateBytes(size)]; buffer.readBytes(b, 0, b.length); return new BitArray(b, size); }
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; out.writeByte(typeByte); out.writeShort(body.writerIndex() + HEADER_SIZE); out.writeBytes(body); } }
public static void formatLabel(final int type, final Boolean unidirectional, final Boolean global, final ByteBuf body, final ByteBuf buffer) { final BitArray reserved = new BitArray(FLAGS_SIZE); reserved.set(UNIDIRECTIONAL, unidirectional); reserved.set(GLOBAL, global); reserved.toByteBuf(buffer); buffer.writeByte(type); buffer.writeBytes(body); } }
@Override public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf bytes) { Preconditions.checkArgument(tlv instanceof Ipv6SidTlv, "Incoming TLV is not Ipv6SidTlv"); final Ipv6SidTlv ipv6Tlv = (Ipv6SidTlv) tlv; bytes.writeZero(RESERVED); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(PROCESS_IPV6_HEADER_FLAG, ipv6Tlv.isProcessIpv6HeadAbility()); flags.toByteBuf(bytes); }
static void serializeEroFlags(final ByteBuf buffer, final Boolean loose) { final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(LOOSE, loose); flags.toByteBuf(buffer); buffer.writeZero(RESERVED_ERO); } }
private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator) { if (mplsProtocolMask != null) { final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1); final BitArray mask = new BitArray(FLAGS_SIZE); mask.set(LDP_BIT, mplsProtocolMask.isLdp()); mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte()); mask.toByteBuf(mplsProtocolMaskBuf); TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator); } } }
public static void serializeSrRange(final SrRange srRange, final ByteBuf aggregator) { final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(INNER_AREA, srRange.isInterArea()); flags.toByteBuf(aggregator); aggregator.writeZero(RESERVED); aggregator.writeShort(srRange.getRangeSize()); serializeSubTlvs(aggregator, srRange.getSubTlvs()); }
@Override public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) { Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficActionExtendedCommunity, "The extended community %s is not TrafficActionExtendedCommunityCase type.", extendedCommunity); final TrafficActionExtendedCommunity trafficAction = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficActionExtendedCommunity) extendedCommunity).getTrafficActionExtendedCommunity(); byteAggregator.writeZero(RESERVED); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(SAMPLE_BIT, trafficAction.isSample()); flags.set(TERMINAL_BIT, trafficAction.isTerminalAction()); flags.toByteBuf(byteAggregator); }
@Override public void serialize(final BitmaskOperand op, final int length, final ByteBuf buffer) { final BitArray bs = new BitArray(OPERAND_LENGTH); bs.set(END_OF_LIST, op.isEndOfList()); bs.set(AND_BIT, op.isAndBit()); bs.set(MATCH, op.isMatch()); bs.set(NOT, op.isNot()); final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT); buffer.writeByte(bs.toByte() | len); }
@Override public void serializeObject(final Object object, final ByteBuf buffer) { Preconditions.checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.", object.getClass()); final NoPath nPObj = (NoPath) object; final ByteBuf body = Unpooled.buffer(); Preconditions.checkArgument(nPObj.getNatureOfIssue() != null, "NatureOfIssue is mandatory."); writeUnsignedByte(nPObj.getNatureOfIssue(), body); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints()); flags.toByteBuf(body); body.writeZero(RESERVED_F_LENGTH); serializeTlvs(nPObj.getTlvs(), body); ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer); }
@Override public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) { Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass()); final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix(); final IpPrefix prefix = specObj.getIpPrefix(); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable()); flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse()); final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH); Preconditions.checkArgument(prefix.getIpv6Prefix() != null, "Ipv6Prefix is mandatory."); writeIpv6Prefix(prefix.getIpv6Prefix(), body); flags.toByteBuf(body); RROSubobjectUtil.formatSubobject(TYPE, body, buffer); } }
@Override public final void serialize(final NumericOperand operand, final int length, final ByteBuf buffer) { final BitArray operandValues = new BitArray(OPERAND_LENGTH); operandValues.set(END_OF_LIST, operand.isEndOfList()); operandValues.set(AND_BIT, operand.isAndBit()); operandValues.set(LESS_THAN, operand.isLessThan()); operandValues.set(GREATER_THAN, operand.isGreaterThan()); operandValues.set(EQUAL, operand.isEquals()); final byte byteLength = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT); buffer.writeByte(operandValues.toByte() | byteLength); }
@Override protected final byte serializeFragment(final Fragment fragment) { final BitArray bs = new BitArray(Byte.SIZE); bs.set(DONT_FRAGMENT, Boolean.FALSE); bs.set(FIRST_FRAGMENT, fragment.isFirst()); bs.set(IS_A_FRAGMENT, fragment.isIsA()); bs.set(LAST_FRAGMENT, fragment.isLast()); return bs.toByte(); } }
@Override public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) { Preconditions.checkArgument(extendedCommunity instanceof Layer2AttributesExtendedCommunityCase, "The extended community %s is not EsImportRouteExtendedCommunityCaseCase type.", extendedCommunity); final Layer2AttributesExtendedCommunity extCom = ((Layer2AttributesExtendedCommunityCase) extendedCommunity).getLayer2AttributesExtendedCommunity(); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(PRIMARY_PE_OFFSET, extCom.isPrimaryPe()); flags.set(BACKUP_PE_OFFSET, extCom.isBackupPe()); flags.set(CONTROL_WORD_OFFSET, extCom.isControlWord()); flags.toByteBuf(body); ByteBufWriteUtil.writeUnsignedShort(extCom.getL2Mtu(), body); body.writeZero(RESERVED); }
@Override protected final byte serializeFragment(final Fragment fragment) { final BitArray bs = new BitArray(Byte.SIZE); bs.set(DONT_FRAGMENT, fragment.isDoNot()); bs.set(FIRST_FRAGMENT, fragment.isFirst()); bs.set(IS_A_FRAGMENT, fragment.isIsA()); bs.set(LAST_FRAGMENT, fragment.isLast()); return bs.toByte(); } }
public static void serializeSrCapabilities(final SrCapabilities caps, final ByteBuf buffer) { final BitArray bs = new BitArray(FLAGS_SIZE); bs.set(MPLS_IPV4, caps.isMplsIpv4()); bs.set(MPLS_IPV6, caps.isMplsIpv6()); bs.set(SR_IPV6, caps.isSrIpv6()); bs.toByteBuf(buffer); buffer.writeZero(RESERVERED); buffer.writeMedium(caps.getRangeSize().intValue()); TlvUtil.writeTLV(SID_TYPE, SidLabelIndexParser.serializeSidValue(caps.getSidLabelIndex()), buffer); }
private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) { if (nodeFlagBits != null) { final ByteBuf nodeFlagBuf = Unpooled.buffer(1); final BitArray flags = new BitArray(FLAGS_SIZE); flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload()); flags.set(ATTACHED_BIT, nodeFlagBits.isAttached()); flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal()); flags.set(ABBR_BIT, nodeFlagBits.isAbr()); flags.set(ROUTER_BIT, nodeFlagBits.isRouter()); flags.set(V6_BIT, nodeFlagBits.isV6()); flags.toByteBuf(nodeFlagBuf); TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator); } } }
private static BitArray serializeBindingSidFlags(final Flags flags) { final BitArray bitFlags = new BitArray(FLAGS_SIZE); if (flags instanceof IsisBindingFlagsCase) { final IsisBindingFlagsCase isisFlags = (IsisBindingFlagsCase) flags; bitFlags.set(AFI, isisFlags.isAddressFamily()); bitFlags.set(MIRROR_CONTEXT, isisFlags.isMirrorContext()); bitFlags.set(SPREAD_TLV, isisFlags.isSpreadTlv()); bitFlags.set(LEAKED, isisFlags.isLeakedFromLevel2()); bitFlags.set(ATTACHED, isisFlags.isAttachedFlag()); } else if (flags instanceof OspfBindingFlagsCase) { final OspfBindingFlagsCase ospfFlags = (OspfBindingFlagsCase) flags; bitFlags.set(MIRROR_CONTEXT_OSPF, ospfFlags.isMirroring()); } return bitFlags; } }