BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROWavebandSwitchingLabelSubobjectParser.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 java.util.Arrays;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.subobject.RROLabelSubobject;
14 import org.opendaylight.protocol.pcep.subobject.RROWavebandSwitchingLabelSubobject;
15 import org.opendaylight.protocol.util.ByteArray;
16 import com.google.common.primitives.UnsignedInts;
17
18 public class RROWavebandSwitchingLabelSubobjectParser implements RROLabelParser {
19
20     public static int WAVEB_F_LENGTH = 4;
21     public static int START_F_LENGTH = 4;
22     public static int END_F_LENGTH = 4;
23
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;
27
28     public static int CONTENT_LENGTH = END_F_OFFSET + END_F_LENGTH;
29
30     @Override
31     public RROLabelSubobject parse(byte[] cutBytes, boolean upStream) throws PCEPDeserializerException {
32         if (cutBytes == null || cutBytes.length == 0)
33             throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
34
35         if (cutBytes.length != CONTENT_LENGTH)
36             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + cutBytes.length + "; Expected: " + CONTENT_LENGTH + ".");
37
38         return new RROWavebandSwitchingLabelSubobject(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);
41     }
42
43     @Override
44     public byte[] put(RROLabelSubobject objToSerialize) {
45         if (!(objToSerialize instanceof RROWavebandSwitchingLabelSubobject))
46             throw new IllegalArgumentException("Unknown RROLabelSubobject instance. Passed " + objToSerialize.getClass()
47                     + ". Needed RROWavebandSwitchingLabelSubobject.");
48         final byte[] retBytes = new byte[CONTENT_LENGTH];
49
50         final RROWavebandSwitchingLabelSubobject obj = (RROWavebandSwitchingLabelSubobject) objToSerialize;
51
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);
55
56         return retBytes;
57     }
58
59 }