Switch PCEP to no-zone ip addresses
[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
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.ero.subobject.subobject.type.SrEroTypeBuilder;
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.explicit.route.object.ero.SubobjectBuilder;
30
31 public class SrEroSubobjectParserTest {
32
33     private static final byte[] srEroSubobjectWithIpv4NodeID  = {
34         0x05,0x0c,(byte) 0x10,0x00,
35         0x00,0x01,(byte) 0xe2,0x40,
36         0x4A,0x7D,0x2b,0x63,
37     };
38
39     private static final byte[] srEroSubobjectWithIpv6NodeID  = {
40         0x05,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[] srEroSubobjectWithIpv4Adjacency  = {
49         0x05,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[] srEroSubobjectWithIpv6Adjacency  = {
56         0x05,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[] srEroSubobjectWithUnnumbered  = {
69         0x05,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[] srEroSubobjectWithoutNAI  = {
78         0x05,0x08,(byte) 0x10,0x08,
79         0x00,0x01,(byte) 0xe2,0x40
80     };
81
82     private static final byte[] srEroSubobjectWithoutSID  = {
83         0x05,0x08,(byte) 0x10,0x04,
84         0x4A,0x7D,0x2b,0x63,
85     };
86
87     private static final byte[] srEroSubobjectWithIpv4NodeIDMFlag  = {
88         0x05,0x0c,(byte) 0x10,0x01,
89         0x07,0x5B,(byte) 0xCD,0x15,
90         0x4A,0x7D,0x2b,0x63,
91     };
92     private static final byte[] srEroSubobjectWithIpv4NodeIDMFlagAfter  = {
93         0x05,0x0c,(byte) 0x10,0x01,
94         0x07,0x5B,(byte) 0xC0,0x00,
95         0x4A,0x7D,0x2b,0x63,
96     };
97
98     private SimplePCEPExtensionProviderContext ctx;
99     private SegmentRoutingActivator act;
100     private SrEroSubobjectParser parser;
101
102     @Before
103     public void setUp() {
104         this.ctx = new SimplePCEPExtensionProviderContext();
105         this.act = new SegmentRoutingActivator();
106         this.act.start(this.ctx);
107         final boolean isIanaAssignedType = false;
108         this.parser = new SrEroSubobjectParser(isIanaAssignedType);
109     }
110
111     @Test
112     public void testSrEroSubobjectIpv4NodeIdNAI() throws PCEPDeserializerException {
113         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
114         builder.setSidType(SidType.Ipv4NodeId);
115         builder.setSid(123456L);
116         builder.setCFlag(false);
117         builder.setMFlag(false);
118         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99"))).build());
119         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
120
121         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithIpv4NodeID, 2)), false));
122         final ByteBuf buffer = Unpooled.buffer();
123         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
124         assertArrayEquals(srEroSubobjectWithIpv4NodeID, ByteArray.getAllBytes(buffer));
125     }
126
127     @Test
128     public void testSrEroSubobjectIpv6NodeIdNAI() throws PCEPDeserializerException {
129         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
130         builder.setSidType(SidType.Ipv6NodeId);
131         builder.setSid(123456L);
132         builder.setCFlag(false);
133         builder.setMFlag(false);
134         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729c"))).build());
135         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
136
137         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithIpv6NodeID, 2)), false));
138         final ByteBuf buffer = Unpooled.buffer();
139         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
140         assertArrayEquals(srEroSubobjectWithIpv6NodeID, ByteArray.getAllBytes(buffer));
141     }
142
143     @Test
144     public void testSrEroSubobjectIpv4AdjacencyNAI() throws PCEPDeserializerException {
145         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
146         builder.setSidType(SidType.Ipv4Adjacency);
147         builder.setSid(123456L);
148         builder.setCFlag(false);
149         builder.setMFlag(false);
150         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99")))
151                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.100"))).build());
152         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
153
154         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithIpv4Adjacency, 2)), false));
155         final ByteBuf buffer = Unpooled.buffer();
156         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
157         assertArrayEquals(srEroSubobjectWithIpv4Adjacency, ByteArray.getAllBytes(buffer));
158     }
159
160     @Test
161     public void testSrEroSubobjectIpv6AdjacencyNAI() throws PCEPDeserializerException {
162         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
163         builder.setSidType(SidType.Ipv6Adjacency);
164         builder.setSid(123456L);
165         builder.setCFlag(false);
166         builder.setMFlag(false);
167         builder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729c")))
168                 .setRemoteIpAddress(new IpAddressNoZone(new Ipv6AddressNoZone("fe80:cd00::211e:729d"))).build());
169         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
170
171         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithIpv6Adjacency, 2)), false));
172         final ByteBuf buffer = Unpooled.buffer();
173         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
174         assertArrayEquals(srEroSubobjectWithIpv6Adjacency, ByteArray.getAllBytes(buffer));
175     }
176
177     @Test
178     public void testSrEroSubobjectUnnumberedNAI() throws PCEPDeserializerException {
179         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
180         builder.setSidType(SidType.Unnumbered);
181         builder.setSid(123456L);
182         builder.setCFlag(false);
183         builder.setMFlag(false);
184         builder.setNai(new UnnumberedAdjacencyBuilder().setLocalNodeId(1L).setLocalInterfaceId(2L).setRemoteNodeId(3L).setRemoteInterfaceId(4L).build());
185         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
186
187         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithUnnumbered, 2)), false));
188         final ByteBuf buffer = Unpooled.buffer();
189         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
190         assertArrayEquals(srEroSubobjectWithUnnumbered, ByteArray.getAllBytes(buffer));
191     }
192
193     @Test
194     public void testSrEroSubobjectWithoutNAI() throws PCEPDeserializerException {
195         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
196         builder.setSidType(SidType.Ipv4NodeId);
197         builder.setSid(123456L);
198         builder.setCFlag(false);
199         builder.setMFlag(false);
200         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
201
202         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithoutNAI, 2)), false));
203         final ByteBuf buffer = Unpooled.buffer();
204         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
205         assertArrayEquals(srEroSubobjectWithoutNAI, ByteArray.getAllBytes(buffer));
206     }
207
208     @Test
209     public void testSrEroSubobjectWithoutBody() throws PCEPDeserializerException {
210         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
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"))).build());
215         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
216
217         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithoutSID, 2)), false));
218         final ByteBuf buffer = Unpooled.buffer();
219         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
220         assertArrayEquals(srEroSubobjectWithoutSID, ByteArray.getAllBytes(buffer));
221     }
222
223     @Test
224     public void testSrEroSubobjectIpv4NodeIdNAIMFlag() throws PCEPDeserializerException {
225         final SrEroTypeBuilder builder = new SrEroTypeBuilder();
226         builder.setCFlag(false);
227         builder.setMFlag(true);
228         builder.setSidType(SidType.Ipv4NodeId);
229         builder.setSid(30140L);
230         builder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone("74.125.43.99"))).build());
231         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(builder.build()).setLoose(false);
232
233         assertEquals(subobjBuilder.build(), this.parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srEroSubobjectWithIpv4NodeIDMFlag, 2)), false));
234         final ByteBuf buffer = Unpooled.buffer();
235         this.parser.serializeSubobject(subobjBuilder.build(), buffer);
236         assertArrayEquals(srEroSubobjectWithIpv4NodeIDMFlagAfter, ByteArray.getAllBytes(buffer));
237     }
238 }