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