a2d04768f65a7c4469128eb2b0a995b979d6de80
[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
30 public class SrRroSubobjectParserTest {
31
32     private static final byte[] SR_RRO_SUBOBJECT_WITH_IPV4_NODEID  = {
33         0x06,0x0c,(byte) 0x10,0x00,
34         0x00,0x01,(byte) 0xe2,0x40,
35         0x4A,0x7D,0x2b,0x63,
36     };
37
38     private static final byte[] SR_RROR_SUBOBJECT_WITH_IPV6_NODEID  = {
39         0x06,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_RRO_SUBOBJECT_WITH_IPV4_ADJ  = {
48         0x06,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_RRO_SUBOBJECT_WITH_IPV6_ADJ  = {
55         0x06,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_RRO_SUBOBJECT_WIT_UNNUMBERED  = {
68         0x06,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_RRO_SUBOBJECT_WITHOUT_NAI  = {
77         0x06,0x08, (byte) 0x10,0xb,
78         0x1e,0x24,(byte)-32, 0x00,
79     };
80
81     private static final byte[] SR_RRO_SUBOBJECT_WITHOUT_SID  = {
82         0x06,0x08,(byte) 0x10,0x04,
83         0x4A,0x7D,0x2b,0x63,
84     };
85
86     private SimplePCEPExtensionProviderContext ctx;
87     private SegmentRoutingActivator act;
88     private boolean isIanaAssignedType;
89     private SrRroSubobjectParser parser;
90
91     @Before
92     public void setUp() {
93         this.ctx = new SimplePCEPExtensionProviderContext();
94         this.act = new SegmentRoutingActivator();
95         this.act.start(this.ctx);
96         this.isIanaAssignedType = false;
97         this.parser = new SrRroSubobjectParser(this.isIanaAssignedType);
98     }
99
100     @Test
101     public void testSrRroSubobjectIpv4NodeIdNAI() throws PCEPDeserializerException {
102         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
103         builder.setSidType(SidType.Ipv4NodeId);
104         builder.setSid(123456L);
105         builder.setCFlag(false);
106         builder.setMFlag(false);
107         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
108             .build());
109         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
110
111         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
112             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV4_NODEID, 2))));
113         final ByteBuf buffer = Unpooled.buffer();
114         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
115         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV4_NODEID, ByteArray.getAllBytes(buffer));
116     }
117
118     @Test
119     public void testSrRroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
120         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
121         builder.setSidType(SidType.Ipv6NodeId);
122         builder.setCFlag(false);
123         builder.setMFlag(false);
124         builder.setSid(123456L);
125         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(
126             new Ipv6AddressNoZone("fe80:cd00::211e:729c"))).build());
127         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
128
129         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
130             ByteArray.cutBytes(SR_RROR_SUBOBJECT_WITH_IPV6_NODEID, 2))));
131         final ByteBuf buffer = Unpooled.buffer();
132         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
133         assertArrayEquals(SR_RROR_SUBOBJECT_WITH_IPV6_NODEID, ByteArray.getAllBytes(buffer));
134     }
135
136     @Test
137     public void testSrRroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
138         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
139         builder.setSidType(SidType.Ipv4Adjacency);
140         builder.setSid(123456L);
141         builder.setCFlag(false);
142         builder.setMFlag(false);
143         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
144             new Ipv4AddressNoZone("74.125.43.99")))
145                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.100"))).build());
146         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
147
148         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
149             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV4_ADJ, 2))));
150         final ByteBuf buffer = Unpooled.buffer();
151         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
152         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV4_ADJ, ByteArray.getAllBytes(buffer));
153     }
154
155     @Test
156     public void testSrRroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
157         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
158         builder.setSidType(SidType.Ipv6Adjacency);
159         builder.setSid(123456L);
160         builder.setCFlag(false);
161         builder.setMFlag(false);
162         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(
163             new Ipv6AddressNoZone("fe80:cd00::211e:729c")))
164                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729d"))).build());
165         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
166
167         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
168             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITH_IPV6_ADJ, 2))));
169         final ByteBuf buffer = Unpooled.buffer();
170         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
171         assertArrayEquals(SR_RRO_SUBOBJECT_WITH_IPV6_ADJ, ByteArray.getAllBytes(buffer));
172     }
173
174     @Test
175     public void testSrRroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
176         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
177         builder.setSidType(SidType.Unnumbered);
178         builder.setSid(123456L);
179         builder.setCFlag(false);
180         builder.setMFlag(false);
181         builder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(1L).setLocalInterfaceId(2L).setRemoteNodeId(3L)
182             .setRemoteInterfaceId(4L).build());
183         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
184
185         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
186             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WIT_UNNUMBERED, 2))));
187         final ByteBuf buffer = Unpooled.buffer();
188         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
189         assertArrayEquals(SR_RRO_SUBOBJECT_WIT_UNNUMBERED, ByteArray.getAllBytes(buffer));
190     }
191
192     @Test
193     public void testSrRroSubobjectWithoutNAI() throws PCEPDeserializerException {
194         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
195         builder.setSidType(SidType.Ipv4NodeId);
196         builder.setSid(123470L);
197         builder.setCFlag(true);
198         builder.setMFlag(true);
199         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
200
201         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
202             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITHOUT_NAI, 2))));
203         final ByteBuf buffer = Unpooled.buffer();
204         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
205         assertArrayEquals(SR_RRO_SUBOBJECT_WITHOUT_NAI, ByteArray.getAllBytes(buffer));
206     }
207
208     @Test
209     public void testSrRroSubobjectWithoutBody() throws PCEPDeserializerException {
210         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
211         builder.setSidType(SidType.Ipv4NodeId);
212         builder.setCFlag(false);
213         builder.setMFlag(false);
214         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
215             .build());
216         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
217
218         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(
219             ByteArray.cutBytes(SR_RRO_SUBOBJECT_WITHOUT_SID, 2))));
220         final ByteBuf buffer = Unpooled.buffer();
221         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
222         assertArrayEquals(SR_RRO_SUBOBJECT_WITHOUT_SID, ByteArray.getAllBytes(buffer));
223     }
224 }