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.subobject.RROLabelSubobject;
12 import org.opendaylight.protocol.pcep.subobject.RROType1LabelSubobject;
13 import org.opendaylight.protocol.util.ByteArray;
14 import com.google.common.primitives.UnsignedInts;
16 public class RROType1LabelSubobjectParser implements RROLabelParser {
18 public static final int LABEL_LENGTH = 4;
21 public RROLabelSubobject parse(byte[] cutBytes, boolean upStream) throws PCEPDeserializerException {
22 if (cutBytes == null || cutBytes.length == 0)
23 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
24 if (cutBytes.length != LABEL_LENGTH)
25 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + cutBytes.length + "; Expected: " + LABEL_LENGTH + ".");
27 return new RROType1LabelSubobject(UnsignedInts.toLong(ByteArray.bytesToInt(cutBytes)), upStream);
31 public byte[] put(RROLabelSubobject objToSerialize) {
32 if (!(objToSerialize instanceof RROType1LabelSubobject))
33 throw new IllegalArgumentException("Unknown RROLabelSubobject instance. Passed " + objToSerialize.getClass() + ". Needed RROType1LabelSubobject.");
35 return ByteArray.intToBytes((int) ((RROType1LabelSubobject) objToSerialize).getLabel());