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.tlv;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.NoSuchElementException;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPOFCodes;
17 import org.opendaylight.protocol.pcep.impl.PCEPOFCodesMapping;
18 import org.opendaylight.protocol.pcep.tlv.OFListTlv;
19 import org.opendaylight.protocol.util.ByteArray;
22 * Parser for {@link org.opendaylight.protocol.pcep.tlv.OFListTlv OFListTlv}
24 public class OFListTlvParser {
26 private static final int OF_CODE_ELEMENT_LENGTH = 2;
28 public static OFListTlv parse(byte[] valueBytes) throws PCEPDeserializerException {
29 if (valueBytes == null || valueBytes.length == 0)
30 throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
31 if (valueBytes.length % OF_CODE_ELEMENT_LENGTH != 0)
32 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + ".");
34 final List<PCEPOFCodes> ofCodes = new ArrayList<PCEPOFCodes>();
35 for (int i = 0; i < valueBytes.length; i += OF_CODE_ELEMENT_LENGTH) {
37 ofCodes.add(PCEPOFCodesMapping.getInstance().getFromCodeIdentifier(
38 ByteArray.bytesToShort(Arrays.copyOfRange(valueBytes, i, i + OF_CODE_ELEMENT_LENGTH)) & 0xFFFF));
39 } catch (final NoSuchElementException nsee) {
40 throw new PCEPDeserializerException(nsee, "Unknown OF Code inside OF Code list Tlv.");
44 return new OFListTlv(ofCodes);
47 public static byte[] put(OFListTlv objToSerialize) {
48 if (objToSerialize == null)
49 throw new IllegalArgumentException("OFListTlv is mandatory.");
51 final List<PCEPOFCodes> ofCodes = objToSerialize.getOfCodes();
52 final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
54 final int size = ofCodes.size();
55 for (int i = 0; i < size; i++) {
56 ByteArray.copyWhole(ByteArray.shortToBytes((short) PCEPOFCodesMapping.getInstance().getFromOFCodesEnum(ofCodes.get(i))), retBytes, i
57 * OF_CODE_ELEMENT_LENGTH);