Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPXROSubobjectParserTest.java
1 /*
2  * Copyright (c) 2013 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.impl;
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.Test;
16 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.XROIpv4PrefixSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.XROIpv6PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.XROPathKey128SubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.XROPathKey32SubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.XROSRLGSubobjectParser;
22 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
23 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKeyBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
44
45 public class PCEPXROSubobjectParserTest {
46
47     private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48         (byte) 0x16, (byte) 0x00 };
49     private static final byte[] ip6PrefixBytes = { (byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
50         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
51         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01 };
52     private static final byte[] srlgBytes = { (byte) 0xa2, (byte) 0x07, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x02 };
53     private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34,
54         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
55     private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
56     private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
57         (byte) 0x50, (byte) 0x00 };
58     private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
59         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
60         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
61
62     @Test
63     public void testXROIp4PrefixSubobject() throws PCEPDeserializerException {
64         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
65         final SubobjectBuilder subs = new SubobjectBuilder();
66         subs.setMandatory(false);
67         subs.setAttribute(Attribute.Interface);
68         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
69                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
70         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false));
71         final ByteBuf buff = Unpooled.buffer();
72         parser.serializeSubobject(subs.build(), buff);
73         assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
74     }
75
76     @Test
77     public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
78         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
79         final SubobjectBuilder subs = new SubobjectBuilder();
80         subs.setMandatory(true);
81         subs.setAttribute(Attribute.Node);
82         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
83                 new IpPrefixBuilder().setIpPrefix(
84                         new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
85                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
86                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
87         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true));
88         final ByteBuf buff = Unpooled.buffer();
89         parser.serializeSubobject(subs.build(), buff);
90         assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
91     }
92
93     @Test
94     public void testXROSrlgSubobject() throws PCEPDeserializerException {
95         final XROSRLGSubobjectParser parser = new XROSRLGSubobjectParser();
96         final SubobjectBuilder subs = new SubobjectBuilder();
97         subs.setMandatory(true);
98         subs.setAttribute(Attribute.Srlg);
99         subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
100         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true));
101         final ByteBuf buff = Unpooled.buffer();
102         parser.serializeSubobject(subs.build(), buff);
103         assertArrayEquals(srlgBytes, ByteArray.getAllBytes(buff));
104     }
105
106     @Test
107     public void testXROUnnumberedSubobject() throws PCEPDeserializerException {
108         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
109         final SubobjectBuilder subs = new SubobjectBuilder();
110         subs.setMandatory(true);
111         subs.setAttribute(Attribute.Node);
112         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
113                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
114         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
115         final ByteBuf buff = Unpooled.buffer();
116         parser.serializeSubobject(subs.build(), buff);
117         assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
118     }
119
120     @Test
121     public void testXROAsNumberSubobject() throws PCEPDeserializerException {
122         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
123         final SubobjectBuilder subs = new SubobjectBuilder();
124         subs.setMandatory(true);
125         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
126         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
127         final ByteBuf buff = Unpooled.buffer();
128         parser.serializeSubobject(subs.build(), buff);
129         assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
130     }
131
132     @Test
133     public void testXROPathKey32Subobject() throws PCEPDeserializerException {
134         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
135         final SubobjectBuilder subs = new SubobjectBuilder();
136         subs.setMandatory(true);
137         final PathKeyBuilder pBuilder = new PathKeyBuilder();
138         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
139         pBuilder.setPathKey(new PathKey(4660));
140         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
141         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
142         final ByteBuf buff = Unpooled.buffer();
143         parser.serializeSubobject(subs.build(), buff);
144         assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
145     }
146
147     @Test
148     public void testXROPathKey128Subobject() throws PCEPDeserializerException {
149         final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
150         final SubobjectBuilder subs = new SubobjectBuilder();
151         subs.setMandatory(true);
152         final PathKeyBuilder pBuilder = new PathKeyBuilder();
153         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
154             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
155         pBuilder.setPathKey(new PathKey(4660));
156         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
157         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
158         final ByteBuf buff = Unpooled.buffer();
159         parser.serializeSubobject(subs.build(), buff);
160         assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
161     }
162 }