Further migration of test code from legacy setters
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing / SrRroSubobjectParserTest.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 io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.SidType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.add.lsp.input.arguments.rro.subobject.subobject.type.SrRroTypeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpAdjacencyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.IpNodeIdBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev181109.sr.subobject.nai.UnnumberedAdjacencyBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30
31 public class SrRroSubobjectParserTest {
32
33     private static final byte[] SR_RRO_SUBOBJECT_WITH_IPV4_NODEID  = {
34         0x06,0x0c,(byte) 0x10,0x00,
35         0x00,0x01,(byte) 0xe2,0x40,
36         0x4A,0x7D,0x2b,0x63,
37     };
38
39     private static final byte[] SR_RROR_SUBOBJECT_WITH_IPV6_NODEID  = {
40         0x06,0x18,(byte) 0x20,0x00,
41         0x00,0x01,(byte) 0xe2,0x40,
42         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
43         0x00,0x00,0x00,0x00,
44         0x00,0x00,0x00,0x00,
45         0x21,0x1E,0x72,(byte) 0x9C,
46     };
47
48     private static final byte[] SR_RRO_SUBOBJECT_WITH_IPV4_ADJ  = {
49         0x06,0x10,(byte) 0x30,0x00,
50         0x00,0x01,(byte) 0xe2,0x40,
51         0x4A,0x7D,0x2b,0x63,
52         0x4A,0x7D,0x2b,0x64,
53     };
54
55     private static final byte[] SR_RRO_SUBOBJECT_WITH_IPV6_ADJ  = {
56         0x06,0x28,(byte) 0x40,0x00,
57         0x00,0x01,(byte) 0xe2,0x40,
58         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
59         0x00,0x00,0x00,0x00,
60         0x00,0x00,0x00,0x00,
61         0x21,0x1E,0x72,(byte) 0x9C,
62         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
63         0x00,0x00,0x00,0x00,
64         0x00,0x00,0x00,0x00,
65         0x21,0x1E,0x72,(byte) 0x9D,
66     };
67
68     private static final byte[] SR_RRO_SUBOBJECT_WIT_UNNUMBERED  = {
69         0x06,0x18,(byte) 0x50,0x00,
70         0x00,0x01,(byte) 0xe2,0x40,
71         0x00,0x00,0x00,0x01,
72         0x00,0x00,0x00,0x02,
73         0x00,0x00,0x00,0x03,
74         0x00,0x00,0x00,0x04
75     };
76
77     private static final byte[] SR_RRO_SUBOBJECT_WITHOUT_NAI  = {
78         0x06,0x08, (byte) 0x10,0xb,
79         0x1e,0x24,(byte)-32, 0x00,
80     };
81
82     private static final byte[] SR_RRO_SUBOBJECT_WITHOUT_SID  = {
83         0x06,0x08,(byte) 0x10,0x04,
84         0x4A,0x7D,0x2b,0x63,
85     };
86
87     private SimplePCEPExtensionProviderContext ctx;
88     private SegmentRoutingActivator act;
89     private boolean isIanaAssignedType;
90     private SrRroSubobjectParser parser;
91
92     @Before
93     public void setUp() {
94         this.ctx = new SimplePCEPExtensionProviderContext();
95         this.act = new SegmentRoutingActivator();
96         this.act.start(this.ctx);
97         this.isIanaAssignedType = false;
98         this.parser = new SrRroSubobjectParser(this.isIanaAssignedType);
99     }
100
101     @Test
102     public void testSrRroSubobjectIpv4NodeIdNAI() throws PCEPDeserializerException {
103         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
104                 .setSidType(SidType.Ipv4NodeId)
105                 .setSid(Uint32.valueOf(123456))
106                 .setCFlag(false)
107                 .setMFlag(false)
108                 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
109                     .build());
110         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
111
112         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
113             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV4_NODEID, 2))));
114         final ByteBuf buffer = Unpooled.buffer();
115         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
116         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV4_NODEID, ByteArray.getAllBytes(buffer));
117     }
118
119     @Test
120     public void testSrRroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
121         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
122                 .setSidType(SidType.Ipv6NodeId)
123                 .setCFlag(false)
124                 .setMFlag(false)
125                 .setSid(Uint32.valueOf(123456))
126                 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
127                     new Ipv6AddressNoZone("fe80:cd00::211e:729c"))).build());
128         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
129
130         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
131             ByteArray.cutBytes(SR_RROR_SUBOBJECT_WITH_IPV6_NODEID, 2))));
132         final ByteBuf buffer = Unpooled.buffer();
133         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
134         assertArrayEquals(SR_RROR_SUBOBJECT_WITH_IPV6_NODEID, ByteArray.getAllBytes(buffer));
135     }
136
137     @Test
138     public void testSrRroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
139         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
140                 .setSidType(SidType.Ipv4Adjacency)
141                 .setSid(Uint32.valueOf(123456))
142                 .setCFlag(false)
143                 .setMFlag(false)
144                 .setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
145                     new Ipv4AddressNoZone("74.125.43.99")))
146                     .setRemoteIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.100"))).build());
147         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
148
149         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
150             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV4_ADJ, 2))));
151         final ByteBuf buffer = Unpooled.buffer();
152         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
153         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV4_ADJ, ByteArray.getAllBytes(buffer));
154     }
155
156     @Test
157     public void testSrRroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
158         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
159                 .setSidType(SidType.Ipv6Adjacency)
160                 .setSid(Uint32.valueOf(123456))
161                 .setCFlag(false)
162                 .setMFlag(false)
163                 .setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
164                     new Ipv6AddressNoZone("fe80:cd00::211e:729c")))
165                     .setRemoteIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729d"))).build());
166         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
167
168         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
169             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV6_ADJ, 2))));
170         final ByteBuf buffer = Unpooled.buffer();
171         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
172         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV6_ADJ, ByteArray.getAllBytes(buffer));
173     }
174
175     @Test
176     public void testSrRroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
177         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
178                 .setSidType(SidType.Unnumbered)
179                 .setSid(Uint32.valueOf(123456))
180                 .setCFlag(false)
181                 .setMFlag(false)
182                 .setNai(new UnnumberedAdjacencyBuilder()
183                     .setLocalNodeId(Uint32.ONE).setLocalInterfaceId(Uint32.valueOf(2))
184                     .setRemoteNodeId(Uint32.valueOf(3)).setRemoteInterfaceId(Uint32.valueOf(4))
185                     .build());
186         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
187
188         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
189             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WIT_UNNUMBERED, 2))));
190         final ByteBuf buffer = Unpooled.buffer();
191         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
192         assertArrayEquals(SR_RRO_SUBOBJECT_WIT_UNNUMBERED, ByteArray.getAllBytes(buffer));
193     }
194
195     @Test
196     public void testSrRroSubobjectWithoutNAI() throws PCEPDeserializerException {
197         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
198                 .setSidType(SidType.Ipv4NodeId)
199                 .setSid(Uint32.valueOf(123470))
200                 .setCFlag(true)
201                 .setMFlag(true);
202         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
203
204         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
205             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITHOUT_NAI, 2))));
206         final ByteBuf buffer = Unpooled.buffer();
207         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
208         assertArrayEquals(SR_RRO_SUBOBJECT_WITHOUT_NAI, ByteArray.getAllBytes(buffer));
209     }
210
211     @Test
212     public void testSrRroSubobjectWithoutBody() throws PCEPDeserializerException {
213         final SrRroTypeBuilder builder = new SrRroTypeBuilder()
214                 .setSidType(SidType.Ipv4NodeId)
215                 .setCFlag(false)
216                 .setMFlag(false)
217                 .setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
218                     .build());
219         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
220
221         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
222             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITHOUT_SID, 2))));
223         final ByteBuf buffer = Unpooled.buffer();
224         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
225         assertArrayEquals(SR_RRO_SUBOBJECT_WITHOUT_SID, ByteArray.getAllBytes(buffer));
226     }
227 }