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.subobject;
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.LabelParser;
12 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CLabel;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.WavebandSwitchingLabel;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelBuilder;
19 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
21 public static final int CTYPE = 3;
23 private static int WAVEB_F_LENGTH = 4;
24 private static int START_F_LENGTH = 4;
25 private static int END_F_LENGTH = 4;
27 private static int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
30 public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
31 if (buffer == null || buffer.length == 0)
32 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
34 if (buffer.length != CONTENT_LENGTH)
35 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
36 + CONTENT_LENGTH + ".");
38 final WavebandSwitchingLabelBuilder builder = new WavebandSwitchingLabelBuilder();
41 builder.setWavebandId(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, WAVEB_F_LENGTH)));
42 byteOffset += WAVEB_F_LENGTH;
43 builder.setStartLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, START_F_LENGTH)));
44 byteOffset += START_F_LENGTH;
45 builder.setEndLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, END_F_LENGTH)));
47 return builder.build();
51 public byte[] serializeLabel(final CLabel subobject) {
52 if (!(subobject instanceof WavebandSwitchingLabel))
53 throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
54 + ". Needed WavebandSwitchingLabel.");
55 final byte[] retBytes = new byte[CONTENT_LENGTH];
57 final WavebandSwitchingLabel obj = (WavebandSwitchingLabel) subobject;
59 System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue()), 0, retBytes, 0, WAVEB_F_LENGTH);
60 System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue()), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
61 System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue()), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH, END_F_LENGTH);
67 public int getType() {