fa9fb16cf520cb5bdd5e2ec79940092ee080d905
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPEROSubobjectParserTest.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 java.util.List;
14
15 import org.junit.Test;
16 import org.opendaylight.protocol.concepts.Ipv6Util;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv4PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv6PrefixSubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
22 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey128SubobjectParser;
23 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey32SubobjectParser;
24 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
25 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
26 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder;
35 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;
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.LabelCaseBuilder;
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.label._case.LabelBuilder;
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 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
48
49 import com.google.common.collect.Lists;
50
51 public class PCEPEROSubobjectParserTest {
52         private static final byte[] ip4PrefixBytes = { (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
53                         (byte) 0x16, (byte) 0x00 };
54         private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55                         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
56                         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00 };
57         private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
58         private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34,
59                         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
60         private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
61                         (byte) 0x50, (byte) 0x00 };
62         private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
63                         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00,
64                         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
65         private static final byte[] labelBytes = { (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25,
66                         (byte) 0xFF };
67         private static final byte[] exrsBytes = { (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
68
69         @Test
70         public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
71                 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
72                 final SubobjectBuilder subs = new SubobjectBuilder();
73                 subs.setLoose(true);
74                 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
75                                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
76                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip4PrefixBytes, 2), true));
77                 assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build()));
78         }
79
80         @Test
81         public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
82                 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
83                 final SubobjectBuilder subs = new SubobjectBuilder();
84                 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
85                                 new IpPrefixBuilder().setIpPrefix(
86                                                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
87                                                                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
88                                                                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
89                 subs.setLoose(false);
90                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(ip6PrefixBytes, 2), false));
91                 assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build()));
92         }
93
94         @Test
95         public void testEROAsNumberSubobject() throws PCEPDeserializerException {
96                 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
97                 final SubobjectBuilder subs = new SubobjectBuilder();
98                 subs.setLoose(true);
99                 subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
100                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(asNumberBytes, 2), true));
101                 assertArrayEquals(asNumberBytes, parser.serializeSubobject(subs.build()));
102         }
103
104         @Test
105         public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
106                 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
107                 final SubobjectBuilder subs = new SubobjectBuilder();
108                 subs.setLoose(true);
109                 subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
110                                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
111                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(unnumberedBytes, 2), true));
112                 assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build()));
113         }
114
115         @Test
116         public void testEROPathKey32Subobject() throws PCEPDeserializerException {
117                 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
118                 final SubobjectBuilder subs = new SubobjectBuilder();
119                 subs.setLoose(true);
120                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
121                 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
122                 pBuilder.setPathKey(new PathKey(4660));
123                 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
124                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey32Bytes, 2), true));
125                 assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build()));
126         }
127
128         @Test
129         public void testEROPathKey128Subobject() throws PCEPDeserializerException {
130                 final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
131                 final SubobjectBuilder subs = new SubobjectBuilder();
132                 subs.setLoose(true);
133                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
134                 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
135                                 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
136                 pBuilder.setPathKey(new PathKey(4660));
137                 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
138                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(pathKey128Bytes, 2), true));
139                 assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build()));
140         }
141
142         @Test
143         public void testEROLabelSubobject() throws Exception {
144                 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(ServiceLoaderPCEPExtensionProviderContext.create().getLabelHandlerRegistry());
145                 final SubobjectBuilder subs = new SubobjectBuilder();
146                 subs.setLoose(true);
147                 subs.setSubobjectType(new LabelCaseBuilder().setLabel(
148                                 new LabelBuilder().setUniDirectional(true).setLabelType(
149                                                 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
150                                                                 new GeneralizedLabelBuilder().setGeneralizedLabel(
151                                                                                 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
152                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(labelBytes, 2), true));
153                 assertArrayEquals(labelBytes, parser.serializeSubobject(subs.build()));
154         }
155
156         @Test
157         public void testEROEXRSSubobject() throws Exception {
158                 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(ServiceLoaderPCEPExtensionProviderContext.create().getXROSubobjectHandlerRegistry());
159                 final SubobjectBuilder subs = new SubobjectBuilder();
160                 subs.setLoose(true);
161                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> list = Lists.newArrayList();
162                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
163                 builder.setMandatory(true);
164                 builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
165                 list.add(builder.build());
166                 subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
167                 assertEquals(subs.build(), parser.parseSubobject(ByteArray.cutBytes(exrsBytes, 2), true));
168                 assertArrayEquals(exrsBytes, parser.serializeSubobject(subs.build()));
169         }
170 }