2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.protocol.pcep.parser.subobject.GeneralizedLabelParser;
18 import org.opendaylight.protocol.pcep.parser.subobject.Type1LabelParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.WavebandSwitchingLabelParser;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint32;
30 public class LabelSubobjectParserTest {
32 private static final byte[] GENERALIZED_LABEL_BYTES = {
33 (byte) 0x80, 0x02, 0x00, 0x04, 0x12, 0x00, 0x25, (byte) 0xFF
36 private static final byte[] TYPE_ONE_LABEL_BYTES = { (byte) 0x81, 0x01, 0x12, 0x00, 0x25, (byte) 0xFF };
38 private static final byte[] WAVEBAND_LABEL_BYTES = {
39 0x01, 0x03, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, (byte) 0x99, (byte) 0x99, 0x00, 0x00, 0x11, 0x11
43 public void testGeneralizedLabel() throws PCEPDeserializerException {
44 final GeneralizedLabelParser parser = new GeneralizedLabelParser();
45 final GeneralizedLabelBuilder iBuilder = new GeneralizedLabelBuilder();
46 iBuilder.setGeneralizedLabel(ByteArray.cutBytes(GENERALIZED_LABEL_BYTES, 2));
47 final GeneralizedLabelCaseBuilder builder =
48 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(iBuilder.build());
50 builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(GENERALIZED_LABEL_BYTES, 2))));
51 final ByteBuf buff = Unpooled.buffer();
52 parser.serializeLabel(true, false, builder.build(), buff);
53 assertArrayEquals(GENERALIZED_LABEL_BYTES, ByteArray.getAllBytes(buff));
56 parser.parseLabel(null);
58 } catch (final IllegalArgumentException e) {
59 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
63 parser.parseLabel(Unpooled.EMPTY_BUFFER);
65 } catch (final IllegalArgumentException e) {
66 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
71 public void testWavebandLabel() throws PCEPDeserializerException {
72 final WavebandSwitchingLabelParser parser = new WavebandSwitchingLabelParser();
73 final WavebandSwitchingLabelBuilder iBuilder = new WavebandSwitchingLabelBuilder()
74 .setWavebandId(Uint32.valueOf(0x1234L))
75 .setStartLabel(Uint32.valueOf(0x9999L))
76 .setEndLabel(Uint32.valueOf(0x1111L));
77 final WavebandSwitchingLabelCaseBuilder builder =
78 new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(iBuilder.build());
80 builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(WAVEBAND_LABEL_BYTES, 2))));
81 final ByteBuf buff = Unpooled.buffer();
82 parser.serializeLabel(false, true, builder.build(), buff);
83 assertArrayEquals(WAVEBAND_LABEL_BYTES, ByteArray.getAllBytes(buff));
86 parser.parseLabel(null);
88 } catch (final IllegalArgumentException e) {
89 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
92 parser.parseLabel(Unpooled.EMPTY_BUFFER);
94 } catch (final IllegalArgumentException e) {
95 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
100 public void testTypeOneLabel() throws PCEPDeserializerException {
101 final Type1LabelParser parser = new Type1LabelParser();
102 final Type1LabelBuilder iBuilder = new Type1LabelBuilder().setType1Label(Uint32.valueOf(0x120025ffL));
103 final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder().setType1Label(iBuilder.build());
104 assertEquals(builder.build(),
105 parser.parseLabel(Unpooled.wrappedBuffer(ByteArray.cutBytes(TYPE_ONE_LABEL_BYTES, 2))));
106 final ByteBuf buff = Unpooled.buffer();
107 parser.serializeLabel(true, true, builder.build(), buff);
108 assertArrayEquals(TYPE_ONE_LABEL_BYTES, ByteArray.getAllBytes(buff));
111 parser.parseLabel(null);
113 } catch (final IllegalArgumentException e) {
114 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
117 parser.parseLabel(Unpooled.EMPTY_BUFFER);
119 } catch (final IllegalArgumentException e) {
120 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());