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 io.netty.buffer.ByteBuf;
12 import java.util.List;
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;
23 import com.google.common.collect.Lists;
26 * Parser for {@link OfList}
28 public class OFListTlvParser implements TlvParser, TlvSerializer {
30 public static final int TYPE = 4;
32 private static final int OF_CODE_ELEMENT_LENGTH = 2;
35 public OfList parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
39 if (buffer.readableBytes() % OF_CODE_ELEMENT_LENGTH != 0) {
40 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ".");
42 final List<OfId> ofCodes = Lists.newArrayList();
43 while (buffer.isReadable()) {
44 ofCodes.add(new OfId(buffer.readUnsignedShort()));
46 return new OfListBuilder().setCodes(ofCodes).build();
50 public byte[] serializeTlv(final Tlv tlv) {
52 throw new IllegalArgumentException("OFListTlv is mandatory.");
54 final OfList oft = (OfList) tlv;
56 final List<OfId> ofCodes = oft.getCodes();
57 final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH];
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);
63 return TlvUtil.formatTlv(TYPE, retBytes);