/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.pcep.impl.tlv; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import org.opendaylight.protocol.pcep.PCEPDeserializerException; import org.opendaylight.protocol.pcep.spi.TlvParser; import org.opendaylight.protocol.pcep.spi.TlvSerializer; import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfListTlv; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder; import com.google.common.collect.Lists; /** * Parser for {@link OfListTlv} */ public class OFListTlvParser implements TlvParser, TlvSerializer { public static final int TYPE = 4; private static final int OF_CODE_ELEMENT_LENGTH = 2; @Override public OfListTlv parseTlv(final byte[] valueBytes) throws PCEPDeserializerException { if (valueBytes == null || valueBytes.length == 0) throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty."); if (valueBytes.length % OF_CODE_ELEMENT_LENGTH != 0) throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "."); final List ofCodes = Lists.newArrayList(); for (int i = 0; i < valueBytes.length; i += OF_CODE_ELEMENT_LENGTH) { try { ofCodes.add(new OfId(ByteArray.bytesToShort(Arrays.copyOfRange(valueBytes, i, i + OF_CODE_ELEMENT_LENGTH)) & 0xFFFF)); } catch (final NoSuchElementException nsee) { throw new PCEPDeserializerException(nsee, "Unknown OF Code inside OF Code list Tlv."); } } return new OfListBuilder().setCodes(ofCodes).build(); } @Override public byte[] serializeTlv(final Tlv tlv) { if (tlv == null) throw new IllegalArgumentException("OFListTlv is mandatory."); final OfListTlv oft = (OfListTlv) tlv; final List ofCodes = oft.getCodes(); final byte[] retBytes = new byte[ofCodes.size() * OF_CODE_ELEMENT_LENGTH]; final int size = ofCodes.size(); for (int i = 0; i < size; i++) { ByteArray.copyWhole(ByteArray.shortToBytes(ofCodes.get(i).getValue().shortValue()), retBytes, i * OF_CODE_ELEMENT_LENGTH); } return retBytes; } @Override public int getType() { return TYPE; } }