Further migration of test code from legacy setters
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing / SrObjectParserTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.segment.routing;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
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;
45
46 public class SrObjectParserTest {
47
48     private static final byte[] OPEN_OBJECT_BYTES = {
49         0x01,0x10,0x00,0x10,
50         0x20,0x1e,0x78,0x01,
51         /* sr-capability-tlv */
52         0x00,0x1a,0x00,0x04,
53         0x00,0x00,0x00,0x01};
54
55     private static final byte[] SR_ERO_OBJECT_BYTES = {
56         0x07,0x10,0x00,0x10,
57         /* ero-subobject */
58         0x05,0x0c,(byte) 0x10,0x00,
59         0x00,0x01,(byte)0xe2,0x40,
60         0x4A,0x7D,0x2b,0x63,
61     };
62
63     private TlvRegistry tlvRegistry;
64     private VendorInformationTlvRegistry viTlvRegistry;
65
66     private SimplePCEPExtensionProviderContext ctx;
67     private SegmentRoutingActivator act;
68
69     @Before
70     public void setUp() {
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();
76     }
77
78     @Test
79     public void testOpenObjectWithSpcTlv() throws PCEPDeserializerException {
80         final PcepOpenObjectWithSpcTlvParser parser = new PcepOpenObjectWithSpcTlvParser(this.tlvRegistry,
81             this.viTlvRegistry);
82
83         final OpenBuilder builder = new OpenBuilder()
84                 .setProcessingRule(false)
85                 .setIgnore(false)
86                 .setVersion(new ProtocolVersion(Uint8.ONE))
87                 .setKeepalive(Uint8.valueOf(30))
88                 .setDeadTimer(Uint8.valueOf(120))
89                 .setSessionId(Uint8.ONE);
90
91         final Tlvs1 tlv = new Tlvs1Builder().setSrPceCapability(new SrPceCapabilityBuilder().setMsd(Uint8.ONE).build())
92                 .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());
98
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));
107     }
108
109     @Test
110     public void testSrEroObjectWithSubobjects() throws PCEPDeserializerException {
111         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
112             this.ctx.getEROSubobjectHandlerRegistry());
113
114         final EroBuilder builder = new EroBuilder();
115         builder.setProcessingRule(false);
116         builder.setIgnore(false);
117         final List<Subobject> subobjects = new ArrayList<>();
118
119         final SrEroTypeBuilder srEroSubBuilder = new SrEroTypeBuilder()
120                 .setCFlag(false)
121                 .setMFlag(false)
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())
127                 .setLoose(false);
128         subobjects.add(subobjBuilder.build());
129
130         builder.setSubobject(subobjects);
131
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));
138     }
139
140     @Test
141     public void testSrEroSerializerWithUpdateLspAugmentation() throws PCEPDeserializerException {
142         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(
143             this.ctx.getEROSubobjectHandlerRegistry());
144
145         final EroBuilder builder = new EroBuilder();
146         builder.setProcessingRule(false);
147         builder.setIgnore(false);
148
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()
153                     .setCFlag(false)
154                     .setMFlag(false)
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())
160                 .setLoose(false);
161         builder.setSubobject(Lists.newArrayList(subobjBuilder.build()));
162
163         final ByteBuf buffer = Unpooled.buffer();
164         parser.serializeObject(builder.build(), buffer);
165         assertArrayEquals(SR_ERO_OBJECT_BYTES, ByteArray.getAllBytes(buffer));
166     }
167 }