BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROType1LabelSubobjectParser.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.subobject;
9
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;
15
16 public class RROType1LabelSubobjectParser implements RROLabelParser {
17
18     public static final int LABEL_LENGTH = 4;
19
20     @Override
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 + ".");
26
27         return new RROType1LabelSubobject(UnsignedInts.toLong(ByteArray.bytesToInt(cutBytes)), upStream);
28     }
29
30     @Override
31     public byte[] put(RROLabelSubobject objToSerialize) {
32         if (!(objToSerialize instanceof RROType1LabelSubobject))
33             throw new IllegalArgumentException("Unknown RROLabelSubobject instance. Passed " + objToSerialize.getClass() + ". Needed RROType1LabelSubobject.");
34
35         return ByteArray.intToBytes((int) ((RROType1LabelSubobject) objToSerialize).getLabel());
36     }
37
38 }