@Test public void testDecode() throws Exception { assertEquals(AttributeType.SxpNodeId, AttributeFactory.decode(new byte[]{80, 5, 4, 0, 0, 0, 0}).getType()); assertEquals(AttributeType.Capabilities, AttributeFactory.decode(new byte[]{80, 6, 6, 3, 0, 2, 0, 1, 0}).getType()); assertEquals(AttributeType.HoldTime, AttributeFactory.decode(new byte[]{80, 7, 2, 0, 10}).getType()); assertEquals(AttributeType.Ipv4AddPrefix, AttributeFactory.decode(new byte[]{16, 11, 0}).getType()); assertEquals(AttributeType.Ipv6AddPrefix, AttributeFactory.decode(new byte[]{16, 12, 0}).getType()); assertEquals(AttributeType.Ipv4DeletePrefix, AttributeFactory.decode(new byte[]{16, 13, 0}).getType()); assertEquals(AttributeType.Ipv6DeletePrefix, AttributeFactory.decode(new byte[]{16, 14, 0}).getType()); assertEquals(AttributeType.PeerSequence, AttributeFactory.decode(new byte[]{16, 16, 4, 0, 0, 0, 0}).getType()); assertEquals(AttributeType.SourceGroupTag, AttributeFactory.decode(new byte[]{16, 17, 2, 0, 20}).getType()); }
protected static Attribute decode(byte[] array) throws AttributeLengthException, AddressLengthException, UnknownNodeIdException, UnknownPrefixException, TlvNotFoundException, UnknownHostException, AttributeVariantException { Flags flags = getFlags(array[0]); AttributeVariant variant = AttributeVariant.NonCompact; if (flags.isCompact()) { variant = AttributeVariant.Compact; if (flags.isExtendedLength()) { variant = AttributeVariant.CompactExtendedLength; } } AttributeType type = AttributeType.Unspecified; int length = 0; byte[] value = new byte[length]; switch (variant) { case Compact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 1)); if (length < 0 || 255 < length) { throw new AttributeLengthException(); } if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { value = ArraysUtil.readBytes(array, 3, length); length += 3; break; } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case CompactExtendedLength: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 2)); if (length < 256 || 4084 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case NonCompact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 3))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 4, 4)); if (length < 0 || 4080 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 8, length); length += 8; break; default: variant = AttributeVariant.None; } return decode(flags, variant, type, length, value); }
AttributeFactory { protected static Attribute decode(byte[] array) throws AttributeLengthException, AddressLengthException, UnknownNodeIdException, UnknownPrefixException, TlvNotFoundException, UnknownHostException, AttributeVariantException { Flags flags = getFlags(array[0]); AttributeVariant variant = AttributeVariant.NonCompact; if (flags.isCompact()) { variant = AttributeVariant.Compact; if (flags.isExtendedLength()) { variant = AttributeVariant.CompactExtendedLength; } } AttributeType type = AttributeType.Unspecified; int length = 0; byte[] value = new byte[length]; switch (variant) { case Compact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 1)); if (length < 0 || 255 < length) { throw new AttributeLengthException(); } if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { value = ArraysUtil.readBytes(array, 3, length); length += 3; break; } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case CompactExtendedLength: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 2)); if (length < 256 || 4084 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case NonCompact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 3))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 4, 4)); if (length < 0 || 4080 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 8, length); length += 8; break; default: variant = AttributeVariant.None; } return decode(flags, variant, type, length, value); } }
AttributeFactory { protected static Attribute decode(byte[] array) throws AttributeLengthException, AddressLengthException, UnknownNodeIdException, UnknownPrefixException, TlvNotFoundException, UnknownHostException, AttributeVariantException { Flags flags = getFlags(array[0]); AttributeVariant variant = AttributeVariant.NonCompact; if (flags.isCompact()) { variant = AttributeVariant.Compact; if (flags.isExtendedLength()) { variant = AttributeVariant.CompactExtendedLength; } } AttributeType type = AttributeType.Unspecified; int length = 0; byte[] value = new byte[length]; switch (variant) { case Compact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 1)); if (length < 0 || 255 < length) { throw new AttributeLengthException(); } if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { value = ArraysUtil.readBytes(array, 3, length); length += 3; break; } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case CompactExtendedLength: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 2)); if (length < 256 || 4084 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case NonCompact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 3))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 4, 4)); if (length < 0 || 4080 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 8, length); length += 8; break; default: variant = AttributeVariant.None; } return decode(flags, variant, type, length, value); }  }
AttributeFactory { protected static Attribute decode(byte[] array) throws AttributeLengthException, AddressLengthException, UnknownNodeIdException, UnknownPrefixException, TlvNotFoundException, UnknownHostException, AttributeVariantException { Flags flags = getFlags(array[0]); AttributeVariant variant = AttributeVariant.NonCompact; if (flags.isCompact()) { variant = AttributeVariant.Compact; if (flags.isExtendedLength()) { variant = AttributeVariant.CompactExtendedLength; } } AttributeType type = AttributeType.Unspecified; int length = 0; byte[] value = new byte[length]; switch (variant) { case Compact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 1)); if (length < 0 || 255 < length) { throw new AttributeLengthException(); } if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { value = ArraysUtil.readBytes(array, 3, length); length += 3; break; } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case CompactExtendedLength: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 2)); if (length < 256 || 4084 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case NonCompact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 3))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 4, 4)); if (length < 0 || 4080 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 8, length); length += 8; break; default: variant = AttributeVariant.None; } return decode(flags, variant, type, length, value); }  static Attribute createCapabilities(Version version); static Attribute createHoldTime(int holdTimeMin); static Attribute createHoldTime(int holdTimeMin, int holdTimeMax); static Attribute createIpv4AddPrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv4DeletePrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv6AddPrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv6DeletePrefix(List<IpPrefix> prefixes, byte flags); static Attribute createPeerSequence(List<NodeId> nodesIds); static Attribute createSourceGroupTag(int sgt); static Attribute createSxpNodeId(NodeId nodeId); static byte[] encodeCapability(Capabilities capability); }
AttributeFactory { protected static Attribute decode(byte[] array) throws AttributeLengthException, AddressLengthException, UnknownNodeIdException, UnknownPrefixException, TlvNotFoundException, UnknownHostException, AttributeVariantException { Flags flags = getFlags(array[0]); AttributeVariant variant = AttributeVariant.NonCompact; if (flags.isCompact()) { variant = AttributeVariant.Compact; if (flags.isExtendedLength()) { variant = AttributeVariant.CompactExtendedLength; } } AttributeType type = AttributeType.Unspecified; int length = 0; byte[] value = new byte[length]; switch (variant) { case Compact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 1)); if (length < 0 || 255 < length) { throw new AttributeLengthException(); } if (Configuration.SET_COMPOSITION_ATTRIBUTE_COMPACT_NO_RESERVED_FIELDS) { value = ArraysUtil.readBytes(array, 3, length); length += 3; break; } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case CompactExtendedLength: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 1))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 2, 2)); if (length < 256 || 4084 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 4, length); length += 4; break; case NonCompact: type = AttributeType.forValue(ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 1, 3))); length = ArraysUtil.bytes2int(ArraysUtil.readBytes(array, 4, 4)); if (length < 0 || 4080 < length) { throw new AttributeLengthException(); } value = ArraysUtil.readBytes(array, 8, length); length += 8; break; default: variant = AttributeVariant.None; } return decode(flags, variant, type, length, value); }  static Attribute createCapabilities(Version version); static Attribute createHoldTime(int holdTimeMin); static Attribute createHoldTime(int holdTimeMin, int holdTimeMax); static Attribute createIpv4AddPrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv4DeletePrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv6AddPrefix(List<IpPrefix> prefixes, byte flags); static Attribute createIpv6DeletePrefix(List<IpPrefix> prefixes, byte flags); static Attribute createPeerSequence(List<NodeId> nodesIds); static Attribute createSourceGroupTag(int sgt); static Attribute createSxpNodeId(NodeId nodeId); static byte[] encodeCapability(Capabilities capability); static final byte COMPACT; static final byte OPTIONAL_COMPACT; static final byte NONTRANSITIVE_COMPACT; }