BUG-47 : 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.Arrays;
11 import java.util.List;
12 import java.util.NoSuchElementException;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvParser;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfListTlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder;
22
23 import com.google.common.collect.Lists;
24
25 /**
26  * Parser for {@link OfListTlv}
27  */
28 public class OFListTlvParser implements TlvParser, TlvSerializer {
29
30         public static final int TYPE = 4;
31
32         private static final int OF_CODE_ELEMENT_LENGTH = 2;
33
34         @Override
35         public OfListTlv parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
36                 if (valueBytes == null || valueBytes.length == 0)
37                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
38                 if (valueBytes.length % OF_CODE_ELEMENT_LENGTH != 0)
39                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + ".");
40
41                 final List<OfId> ofCodes = Lists.newArrayList();
42                 for (int i = 0; i < valueBytes.length; i += OF_CODE_ELEMENT_LENGTH) {
43                         try {
44                                 ofCodes.add(new OfId(ByteArray.bytesToShort(Arrays.copyOfRange(valueBytes, i, i + OF_CODE_ELEMENT_LENGTH)) & 0xFFFF));
45                         } catch (final NoSuchElementException nsee) {
46                                 throw new PCEPDeserializerException(nsee, "Unknown OF Code inside OF Code list Tlv.");
47                         }
48                 }
49                 return new OfListBuilder().setCodes(ofCodes).build();
50         }
51
52         @Override
53         public byte[] serializeTlv(final Tlv tlv) {
54                 if (tlv == null)
55                         throw new IllegalArgumentException("OFListTlv is mandatory.");
56                 final OfListTlv oft = (OfListTlv) tlv;
57
58                 final List<OfId> ofCodes = oft.getCodes();
59                 final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
60
61                 final int size = ofCodes.size();
62                 for (int i = 0; i < size; i++) {
63                         ByteArray.copyWhole(ByteArray.shortToBytes(ofCodes.get(i).getValue().shortValue()), retBytes, i * OF_CODE_ELEMENT_LENGTH);
64                 }
65                 return retBytes;
66         }
67
68         @Override
69         public int getType() {
70                 return TYPE;
71         }
72 }