aa4a3ed271cd04b96f4b4b2ff3bfa3372804de32
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing / SrEroSubobjectParserTest.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.ero.subobject.subobject.type.SrEroTypeBuilder;
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.explicit.route.object.ero.SubobjectBuilder;
29
30 public class SrEroSubobjectParserTest {
31
32     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV4_NODEID = {
33         0x05,0x0c,(byte) 0x10,0x00,
34         0x00,0x01,(byte) 0xe2,0x40,
35         0x4A,0x7D,0x2b,0x63,
36     };
37
38     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV6_NODEID = {
39         0x05,0x18,(byte) 0x20,0x00,
40         0x00,0x01,(byte) 0xe2,0x40,
41         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
42         0x00,0x00,0x00,0x00,
43         0x00,0x00,0x00,0x00,
44         0x21,0x1E,0x72,(byte) 0x9C,
45     };
46
47     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV4_ADJ = {
48         0x05,0x10,(byte) 0x30,0x00,
49         0x00,0x01,(byte) 0xe2,0x40,
50         0x4A,0x7D,0x2b,0x63,
51         0x4A,0x7D,0x2b,0x64,
52     };
53
54     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV6_ADJ = {
55         0x05,0x28,(byte) 0x40,0x00,
56         0x00,0x01,(byte) 0xe2,0x40,
57         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
58         0x00,0x00,0x00,0x00,
59         0x00,0x00,0x00,0x00,
60         0x21,0x1E,0x72,(byte) 0x9C,
61         (byte) 0xFE,(byte) 0x80,(byte) 0xCD,0x00,
62         0x00,0x00,0x00,0x00,
63         0x00,0x00,0x00,0x00,
64         0x21,0x1E,0x72,(byte) 0x9D,
65     };
66
67     private static final byte[] SR_ERO_SUBOBJECT_WITH_UNNUMBERED = {
68         0x05,0x18,(byte) 0x50,0x00,
69         0x00,0x01,(byte) 0xe2,0x40,
70         0x00,0x00,0x00,0x01,
71         0x00,0x00,0x00,0x02,
72         0x00,0x00,0x00,0x03,
73         0x00,0x00,0x00,0x04
74     };
75
76     private static final byte[] SR_ERO_SUBOBJECT_WITHOUT_NAI = {
77         0x05,0x08,(byte) 0x10,0x08,
78         0x00,0x01,(byte) 0xe2,0x40
79     };
80
81     private static final byte[] SR_ERO_SUBOBJECT_WITHOUT_SID = {
82         0x05,0x08,(byte) 0x10,0x04,
83         0x4A,0x7D,0x2b,0x63,
84     };
85
86     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV4_NODEID_MFLAG = {
87         0x05,0x0c,(byte) 0x10,0x01,
88         0x07,0x5B,(byte) 0xCD,0x15,
89         0x4A,0x7D,0x2b,0x63,
90     };
91     private static final byte[] SR_ERO_SUBOBJECT_WITH_IPV4_NODEID_MFLAG_AFTER = {
92         0x05,0x0c,(byte) 0x10,0x01,
93         0x07,0x5B,(byte) 0xC0,0x00,
94         0x4A,0x7D,0x2b,0x63,
95     };
96
97     private SimplePCEPExtensionProviderContext ctx;
98     private SegmentRoutingActivator act;
99     private SrEroSubobjectParser parser;
100
101     @Before
102     public void setUp() {
103         this.ctx = new SimplePCEPExtensionProviderContext();
104         this.act = new SegmentRoutingActivator();
105         this.act.start(this.ctx);
106         final boolean isIanaAssignedType = false;
107         this.parser = new SrEroSubobjectParser(isIanaAssignedType);
108     }
109
110     @Test
111     public void testSrEroSubobjectIpv4NodeIdNAI() throws PCEPDeserializerException {
112         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
113         builder.setSidType(SidType.Ipv4NodeId);
114         builder.setSid(123456L);
115         builder.setCFlag(false);
116         builder.setMFlag(false);
117         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
118             .build());
119         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
120
121         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
122             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_IPV4_NODEID, 2)), false));
123         final ByteBuf buffer = Unpooled.buffer();
124         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
125         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_IPV4_NODEID, ByteArray.getAllBytes(buffer));
126     }
127
128     @Test
129     public void testSrEroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
130         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
131         builder.setSidType(SidType.Ipv6NodeId);
132         builder.setSid(123456L);
133         builder.setCFlag(false);
134         builder.setMFlag(false);
135         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
136             new Ipv6AddressNoZone("fe80:cd00::211e:729c"))).build());
137         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
138
139         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
140             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_IPV6_NODEID, 2)), false));
141         final ByteBuf buffer = Unpooled.buffer();
142         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
143         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_IPV6_NODEID, ByteArray.getAllBytes(buffer));
144     }
145
146     @Test
147     public void testSrEroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
148         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
149         builder.setSidType(SidType.Ipv4Adjacency);
150         builder.setSid(123456L);
151         builder.setCFlag(false);
152         builder.setMFlag(false);
153         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
154             new Ipv4AddressNoZone("74.125.43.99")))
155                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.100"))).build());
156         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
157
158         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
159             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_IPV4_ADJ, 2)), false));
160         final ByteBuf buffer = Unpooled.buffer();
161         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
162         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_IPV4_ADJ, ByteArray.getAllBytes(buffer));
163     }
164
165     @Test
166     public void testSrEroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
167         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
168         builder.setSidType(SidType.Ipv6Adjacency);
169         builder.setSid(123456L);
170         builder.setCFlag(false);
171         builder.setMFlag(false);
172         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
173             new Ipv6AddressNoZone("fe80:cd00::211e:729c")))
174                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729d"))).build());
175         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
176
177         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
178             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_IPV6_ADJ, 2)), false));
179         final ByteBuf buffer = Unpooled.buffer();
180         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
181         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_IPV6_ADJ, ByteArray.getAllBytes(buffer));
182     }
183
184     @Test
185     public void testSrEroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
186         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
187         builder.setSidType(SidType.Unnumbered);
188         builder.setSid(123456L);
189         builder.setCFlag(false);
190         builder.setMFlag(false);
191         builder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(1L).setLocalInterfaceId(2L)
192             .setRemoteNodeId(3L).setRemoteInterfaceId(4L).build());
193         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
194
195         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
196             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_UNNUMBERED, 2)), false));
197         final ByteBuf buffer = Unpooled.buffer();
198         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
199         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_UNNUMBERED, ByteArray.getAllBytes(buffer));
200     }
201
202     @Test
203     public void testSrEroSubobjectWithoutNAI() throws PCEPDeserializerException {
204         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
205         builder.setSidType(SidType.Ipv4NodeId);
206         builder.setSid(123456L);
207         builder.setCFlag(false);
208         builder.setMFlag(false);
209         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
210
211         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
212             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITHOUT_NAI, 2)), false));
213         final ByteBuf buffer = Unpooled.buffer();
214         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
215         assertArrayEquals(SR_ERO_SUBOBJECT_WITHOUT_NAI, ByteArray.getAllBytes(buffer));
216     }
217
218     @Test
219     public void testSrEroSubobjectWithoutBody() throws PCEPDeserializerException {
220         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
221         builder.setSidType(SidType.Ipv4NodeId);
222         builder.setCFlag(false);
223         builder.setMFlag(false);
224         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
225             new Ipv4AddressNoZone("74.125.43.99"))).build());
226         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
227
228         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
229             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITHOUT_SID, 2)), false));
230         final ByteBuf buffer = Unpooled.buffer();
231         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
232         assertArrayEquals(SR_ERO_SUBOBJECT_WITHOUT_SID, ByteArray.getAllBytes(buffer));
233     }
234
235     @Test
236     public void testSrEroSubobjectIpv4NodeIdNAIMFlag() throws PCEPDeserializerException {
237         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
238         builder.setCFlag(false);
239         builder.setMFlag(true);
240         builder.setSidType(SidType.Ipv4NodeId);
241         builder.setSid(30140L);
242         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
243             .build());
244         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
245
246         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
247             ByteArray.cutBytes(SR_ERO_SUBOBJECT_WITH_IPV4_NODEID_MFLAG, 2)), false));
248         final ByteBuf buffer = Unpooled.buffer();
249         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
250         assertArrayEquals(SR_ERO_SUBOBJECT_WITH_IPV4_NODEID_MFLAG_AFTER, ByteArray.getAllBytes(buffer));
251     }
252 }