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 java.util.Arrays;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
14 import org.opendaylight.protocol.pcep.subobject.EROWavebandSwitchingLabelSubobject;
15 import org.opendaylight.protocol.util.ByteArray;
16 import com.google.common.primitives.UnsignedInts;
18 public class EROWavebandSwitchingLabelSubobjectParser implements EROLabelParser {
20 public static int WAVEB_F_LENGTH = 4;
21 public static int START_F_LENGTH = 4;
22 public static int END_F_LENGTH = 4;
24 public static int WAVEB_F_OFFSET = 0;
25 public static int START_F_OFFSET = WAVEB_F_OFFSET + WAVEB_F_LENGTH;
26 public static int END_F_OFFSET = START_F_OFFSET + START_F_LENGTH;
28 public static int CONTENT_LENGTH = END_F_OFFSET + END_F_LENGTH;
31 public EROLabelSubobject parse(byte[] cutBytes, boolean upStream, boolean loose) throws PCEPDeserializerException {
32 if (cutBytes == null || cutBytes.length == 0)
33 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
35 if (cutBytes.length != CONTENT_LENGTH)
36 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + cutBytes.length + "; Expected: " + CONTENT_LENGTH + ".");
38 return new EROWavebandSwitchingLabelSubobject(UnsignedInts.toLong(ByteArray.bytesToInt(Arrays.copyOfRange(cutBytes, WAVEB_F_OFFSET, START_F_OFFSET))),
39 UnsignedInts.toLong(ByteArray.bytesToInt(Arrays.copyOfRange(cutBytes, START_F_OFFSET, END_F_OFFSET))), UnsignedInts.toLong(ByteArray
40 .bytesToInt(Arrays.copyOfRange(cutBytes, END_F_OFFSET, CONTENT_LENGTH))), upStream, loose);
44 public byte[] put(EROLabelSubobject objToSerialize) {
45 if (!(objToSerialize instanceof EROWavebandSwitchingLabelSubobject))
46 throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed " + objToSerialize.getClass()
47 + ". Needed EROWavebandSwitchingLabelSubobject.");
48 final byte[] retBytes = new byte[CONTENT_LENGTH];
50 final EROWavebandSwitchingLabelSubobject obj = (EROWavebandSwitchingLabelSubobject) objToSerialize;
52 System.arraycopy(ByteArray.intToBytes((int) obj.getWavebandId()), 0, retBytes, WAVEB_F_OFFSET, WAVEB_F_LENGTH);
53 System.arraycopy(ByteArray.intToBytes((int) obj.getStartLabel()), 0, retBytes, START_F_OFFSET, START_F_LENGTH);
54 System.arraycopy(ByteArray.intToBytes((int) obj.getEndLabel()), 0, retBytes, END_F_OFFSET, END_F_LENGTH);