/** * Convert a byte array to a colon-separated hex string. * * @param bytes byte array to be converted * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef", * or "(null)" if given byte array is null */ public static String toHexString(final byte[] bytes) { return toHexString(bytes, ":"); }
/** * Convert a long number to colon-separated hex string. * Prepend zero padding until 8 bytes. * * @param val long number to be converted * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" */ public static String toHexString(final long val) { return toHexString(val, 8); }
/** * Returns a hexadecimal representation of this byte sequence, e.g. * 0xbeef. The length of the returned string is not representative of the * length of the byte sequence, as all padding zeros are removed. * * @return hexadecimal representation */ @Override public String toString() { final String hexValue = HexString .toHexString(value.array(), "") // Remove leading zeros, but leave one if string is all zeros. .replaceFirst("^0+(?!$)", ""); return "0x" + hexValue; }
private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) { ObjectNode matchTernaryNode = context.mapper().createObjectNode(); matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id()); matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase()); matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE, HexString.toHexString(ternaryFieldMatch.value().asArray(), null)); matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK, HexString.toHexString(ternaryFieldMatch.mask().asArray(), null)); return matchTernaryNode; }
private ObjectNode parsePiMatchRange(PiRangeFieldMatch rangeFieldMatch) { ObjectNode matchRangeNode = context.mapper().createObjectNode(); matchRangeNode.put(CriterionCodec.PI_MATCH_FIELD_ID, rangeFieldMatch.fieldId().id()); matchRangeNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.RANGE.name().toLowerCase()); matchRangeNode.put(CriterionCodec.PI_MATCH_HIGH_VALUE, HexString.toHexString(rangeFieldMatch.highValue().asArray(), null)); matchRangeNode.put(CriterionCodec.PI_MATCH_LOW_VALUE, HexString.toHexString(rangeFieldMatch.lowValue().asArray(), null)); return matchRangeNode; }
private ObjectNode parsePiMatchExact(PiExactFieldMatch exactFieldMatch) { ObjectNode matchExactNode = context.mapper().createObjectNode(); matchExactNode.put(CriterionCodec.PI_MATCH_FIELD_ID, exactFieldMatch.fieldId().id()); matchExactNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.EXACT.name().toLowerCase()); matchExactNode.put(CriterionCodec.PI_MATCH_VALUE, HexString.toHexString(exactFieldMatch.value().asArray(), null)); return matchExactNode; }
private ObjectNode parsePiMatchLpm(PiLpmFieldMatch lpmFieldMatch) { ObjectNode matchLpmNode = context.mapper().createObjectNode(); matchLpmNode.put(CriterionCodec.PI_MATCH_FIELD_ID, lpmFieldMatch.fieldId().id()); matchLpmNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.LPM.name().toLowerCase()); matchLpmNode.put(CriterionCodec.PI_MATCH_VALUE, HexString.toHexString(lpmFieldMatch.value().asArray(), null)); matchLpmNode.put(CriterionCodec.PI_MATCH_PREFIX, lpmFieldMatch.prefixLength()); return matchLpmNode; }
@Override public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { OduSignalId oduSignalId = ((OduSignalIdCriterion) criterion).oduSignalId(); ObjectNode child = root.putObject(CriterionCodec.ODU_SIGNAL_ID); child.put(CriterionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); child.put(CriterionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); child.put(CriterionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap())); return root; } }
/** * Encode an L1 modification instruction. * * @param result json node that the instruction attributes are added to */ private void encodeL1(ObjectNode result) { L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction; result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name()); switch (l1Instruction.subtype()) { case ODU_SIGID: final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction = (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction; OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId(); ObjectNode child = result.putObject("oduSignalId"); child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber()); child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength()); child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap())); break; default: log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype()); break; } }
for (PiActionParam actionParam : piAction.parameters()) { jsonActionParams.put(actionParam.id().id(), HexString.toHexString(actionParam.value().asArray(), null));