/** * Convert a colon-separated hex string to byte array. * * @param values colon-separated hex string to be converted, * e.g. "0f:ca:fe:de:ad:be:ef" * @return converted byte array * @throws NumberFormatException if input hex string cannot be parsed */ public static byte[] fromHexString(final String values) { return fromHexString(values, ":"); }
/** * 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); }
@Override public Criterion decodeCriterion(ObjectNode json) { JsonNode oduSignalId = nullIsIllegal(json.get(CriterionCodec.ODU_SIGNAL_ID), CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE); int tributaryPortNumber = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_PORT_NUMBER), CriterionCodec.TRIBUTARY_PORT_NUMBER + MISSING_MEMBER_MESSAGE).asInt(); int tributarySlotLen = nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_LEN), CriterionCodec.TRIBUTARY_SLOT_LEN + MISSING_MEMBER_MESSAGE).asInt(); byte[] tributarySlotBitmap = HexString.fromHexString( nullIsIllegal(oduSignalId.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP), CriterionCodec.TRIBUTARY_SLOT_BITMAP + MISSING_MEMBER_MESSAGE).asText()); return Criteria.matchOduSignalId( OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap)); } }
/** * 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; }
/** * Decodes a Layer 1 instruction. * * @return instruction object decoded from the JSON * @throws IllegalArgumentException if the JSON is invalid */ private Instruction decodeL1() { String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE), InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText(); if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) { int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER), InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); int tributarySlotLen = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_LEN), InstructionCodec.TRIBUTARY_SLOT_LEN + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt(); byte[] tributarySlotBitmap = null; tributarySlotBitmap = HexString.fromHexString( nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_BITMAP), InstructionCodec.TRIBUTARY_SLOT_BITMAP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText()); return Instructions.modL1OduSignalId(OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap)); } throw new IllegalArgumentException("L1 Instruction subtype " + subType + " is not supported"); }
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; }
CriterionCodec.PI_MATCH_FIELD_ID + MISSING_MEMBER_MESSAGE).asText()), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_VALUE), CriterionCodec.PI_MATCH_VALUE + MISSING_MEMBER_MESSAGE).asText(), null)); CriterionCodec.PI_MATCH_FIELD_ID + MISSING_MEMBER_MESSAGE).asText()), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_VALUE), CriterionCodec.PI_MATCH_VALUE + MISSING_MEMBER_MESSAGE).asText(), null), CriterionCodec.PI_MATCH_FIELD_ID + MISSING_MEMBER_MESSAGE).asText()), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_VALUE), CriterionCodec.PI_MATCH_VALUE + MISSING_MEMBER_MESSAGE).asText(), null), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_MASK), CriterionCodec.PI_MATCH_MASK + MISSING_MEMBER_MESSAGE).asText(), null)); CriterionCodec.PI_MATCH_FIELD_ID + MISSING_MEMBER_MESSAGE).asText()), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_LOW_VALUE), CriterionCodec.PI_MATCH_LOW_VALUE + MISSING_MEMBER_MESSAGE).asText(), null), HexString.fromHexString(nullIsIllegal(node.get(CriterionCodec.PI_MATCH_HIGH_VALUE), CriterionCodec.PI_MATCH_HIGH_VALUE + MISSING_MEMBER_MESSAGE).asText(), null)
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; }
throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x"); GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString( groupKeyStr.split("0x")[1], ""));
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; }
piActionParam = new PiActionParam(piActionParamId, ImmutableByteSequence.copyFrom( HexString.fromHexString(param.getValue(), null))); builder.withParameter(piActionParam);
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));