BUG-612 : switched PCEP Tlvs to ByteBuf
[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 io.netty.buffer.ByteBuf;
11
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.spi.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.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
22
23 import com.google.common.collect.Lists;
24
25 /**
26  * Parser for {@link OfList}
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 OfList parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
36                 if (buffer == null) {
37                         return null;
38                 }
39                 if (buffer.readableBytes() % OF_CODE_ELEMENT_LENGTH != 0) {
40                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
41                 }
42                 final List<OfId> ofCodes = Lists.newArrayList();
43                 while (buffer.isReadable()) {
44                         ofCodes.add(new OfId(buffer.readUnsignedShort()));
45                 }
46                 return new OfListBuilder().setCodes(ofCodes).build();
47         }
48
49         @Override
50         public byte[] serializeTlv(final Tlv tlv) {
51                 if (tlv == null) {
52                         throw new IllegalArgumentException("OFListTlv is mandatory.");
53                 }
54                 final OfList oft = (OfList) tlv;
55
56                 final List<OfId> ofCodes = oft.getCodes();
57                 final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
58
59                 final int size = ofCodes.size();
60                 for (int i = 0; i < size; i++) {
61                         ByteArray.copyWhole(ByteArray.shortToBytes(ofCodes.get(i).getValue().shortValue()), retBytes, i * OF_CODE_ELEMENT_LENGTH);
62                 }
63                 return TlvUtil.formatTlv(TYPE, retBytes);
64         }
65 }