2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.segment.routing;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.parser.object.PCEPExplicitRouteObjectParser;
21 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
24 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
25 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Tlvs3;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Tlvs3Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.SidType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.Tlvs1;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.Tlvs1Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.add.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.pce.capability.tlv.SrPceCapabilityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpNodeIdBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ProtocolVersion;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
43 import org.opendaylight.yangtools.yang.common.Uint32;
44 import org.opendaylight.yangtools.yang.common.Uint8;
46 public class SrObjectParserTest {
48 private static final byte[] OPEN_OBJECT_BYTES = {
51 /* sr-capability-tlv */
55 private static final byte[] SR_ERO_OBJECT_BYTES = {
58 0x05,0x0c,(byte) 0x10,0x00,
59 0x00,0x01,(byte)0xe2,0x40,
63 private TlvRegistry tlvRegistry;
64 private VendorInformationTlvRegistry viTlvRegistry;
66 private SimplePCEPExtensionProviderContext ctx;
67 private SegmentRoutingActivator act;
71 this.ctx = new SimplePCEPExtensionProviderContext();
72 this.act = new SegmentRoutingActivator();
73 this.act.start(this.ctx);
74 this.tlvRegistry = this.ctx.getTlvHandlerRegistry();
75 this.viTlvRegistry = this.ctx.getVendorInformationTlvRegistry();
79 public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
80 final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(this.tlvRegistry,
83 final OpenBuilder builder = new OpenBuilder()
84 .setProcessingRule(false)
86 .setVersion(new ProtocolVersion(Uint8.ONE))
87 .setKeepalive(Uint8.valueOf(30))
88 .setDeadTimer(Uint8.valueOf(120))
89 .setSessionId(Uint8.ONE);
91 final Tlvs1 tlv = new Tlvs1Builder().setSrPceCapability(new SrPceCapabilityBuilder().setMsd(Uint8.ONE).build())
93 builder.setTlvs(new TlvsBuilder()
94 .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful
95 .rev181109.Tlvs1.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep
96 .ietf.stateful.rev181109.Tlvs1Builder().build()).addAugmentation(Tlvs1.class, tlv)
97 .addAugmentation(Tlvs3.class, new Tlvs3Builder().build()).build());
99 final ByteBuf result = Unpooled.wrappedBuffer(OPEN_OBJECT_BYTES);
100 assertEquals(builder.build(),
101 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
102 final ByteBuf buffer = Unpooled.buffer();
103 parser.serializeObject(builder.build(), buffer);
104 parser.serializeTlvs(null, Unpooled.EMPTY_BUFFER);
105 parser.serializeTlvs(new TlvsBuilder().build(), Unpooled.EMPTY_BUFFER);
106 assertArrayEquals(OPEN_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
110 public void testSrEroObjectWithSubobjects() throws PCEPDeserializerException {
111 final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
112 this.ctx.getEROSubobjectHandlerRegistry());
114 final EroBuilder builder = new EroBuilder();
115 builder.setProcessingRule(false);
116 builder.setIgnore(false);
117 final List<Subobject> subobjects = new ArrayList<>();
119 final SrEroTypeBuilder srEroSubBuilder = new SrEroTypeBuilder()
122 .setSidType(SidType.Ipv4NodeId)
123 .setSid(Uint32.valueOf(123456))
124 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
125 new Ipv4AddressNoZone("74.125.43.99"))).build());
126 final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build())
128 subobjects.add(subobjBuilder.build());
130 builder.setSubobject(subobjects);
132 final ByteBuf result = Unpooled.wrappedBuffer(SR_ERO_OBJECT_BYTES);
133 assertEquals(builder.build(),
134 parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
135 final ByteBuf buffer = Unpooled.buffer();
136 parser.serializeObject(builder.build(), buffer);
137 assertArrayEquals(SR_ERO_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
141 public void testSrEroSerializerWithUpdateLspAugmentation() throws PCEPDeserializerException {
142 final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
143 this.ctx.getEROSubobjectHandlerRegistry());
145 final EroBuilder builder = new EroBuilder();
146 builder.setProcessingRule(false);
147 builder.setIgnore(false);
149 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.update.lsp
150 .input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder srEroSubBuilder =
151 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109
152 .update.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder()
155 .setSidType(SidType.Ipv4NodeId)
156 .setSid(Uint32.valueOf(123456))
157 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
158 new Ipv4AddressNoZone("74.125.43.99"))).build());
159 final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroSubBuilder.build())
161 builder.setSubobject(Lists.newArrayList(subobjBuilder.build()));
163 final ByteBuf buffer = Unpooled.buffer();
164 parser.serializeObject(builder.build(), buffer);
165 assertArrayEquals(SR_ERO_OBJECT_BYTES, ByteArray.getAllBytes(buffer));