@Override protected byte[] readPceId(final ByteBuf buffer) { return ByteArray.readBytes(buffer, PCE_ID_F_LENGTH); }
/** * Encode input ByteBuf with Base64 to string format. * * @param buffer Input ByteBuf * @return String representation of encoded ByteBuf. */ public static String encodeBase64(final ByteBuf buffer) { return BaseEncoding.base64().encode(ByteArray.readAllBytes(buffer)); } }
/** * Writes BitSet's bits if not null, otherwise writes zeros to the * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by * specified length. * * @param bitSet * BitSet values to be written to the output, starting from left most bit. * @param outputLength * Number of bytes to be written, must be greater than 0. * @param output * ByteBuf, where bitSet or zeros are written. */ public static void writeBitSet(final BitSet bitSet, final int outputLength, final ByteBuf output) { Preconditions.checkArgument(outputLength > 0); if (bitSet != null) { output.writeBytes(ByteArray.bitSetToBytes(bitSet, outputLength)); } else { output.writeZero(outputLength); } }
@Override public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException { if (buffer == null) { return null; } if (buffer.readableBytes() < FLAGS_F_LENGTH) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= " + FLAGS_F_LENGTH + "."); } final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH)); final StatefulBuilder sb = new StatefulBuilder(); sb.setIncludeDbVersion(flags.get(S_FLAG_OFFSET)); sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET)); return sb.build(); }
private static List<DestinationPorts> parseDestinationPort(final ByteBuf nlri) { final List<DestinationPorts> ports = new ArrayList<>(); boolean end = false; // we can do this as all fields will be rewritten in the cycle final DestinationPortsBuilder builder = new DestinationPortsBuilder(); while (!end) { final byte b = nlri.readByte(); final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b); builder.setOp(op); final short length = AbstractOperandParser.parseLength(b); builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length))); end = op.isEndOfList(); ports.add(builder.build()); } return ports; } }
private static List<FlowLabel> parseFlowLabel(final ByteBuf nlri) { final List<FlowLabel> labels = new ArrayList<>(); boolean end = false; // we can do this as all fields will be rewritten in the cycle final FlowLabelBuilder builder = new FlowLabelBuilder(); while (!end) { final byte b = nlri.readByte(); final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b); builder.setOp(op); final short length = AbstractOperandParser.parseLength(b); builder.setValue(ByteArray.bytesToLong(ByteArray.readBytes(nlri, length))); end = op.isEndOfList(); labels.add(builder.build()); } return labels; } }
if (withdrawnRoutesLength > 0) { final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength)); builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build()); final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer)); if (!nlri.isEmpty()) {
break; case MAX_BANDWIDTH: builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth()); break; case MAX_RESERVABLE_BANDWIDTH: builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth()); break; break; case TE_METRIC: builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getTeMetric()); break; case METRIC: builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getMetric()); break; break; case LINK_NAME: builder.setLinkName(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII)); LOG.debug("Parsed Link Name : {}", builder.getLinkName()); break;
@Override public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException { Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty."); if (LOG.isTraceEnabled()) { LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer))); } final List<OptionalCapabilities> optionalCapas = Lists.newArrayList(); while (buffer.isReadable()) { final OptionalCapabilities optionalCapa = parseOptionalCapability(buffer); if (optionalCapa != null) { optionalCapas.add(optionalCapa); } } return new BgpParametersBuilder().setOptionalCapabilities(optionalCapas).build(); }
if (j == (i + MARKER_LENGTH)) { final int length = ByteArray.bytesToInt(new byte[]{ byteArray[j], byteArray[j + 1] });
final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(flagsByte, OT_SF_OFFSET, OT_SF_LENGTH)); final int objLength = bytes.readUnsignedShort();
private static List<TcpFlags> parseTcpFlags(final ByteBuf nlri) { final List<TcpFlags> flags = new ArrayList<>(); boolean end = false; // we can do this as all fields will be rewritten in the cycle final TcpFlagsBuilder builder = new TcpFlagsBuilder(); while (!end) { final byte b = nlri.readByte(); final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b); builder.setOp(op); final short length = AbstractOperandParser.parseLength(b); builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length))); end = op.isEndOfList(); flags.add(builder.build()); } return flags; } }
@Override public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException { if (buffer == null) { return null; } if (buffer.readableBytes() < FLAGS_F_LENGTH) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= " + FLAGS_F_LENGTH + "."); } final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH)); final StatefulBuilder sb = new StatefulBuilder(); sb.setIncludeDbVersion(flags.get(S_FLAG_OFFSET)); sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET)); if (flags.get(I_FLAG_OFFSET)) { sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build()); } return sb.build(); }
@Override public Bandwidth parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (bytes.readableBytes() != BANDWIDTH_F_LENGTH) { throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + BANDWIDTH_F_LENGTH + "."); } final BandwidthBuilder builder = new BandwidthBuilder(); builder.setIgnore(header.isIgnore()); builder.setProcessingRule(header.isProcessingRule()); builder.setBandwidth(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(ByteArray.getAllBytes(bytes))); return builder.build(); }
public static List<byte[]> parseMessages(final String c) { final String content = clearWhiteSpaceToUpper(c); final int sixteen = 16; final int four = 4; // search for 16 FFs final List<byte[]> messages = Lists.newLinkedList(); int idx = content.indexOf(FF_16, 0); while (idx > -1) { // next 2 bytes are length final int lengthIdx = idx + sixteen * 2; final int messageIdx = lengthIdx + four; final String hexLength = content.substring(lengthIdx, messageIdx); final byte[] byteLength = BaseEncoding.base16().decode(hexLength); final int length = ByteArray.bytesToInt(byteLength); final int messageEndIdx = idx + length * 2; // Assert that message is longer than minimum 19(header.length == 19) // If length in BGP message would be 0, loop would never end Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than " + MINIMAL_LENGTH); final String hexMessage = content.substring(idx, messageEndIdx); final byte[] message = BaseEncoding.base16().decode(hexMessage); messages.add(message); idx = messageEndIdx; idx = content.indexOf(FF_16, idx); } LOG.info("Succesfully extracted {} messages", messages.size()); return messages; }
@Override public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException { Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH); final OpenBuilder builder = new OpenBuilder(); builder.setVersion(new ProtocolVersion((short) versionValue)); builder.setProcessingRule(header.isProcessingRule()); builder.setIgnore(header.isIgnore()); final short keepalive = bytes.readUnsignedByte(); builder.setKeepalive(keepalive); final short deadTimer = bytes.readUnsignedByte(); if(keepalive == 0) { builder.setDeadTimer((short) 0); } else { builder.setDeadTimer(deadTimer); } builder.setSessionId(bytes.readUnsignedByte()); final TlvsBuilder tbuilder = new TlvsBuilder(); parseTlvs(tbuilder, bytes.slice()); builder.setTlvs(tbuilder.build()); final Open obj = builder.build(); if (versionValue != PCEP_VERSION) { // TODO: Should we move this check into the negotiator LOG.debug("Unsupported PCEP version {}", versionValue); return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj); } return obj; }
@Override protected byte[] readPceId(final ByteBuf buffer) { return ByteArray.readBytes(buffer, PCE128_ID_F_LENGTH); }
private static List<SourcePorts> parseSourcePort(final ByteBuf nlri) { final List<SourcePorts> ports = new ArrayList<>(); boolean end = false; // we can do this as all fields will be rewritten in the cycle final SourcePortsBuilder builder = new SourcePortsBuilder(); while (!end) { final byte b = nlri.readByte(); final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b); builder.setOp(op); final short length = AbstractOperandParser.parseLength(b); builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length))); end = op.isEndOfList(); ports.add(builder.build()); } return ports; } }
@Override public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException { if (buffer == null) { return null; } final RsvpErrorBuilder builder = new RsvpErrorBuilder(); if (buffer.readableBytes() == V4_RSVP_LENGTH) { builder.setNode(new IpAddress(Ipv4Util.addressForByteBuf(buffer))); } else if (buffer.readableBytes() == V6_RSVP_LENGTH) { builder.setNode(new IpAddress(Ipv6Util.addressForByteBuf(buffer))); } final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH)); builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET))); final short errorCode = (short) UnsignedBytes.toInt(buffer.readByte()); builder.setCode(errorCode); final int errorValue = buffer.readUnsignedShort(); builder.setValue(errorValue); return new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build(); }
private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode linkstate) { final ByteBuf buffer = Unpooled.buffer(); final CLinkstateDestination cLinkstateDestination = LinkstateNlriParser.extractLinkstateDestination(linkstate); SimpleNlriTypeRegistry.getInstance().serializeNlriType(cLinkstateDestination, buffer); return new NodeIdentifierWithPredicates(LinkstateRoute.QNAME, ROUTE_KEY, ByteArray.readAllBytes(buffer)); }