+ final byte[] bytesIsis = { (byte)0xC0, 0, 0, 0, 10, 4, (byte)0x89, 0, 4, 1, 2, 3, 4 };
+ final byte[] bytesOspf = { 0, 0, 0, 0, 10, 4, (byte)0x89, 0, 4, 1, 2, 3, 4 };
+ final SrCapabilities capsIsis = new SrCapabilitiesBuilder().setMplsIpv4(Boolean.TRUE).setMplsIpv6(Boolean.TRUE).setSrIpv6(Boolean.FALSE).setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).setRangeSize((long) 10).build();
+ final SrCapabilities capsOspf = new SrCapabilitiesBuilder().setMplsIpv4(Boolean.FALSE).setMplsIpv6(Boolean.FALSE).setSrIpv6(Boolean.FALSE).setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).setRangeSize((long) 10).build();
+ assertEquals(capsIsis, SrNodeAttributesParser.parseSrCapabilities(Unpooled.wrappedBuffer(bytesIsis), ProtocolId.IsisLevel1));
+ assertEquals(capsOspf, SrNodeAttributesParser.parseSrCapabilities(Unpooled.wrappedBuffer(bytesIsis), ProtocolId.Ospf));
+ final ByteBuf encodedIsis = Unpooled.buffer();
+ final ByteBuf encodedOspf = Unpooled.buffer();
+ SrNodeAttributesParser.serializeSrCapabilities(capsIsis, encodedIsis);
+ SrNodeAttributesParser.serializeSrCapabilities(capsOspf, encodedOspf);
+ assertArrayEquals(bytesIsis, ByteArray.readAllBytes(encodedIsis));
+ assertArrayEquals(bytesOspf, ByteArray.readAllBytes(encodedOspf));
+ }
+
+ // tools.ietf.org/html/draft-gredler-idr-bgp-ls-segment-routing-ext-00#section-2.3.1
+ @Test
+ public void testSrPrefix() {
+ final byte[] bytes = { (byte)0xA0, 0, 0, 0, 1, 2, 3, 4 };
+ final byte[] bytesOspf = { (byte)0x20, 0, 0, 0, 1, 2, 3, 4 };
+ final SrPrefix prefixIsis = new SrPrefixBuilder()
+ .setFlags(ISIS_PREFIX_FLAGS)
+ .setAlgorithm(Algorithm.ShortestPathFirst).setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).build();
+ final SrPrefix prefixOspf = new SrPrefixBuilder()
+ .setFlags(OSPF_PREFIX_FLAGS)
+ .setAlgorithm(Algorithm.ShortestPathFirst).setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).build();
+ assertEquals(prefixIsis, SrPrefixAttributesParser.parseSrPrefix(Unpooled.wrappedBuffer(bytes), ProtocolId.IsisLevel1));
+ assertEquals(prefixOspf, SrPrefixAttributesParser.parseSrPrefix(Unpooled.wrappedBuffer(bytes), ProtocolId.Ospf));
+ final ByteBuf serializedPrefix = Unpooled.buffer();
+ final ByteBuf serializedPrefixOspf = Unpooled.buffer();
+ SrPrefixAttributesParser.serializeSrPrefix(prefixIsis, serializedPrefix);
+ SrPrefixAttributesParser.serializeSrPrefix(prefixOspf, serializedPrefixOspf);
+ assertArrayEquals(bytes, ByteArray.readAllBytes(serializedPrefix));
+ assertArrayEquals(bytesOspf, ByteArray.readAllBytes(serializedPrefixOspf));
+ }
+
+ // https://tools.ietf.org/html/draft-gredler-idr-bgp-ls-segment-routing-ext-02#section-2.3.2
+ @Test
+ public void testIpv6SrPrefix() {
+ final byte[] bytes = { 0, 0, 0};
+ final Ipv6SrPrefix prefix = new Ipv6SrPrefixBuilder().setAlgorithm(Algorithm.ShortestPathFirst).build();
+ assertEquals(prefix, Ipv6SrPrefixAttributesParser.parseSrIpv6Prefix(Unpooled.wrappedBuffer(bytes)));
+ final ByteBuf serializedPrefix = Unpooled.buffer();
+ Ipv6SrPrefixAttributesParser.serializeIpv6SrPrefix(prefix, serializedPrefix);
+ assertArrayEquals(bytes, ByteArray.readAllBytes(serializedPrefix));
+ }
+
+ // tools.ietf.org/html/draft-gredler-idr-bgp-ls-segment-routing-ext-00#section-2.3.2
+ @Test
+ public void testRangeTlv() {
+ final byte[] tested = {
+ 0, 0, 0, 5,
+ 4, (byte)0x89, 0, 4, 1, 2, 3, 4, // sid
+ 4, (byte)0x86, 0, 7, (byte)0xac, 1, 0, 0, 1, 2, 0, // prefix + mpls label
+ 4, (byte)0x88, 0, 0x58, 5, 0x48, 0, 0, // binding sid
+ // binding sub-tlvs
+ 4, (byte)0x86, 0, 8, (byte)0xa0, 1, 0, 0, 1, 2, 3, 4, // prefix
+ 4, (byte)0x89, 0, 4, 1, 2, 3, 4, // sid
+ 4, (byte)0x8a, 0, 4, 0, 0, 0, 6, // ero metric
+ 4, (byte)0x8b, 0, 8, 0, 0, 0, 0, 9, 8, 7, 6, // IPv4 ERO
+ 4, (byte)0x8d, 0, 0x0c, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, // Unnumbered Interface ID ERO Sub-TLV
+ 4, (byte)0x8e, 0, 8, 0, 0, 0, 0, 3, 4, 5, 6, // IPv4 ERO backup
+ 4, (byte)0x90, 0, 0x0c, 0, 0, 0, 0, 3, 3, 3, 3, 4, 4, 4, 4, // Unnumbered Interface ID ERO Sub-TLV Backup
+ };
+ final SrRange parsedRange = RangeTlvParser.parseSrRange(Unpooled.wrappedBuffer(tested), ProtocolId.IsisLevel1);
+
+ final List<SubTlvs> rangeSubTlvs = new ArrayList<>();
+ addSubTlvs(rangeSubTlvs);
+ final SrRange expected = new SrRangeBuilder().setInterArea(Boolean.FALSE).setRangeSize(5).setSubTlvs(rangeSubTlvs).build();
+
+ assertEquals(expected, parsedRange);
+
+ final ByteBuf serializedRange = Unpooled.buffer();
+ RangeTlvParser.serializeSrRange(parsedRange, serializedRange);
+ assertArrayEquals(tested, ByteArray.getAllBytes(serializedRange));
+ }
+
+ private void addSubTlvs(final List<SubTlvs> rangeSubTlvs) {
+ rangeSubTlvs.add(new SubTlvsBuilder().setRangeSubTlv(
+ new SidLabelTlvCaseBuilder()
+ .setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).build()).build());
+ rangeSubTlvs.add(new SubTlvsBuilder().setRangeSubTlv(
+ new PrefixSidTlvCaseBuilder()
+ .setFlags(ISIS_PREFIX_FLAGS)
+ .setAlgorithm(Algorithm.StrictShortestPathFirst)
+ .setSidLabelIndex(new LocalLabelCaseBuilder().setLocalLabel(new MplsLabel(66048L)).build()).build()).build());
+ final List<BindingSubTlvs> bindingSubTlvs = new ArrayList<>();
+ addBindingSubTlvs(bindingSubTlvs);
+ rangeSubTlvs.add(new SubTlvsBuilder().setRangeSubTlv(
+ new BindingSidTlvCaseBuilder()
+ .setWeight(new Weight((short) 5))
+ .setFlags(BINDING_FLAGS)
+ .setBindingSubTlvs(bindingSubTlvs).build()).build());
+ }
+
+ private void addBindingSubTlvs(final List<BindingSubTlvs> bindingSubTlvs) {
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new PrefixSidCaseBuilder()
+ .setFlags(ISIS_PREFIX_FLAGS)
+ .setAlgorithm(Algorithm.StrictShortestPathFirst)
+ .setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build())
+ .build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new SidLabelCaseBuilder().setSidLabelIndex(new SidCaseBuilder().setSid(16909060L).build()).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new EroMetricCaseBuilder().setEroMetric(new TeMetric(6L)).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new Ipv4EroCaseBuilder().setLoose(Boolean.FALSE).setAddress(new Ipv4Address("9.8.7.6")).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new UnnumberedInterfaceIdEroCaseBuilder().setLoose(Boolean.FALSE).setRouterId(16843009L).setInterfaceId(33686018L).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new Ipv4EroBackupCaseBuilder().setLoose(Boolean.FALSE).setAddress(new Ipv4Address("3.4.5.6")).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new UnnumberedInterfaceIdBackupEroCaseBuilder().setLoose(Boolean.FALSE).setRouterId(50529027L).setInterfaceId(67372036L).build()).build());
+ }
+
+ @Test
+ public void testRangeTlvIpv6() {
+ final byte[] tested = Bytes.concat(
+ new byte[] {
+ 0, 0, 0, 5,
+ 4, (byte)0x88, 0, 0x34, 5, 0x48, 0, 0, // binding sid
+ // binding sub-tlvs
+ 4, (byte)0x8c, 0, 0x14, 0, 0, 0, 0 }, IPV6_A_BYTES, // IPv6 ERO
+ new byte[] { 4, (byte)0x8f, 0, 0x14, 0, 0, 0, 0 }, IPV6_B_BYTES // IPv6 ERO backup
+ );
+ final SrRange parsedRange = RangeTlvParser.parseSrRange(Unpooled.wrappedBuffer(tested), ProtocolId.IsisLevel1);
+
+ final List<SubTlvs> rangeSubTlvs = new ArrayList<>();
+ final List<BindingSubTlvs> bindingSubTlvs = new ArrayList<>();
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new Ipv6EroCaseBuilder().setLoose(Boolean.FALSE).setAddress(new Ipv6Address(IPV6_A)).build()).build());
+ bindingSubTlvs.add(new BindingSubTlvsBuilder().setBindingSubTlv(
+ new Ipv6EroBackupCaseBuilder().setLoose(Boolean.FALSE).setAddress(new Ipv6Address(IPV6_B)).build()).build());
+ rangeSubTlvs.add(new SubTlvsBuilder().setRangeSubTlv(
+ new BindingSidTlvCaseBuilder()
+ .setWeight(new Weight((short) 5))
+ .setFlags(BINDING_FLAGS)
+ .setBindingSubTlvs(bindingSubTlvs).build()).build());
+ final SrRange expected = new SrRangeBuilder().setInterArea(Boolean.FALSE).setRangeSize(5).setSubTlvs(rangeSubTlvs).build();
+
+ assertEquals(expected, parsedRange);
+
+ final ByteBuf serializedRange = Unpooled.buffer();
+ RangeTlvParser.serializeSrRange(parsedRange, serializedRange);
+ assertArrayEquals(tested, ByteArray.getAllBytes(serializedRange));
+ }
+
+ // tools.ietf.org/html/draft-gredler-idr-bgp-ls-segment-routing-ext-00#section-2.2.1
+ @Test
+ public void testSrAdjId() {
+ final byte[] tested = { (byte)0x60, 10, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+ final byte[] testedOspf = { (byte)0xc0, 10, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+ final byte[] sidLabel = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+ final SrAdjIds srAdjId = new SrAdjIdsBuilder()
+ .setFlags(ISIS_ADJ_FLAGS)
+ .setWeight(new Weight((short) 10))
+ .setSidLabelIndex(new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(Unpooled.copiedBuffer(sidLabel))).build()).build();
+ final SrAdjIds ospfAdj = new SrAdjIdsBuilder()
+ .setFlags(OSPF_ADJ_FLAGS)
+ .setWeight(new Weight((short) 10))
+ .setSidLabelIndex(new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(Unpooled.copiedBuffer(sidLabel))).build()).build();
+
+ assertEquals(srAdjId, new SrAdjIdsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(Unpooled.wrappedBuffer(tested), ProtocolId.IsisLevel1)).build());
+ assertEquals(ospfAdj, new SrAdjIdsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(Unpooled.wrappedBuffer(testedOspf), ProtocolId.Ospf)).build());
+ final ByteBuf serializedData = SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(srAdjId);
+ final ByteBuf serializedOspf = SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(ospfAdj);
+ assertArrayEquals(tested, ByteArray.readAllBytes(serializedData));
+ assertArrayEquals(testedOspf, ByteArray.readAllBytes(serializedOspf));
+ }
+
+ @Test
+ public void testSrLanAdjIdOspf() {
+ final byte[] tested = { (byte)0x60, 10, 0, 0, 1, 2, 3, 4, 0, 0x5d, (byte)0xc0 };
+ final SrLanAdjIds srLanAdjId = new SrLanAdjIdsBuilder()
+ .setFlags(OSPF_LAN_ADJ_FLAGS)
+ .setWeight(new Weight((short)10))
+ .setNeighborId(new Ipv4Address("1.2.3.4"))
+ .setSidLabelIndex(new LocalLabelCaseBuilder().setLocalLabel(new MplsLabel(24000L)).build()).build();
+ assertEquals(srLanAdjId, SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(Unpooled.wrappedBuffer(tested), ProtocolId.Ospf));
+ final ByteBuf serializedData = SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(srLanAdjId);
+ assertArrayEquals(tested, ByteArray.readAllBytes(serializedData));
+ }
+
+ @Test
+ public void testSrLanAdjIdIsis() {
+ final byte[] tested = { (byte)0x60, 10, 0, 0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+ final byte[] sidLabel = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+ final byte[] systemId = { 1, 2, 3, 4, 5, 6 };
+ final SrLanAdjIds srLanAdjId = new SrLanAdjIdsBuilder()
+ .setFlags(ISIS_ADJ_FLAGS)
+ .setWeight(new Weight((short)10))
+ .setIsoSystemId(new IsoSystemIdentifier(systemId))
+ .setSidLabelIndex(new Ipv6AddressCaseBuilder().setIpv6Address(Ipv6Util.addressForByteBuf(Unpooled.copiedBuffer(sidLabel))).build()).build();
+ assertEquals(srLanAdjId, SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(Unpooled.wrappedBuffer(tested), ProtocolId.IsisLevel1));
+ final ByteBuf serializedData = SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(srLanAdjId);
+ assertArrayEquals(tested, ByteArray.readAllBytes(serializedData));
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testSrLinkAttributesPrivateConstructor() throws Throwable {
+ final Constructor<SrLinkAttributesParser> c = SrLinkAttributesParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testSrNodeAttributesPrivateConstructor() throws Throwable {
+ final Constructor<SrNodeAttributesParser> c = SrNodeAttributesParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testSrPrefixAttributesPrivateConstructor() throws Throwable {
+ final Constructor<SrPrefixAttributesParser> c = SrPrefixAttributesParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testIpv6SrPrefixAttributesPrivateConstructor() throws Throwable {
+ final Constructor<Ipv6SrPrefixAttributesParser> c = Ipv6SrPrefixAttributesParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testBindingParserPrivateConstructor() throws Throwable {
+ final Constructor<BindingSidLabelParser> c = BindingSidLabelParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testRangeTlvParserPrivateConstructor() throws Throwable {
+ final Constructor<RangeTlvParser> c = RangeTlvParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }
+ }
+
+ @Test(expected=UnsupportedOperationException.class)
+ public void testSidLabelIndexParserPrivateConstructor() throws Throwable {
+ final Constructor<SidLabelIndexParser> c = SidLabelIndexParser.class.getDeclaredConstructor();
+ c.setAccessible(true);
+ try {
+ c.newInstance();
+ } catch (final InvocationTargetException e) {
+ throw e.getCause();
+ }