3169652c24851886e0f32665aea0450ca34ef3a2
[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
9 package org.opendaylight.protocol.pcep.segment.routing;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.SidType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.add.lsp.input.arguments.rro.subobject.subobject.type.SrRroTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.sr.subobject.nai.IpAdjacencyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.sr.subobject.nai.IpNodeIdBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.sr.subobject.nai.UnnumberedAdjacencyBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
30
31 public class SrRroSubobjectParserTest {
32
33     private static final byte[] srRroSubobjectWithIpv4NodeID  = {
34         0x06,0x0c,(byte) 0x10,0x00,
35         0x00,0x01,(byte) 0xe2,0x40,
36         0x4A,0x7D,0x2b,0x63,
37     };
38
39     private static final byte[] srRroSubobjectWithIpv6NodeID  = {
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[] srRroSubobjectWithIpv4Adjacency  = {
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[] srRroSubobjectWithIpv6Adjacency  = {
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[] srRroSubobjectWithUnnumbered  = {
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[] srRroSubobjectWithoutNAI  = {
78         0x06,0x08, (byte) 0x10,0xb,
79         0x1e,0x24,(byte)-32, 0x00,
80     };
81
82     private static final byte[] srRroSubobjectWithoutSID  = {
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         builder.setSidType(SidType.Ipv4NodeId);
105         builder.setSid(123456L);
106         builder.setCFlag(false);
107         builder.setMFlag(false);
108         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99"))).build());
109         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
110
111         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv4NodeID, 2))));
112         final ByteBuf buffer = Unpooled.buffer();
113         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
114         assertArrayEquals(srRroSubobjectWithIpv4NodeID, ByteArray.getAllBytes(buffer));
115     }
116
117     @Test
118     public void testSrRroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
119         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
120         builder.setSidType(SidType.Ipv6NodeId);
121         builder.setCFlag(false);
122         builder.setMFlag(false);
123         builder.setSid(123456L);
124         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729c"))).build());
125         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
126
127         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv6NodeID, 2))));
128         final ByteBuf buffer = Unpooled.buffer();
129         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
130         assertArrayEquals(srRroSubobjectWithIpv6NodeID, ByteArray.getAllBytes(buffer));
131     }
132
133     @Test
134     public void testSrRroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
135         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
136         builder.setSidType(SidType.Ipv4Adjacency);
137         builder.setSid(123456L);
138         builder.setCFlag(false);
139         builder.setMFlag(false);
140         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
141                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.100"))).build());
142         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
143
144         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv4Adjacency, 2))));
145         final ByteBuf buffer = Unpooled.buffer();
146         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
147         assertArrayEquals(srRroSubobjectWithIpv4Adjacency, ByteArray.getAllBytes(buffer));
148     }
149
150     @Test
151     public void testSrRroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
152         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
153         builder.setSidType(SidType.Ipv6Adjacency);
154         builder.setSid(123456L);
155         builder.setCFlag(false);
156         builder.setMFlag(false);
157         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729c")))
158                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729d"))).build());
159         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
160
161         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv6Adjacency, 2))));
162         final ByteBuf buffer = Unpooled.buffer();
163         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
164         assertArrayEquals(srRroSubobjectWithIpv6Adjacency, ByteArray.getAllBytes(buffer));
165     }
166
167     @Test
168     public void testSrRroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
169         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
170         builder.setSidType(SidType.Unnumbered);
171         builder.setSid(123456L);
172         builder.setCFlag(false);
173         builder.setMFlag(false);
174         builder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(1L).setLocalInterfaceId(2L).setRemoteNodeId(3L).setRemoteInterfaceId(4L).build());
175         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
176
177         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithUnnumbered, 2))));
178         final ByteBuf buffer = Unpooled.buffer();
179         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
180         assertArrayEquals(srRroSubobjectWithUnnumbered, ByteArray.getAllBytes(buffer));
181     }
182
183     @Test
184     public void testSrRroSubobjectWithoutNAI() throws PCEPDeserializerException {
185         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
186         builder.setSidType(SidType.Ipv4NodeId);
187         builder.setSid(123470L);
188         builder.setCFlag(true);
189         builder.setMFlag(true);
190         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
191
192         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithoutNAI, 2))));
193         final ByteBuf buffer = Unpooled.buffer();
194         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
195         assertArrayEquals(srRroSubobjectWithoutNAI, ByteArray.getAllBytes(buffer));
196     }
197
198     @Test
199     public void testSrRroSubobjectWithoutBody() throws PCEPDeserializerException {
200         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
201         builder.setSidType(SidType.Ipv4NodeId);
202         builder.setCFlag(false);
203         builder.setMFlag(false);
204         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99"))).build());
205         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
206
207         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithoutSID, 2))));
208         final ByteBuf buffer = Unpooled.buffer();
209         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
210         assertArrayEquals(srRroSubobjectWithoutSID, ByteArray.getAllBytes(buffer));
211     }
212 }