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