Merge "Bug-2225 Update PCEP segment routing according draft version 00."
[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.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SidType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.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.rev150112.sr.subobject.nai.IpAdjacencyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.subobject.nai.IpNodeIdBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.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,0x02,
79         0x00,0x01,(byte) 0xe2,0x40,
80     };
81
82     private static final byte[] srRroSubobjectWithoutSID  = {
83         0x06,0x08,(byte) 0x10,0x01,
84         0x4A,0x7D,0x2b,0x63,
85     };
86
87     private SimplePCEPExtensionProviderContext ctx;
88     private SegmentRoutingActivator act;
89
90     @Before
91     public void setUp() {
92         this.ctx = new SimplePCEPExtensionProviderContext();
93         this.act = new SegmentRoutingActivator();
94         this.act.start(this.ctx);
95     }
96
97     @Test
98     public void testSrRroSubobjectIpv4NodeIdNAI() throws PCEPDeserializerException {
99         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
100         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
101         builder.setSidType(SidType.Ipv4NodeId);
102         builder.setSid(123456L);
103         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(new Ipv4Address("74.125.43.99"))).build());
104         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
105
106         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv4NodeID, 2))));
107         final ByteBuf buffer = Unpooled.buffer();
108         parser.serializeSubobject(subobjBuilder.build(), buffer);
109         assertArrayEquals(srRroSubobjectWithIpv4NodeID, ByteArray.getAllBytes(buffer));
110     }
111
112     @Test
113     public void testSrRroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
114         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
115         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
116         builder.setSidType(SidType.Ipv6NodeId);
117         builder.setSid(123456L);
118         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(new Ipv6Address("fe80:cd00::211e:729c"))).build());
119         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
120
121         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv6NodeID, 2))));
122         final ByteBuf buffer = Unpooled.buffer();
123         parser.serializeSubobject(subobjBuilder.build(), buffer);
124         assertArrayEquals(srRroSubobjectWithIpv6NodeID, ByteArray.getAllBytes(buffer));
125     }
126
127     @Test
128     public void testSrRroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
129         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
130         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
131         builder.setSidType(SidType.Ipv4Adjacency);
132         builder.setSid(123456L);
133         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddress(new Ipv4Address("74.125.43.99")))
134                 .setRemoteIpAddress(new IpAddress(new Ipv4Address("74.125.43.100"))).build());
135         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
136
137         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv4Adjacency, 2))));
138         final ByteBuf buffer = Unpooled.buffer();
139         parser.serializeSubobject(subobjBuilder.build(), buffer);
140         assertArrayEquals(srRroSubobjectWithIpv4Adjacency, ByteArray.getAllBytes(buffer));
141     }
142
143     @Test
144     public void testSrRroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
145         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
146         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
147         builder.setSidType(SidType.Ipv6Adjacency);
148         builder.setSid(123456L);
149         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddress(new Ipv6Address("fe80:cd00::211e:729c")))
150                 .setRemoteIpAddress(new IpAddress(new Ipv6Address("fe80:cd00::211e:729d"))).build());
151         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
152
153         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithIpv6Adjacency, 2))));
154         final ByteBuf buffer = Unpooled.buffer();
155         parser.serializeSubobject(subobjBuilder.build(), buffer);
156         assertArrayEquals(srRroSubobjectWithIpv6Adjacency, ByteArray.getAllBytes(buffer));
157     }
158
159     @Test
160     public void testSrRroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
161         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
162         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
163         builder.setSidType(SidType.Unnumbered);
164         builder.setSid(123456L);
165         builder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(1L).setLocalInterfaceId(2L).setRemoteNodeId(3L).setRemoteInterfaceId(4L).build());
166         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
167
168         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithUnnumbered, 2))));
169         final ByteBuf buffer = Unpooled.buffer();
170         parser.serializeSubobject(subobjBuilder.build(), buffer);
171         assertArrayEquals(srRroSubobjectWithUnnumbered, ByteArray.getAllBytes(buffer));
172     }
173
174     @Test
175     public void testSrRroSubobjectWithoutNAI() throws PCEPDeserializerException {
176         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
177         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
178         builder.setSidType(SidType.Ipv4NodeId);
179         builder.setSid(123456L);
180         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
181
182         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithoutNAI, 2))));
183         final ByteBuf buffer = Unpooled.buffer();
184         parser.serializeSubobject(subobjBuilder.build(), buffer);
185         assertArrayEquals(srRroSubobjectWithoutNAI, ByteArray.getAllBytes(buffer));
186     }
187
188     @Test
189     public void testSrRroSubobjectWithoutBody() throws PCEPDeserializerException {
190         final SrRroSubobjectParser parser = new SrRroSubobjectParser();
191         final SrRroTypeBuilder builder = new SrRroTypeBuilder();
192         builder.setSidType(SidType.Ipv4NodeId);
193         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(new Ipv4Address("74.125.43.99"))).build());
194         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build());
195
196         assertEquals(subobjBuilder.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srRroSubobjectWithoutSID, 2))));
197         final ByteBuf buffer = Unpooled.buffer();
198         parser.serializeSubobject(subobjBuilder.build(), buffer);
199         assertArrayEquals(srRroSubobjectWithoutSID, ByteArray.getAllBytes(buffer));
200     }
201 }