Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / OFListTlvParser.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.tlv;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.NoSuchElementException;
14
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;
20
21 /**
22  * Parser for {@link org.opendaylight.protocol.pcep.tlv.OFListTlv OFListTlv}
23  */
24 public class OFListTlvParser {
25
26         private static final int OF_CODE_ELEMENT_LENGTH = 2;
27
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 + ".");
33
34                 final List<PCEPOFCodes> ofCodes = new ArrayList<PCEPOFCodes>();
35                 for (int i = 0; i < valueBytes.length; i += OF_CODE_ELEMENT_LENGTH) {
36                         try {
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.");
41                         }
42                 }
43
44                 return new OFListTlv(ofCodes);
45         }
46
47         public static byte[] put(OFListTlv objToSerialize) {
48                 if (objToSerialize == null)
49                         throw new IllegalArgumentException("OFListTlv is mandatory.");
50
51                 final List<PCEPOFCodes> ofCodes = objToSerialize.getOfCodes();
52                 final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
53
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);
58                 }
59
60                 return retBytes;
61         }
62 }