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