2 * Copyright (c) 2014 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.ietf.stateful02;
10 import java.util.Arrays;
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.TlvParser;
14 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import com.google.common.primitives.UnsignedLong;
23 * Parser for {@link LspDbVersion}
25 public class LspDbVersionTlvParser implements TlvParser, TlvSerializer {
27 public static final int TYPE = 23;
29 private static final int DBV_F_LENGTH = 8;
32 public LspDbVersion parseTlv(final byte[] buffer) throws PCEPDeserializerException {
33 return new LspDbVersionBuilder().setVersion(
34 UnsignedLong.fromLongBits(ByteArray.bytesToLong(ByteArray.subByte(buffer, 0, DBV_F_LENGTH))).bigIntegerValue()).build();
38 public byte[] serializeTlv(final Tlv tlv) {
40 throw new IllegalArgumentException("LspDbVersionTlv is mandatory.");
42 final LspDbVersion lsp = (LspDbVersion) tlv;
43 final byte[] array = ByteArray.longToBytes(UnsignedLong.valueOf(lsp.getVersion()).longValue(), DBV_F_LENGTH);
44 if (array.length > DBV_F_LENGTH) {
45 throw new IllegalArgumentException("LspDBVersion too big.");
47 final byte[] result = new byte[DBV_F_LENGTH];
48 Arrays.fill(result, (byte) 0);
50 for (int i = array.length - 1; i >= 0; i--) {
51 result[j] |= array[i];
58 public int getType() {